Example usage for com.fasterxml.jackson.core JsonFactory createGenerator

List of usage examples for com.fasterxml.jackson.core JsonFactory createGenerator

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory createGenerator.

Prototype

public JsonGenerator createGenerator(Writer out) throws IOException 

Source Link

Document

Method for constructing JSON generator for writing JSON content using specified Writer.

Usage

From source file:com.github.jonpeterson.jackson.module.interceptor.JsonInterceptingSerializer.java

@Override
public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException {
    // serialize the value into a byte array buffer then parse it back out into a JsonNode tree
    // TODO: find a better way to convert the value into a tree
    JsonFactory factory = generator.getCodec().getFactory();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(4096);
    JsonGenerator bufferGenerator = factory.createGenerator(buffer);
    try {/*  ww w  .  ja  va2  s  .c o m*/
        delegate.serialize(value, bufferGenerator, provider);
    } finally {
        bufferGenerator.close();
    }
    JsonNode jsonNode = factory.createParser(buffer.toByteArray()).readValueAsTree();

    // execute interceptors on node
    for (JsonInterceptor interceptor : interceptors)
        jsonNode = interceptor.intercept(jsonNode, jsonNodeFactory);

    // write node
    generator.writeTree(jsonNode);
}

From source file:org.wso2.carbon.mediator.datamapper.engine.output.writers.JSONWriter.java

public JSONWriter(Schema outputSchema) throws SchemaException, WriterException {
    this.outputSchema = outputSchema;
    this.writer = new StringWriter();
    this.schemaElementList = new ArrayList<>();
    this.schemaElementList.add(new SchemaElement(outputSchema.getName()));
    try {/* w ww  .j ava2 s.c  o m*/
        JsonFactory jsonFactory = new JsonFactory();
        this.jsonGenerator = jsonFactory.createGenerator(writer);
        writeStartAnonymousObject();
    } catch (IOException e) {
        throw new WriterException("Error while creating json generator. " + e.getMessage());
    }
}

From source file:org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.ImplementationsOfOneRelationshipTypeResource.java

/**
 * required by implementations.jsp//from   w w  w. java 2s .  com
 *
 * Method similar top the one of ImplementationsOfOneNodeTypeResource
 *
 * @return for each node type implementation implementing the associated
 *         node type
 */
@Override
public String getImplementationsTableData() {
    String res;
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter tableDataSW = new StringWriter();
    try {
        JsonGenerator jGenerator = jsonFactory.createGenerator(tableDataSW);
        jGenerator.writeStartArray();

        Collection<RelationshipTypeImplementationId> allNTIids = BackendUtils
                .getAllElementsRelatedWithATypeAttribute(RelationshipTypeImplementationId.class,
                        this.getTypeId().getQName());
        for (RelationshipTypeImplementationId ntiID : allNTIids) {
            jGenerator.writeStartArray();
            jGenerator.writeString(ntiID.getNamespace().getDecoded());
            jGenerator.writeString(ntiID.getXmlId().getDecoded());
            jGenerator.writeEndArray();
        }
        jGenerator.writeEndArray();
        jGenerator.close();
        tableDataSW.close();
        res = tableDataSW.toString();
    } catch (Exception e) {
        ImplementationsOfOneRelationshipTypeResource.LOGGER.error(e.getMessage(), e);
        res = "[]";
    }
    return res;
}

From source file:org.eclipse.winery.repository.resources.entitytypes.nodetypes.ImplementationsOfOneNodeTypeResource.java

/**
 * required by implementations.jsp/*from  www.  jav  a 2s  .c  o  m*/
 *
 * @return for each node type implementation implementing the associated
 *         node type
 */
@Override
public String getImplementationsTableData() {
    String res;
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter tableDataSW = new StringWriter();
    try {
        JsonGenerator jGenerator = jsonFactory.createGenerator(tableDataSW);
        jGenerator.writeStartArray();

        Collection<NodeTypeImplementationId> allNodeTypeImplementations = BackendUtils
                .getAllElementsRelatedWithATypeAttribute(NodeTypeImplementationId.class,
                        this.getTypeId().getQName());
        for (NodeTypeImplementationId ntiID : allNodeTypeImplementations) {
            jGenerator.writeStartArray();
            jGenerator.writeString(ntiID.getNamespace().getDecoded());
            jGenerator.writeString(ntiID.getXmlId().getDecoded());
            jGenerator.writeEndArray();
        }
        jGenerator.writeEndArray();
        jGenerator.close();
        tableDataSW.close();
        res = tableDataSW.toString();
    } catch (Exception e) {
        ImplementationsOfOneNodeTypeResource.LOGGER.error(e.getMessage(), e);
        res = "[]";
    }
    return res;
}

From source file:net.geco.model.iojson.JacksonSerializer.java

public JacksonSerializer(Writer writer, boolean debug) throws IOException {
    this();//from   w  w  w.  j  a v a2  s.  c o m
    JsonFactory jsonFactory = new JsonFactory();
    gen = jsonFactory.createGenerator(writer);
    if (debug) {
        gen.useDefaultPrettyPrinter();
    } else {
        gen.configure(Feature.QUOTE_FIELD_NAMES, false);
    }
}

From source file:org.dswarm.xsd2jsonschema.model.JSRoot.java

public String render() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory();
    final StringWriter writer = new StringWriter();

    final JsonGenerator generator = jsonFactory.createGenerator(writer);
    generator.setPrettyPrinter(new DefaultPrettyPrinter());

    render(generator);/*w  ww  .  j  ava 2  s . c  o m*/

    return writer.getBuffer().toString();
}

From source file:org.fluentd.jvmwatcher.parser.JsonSimpleLogParser.java

@Override
public boolean parseState(PrintWriter out, JvmWatchState state) {
    boolean ret = false;
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator generator = null;//from  ww  w  . j  av  a2  s . c om
    try {
        generator = jsonFactory.createGenerator(out);
        // convert to JSON stream.
        this.outSimpleLog(generator, state);
        ret = true;
    } catch (IOException ex) {
        log.error("Parse output error.", ex);
        ret = false;
    } finally {
        if (null != generator) {
            try {
                // flush to JSON stream.
                generator.flush();
            } catch (IOException ex) {
                log.error("writer flush error.", ex);
            }
        }
    }

    return ret;
}

From source file:com.quinsoft.zeidon.utils.WriteOisToJsonStreamNoIncrementals.java

@Override
public void writeToStream(SerializeOi options, Writer writer) {
    this.viewList = options.getViewList();
    if (options.getFlags() == null)
        flags = EnumSet.noneOf(WriteOiFlags.class);
    else//ww w .  j a  v a2  s  . c  om
        flags = options.getFlags();
    if (flags.contains(WriteOiFlags.INCREMENTAL))
        throw new ZeidonException("This JSON stream writer not intended for writing incremental.");

    this.options = options;

    JsonFactory jsonF = new JsonFactory();
    try {
        jg = jsonF.createGenerator(writer);
        jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier
        jg.writeStartObject();

        jg.writeStringField("version", VERSION);

        for (View view : viewList)
            writeOi(view);

        jg.writeEndObject();
        jg.close();
    } catch (Exception e) {
        throw ZeidonException.wrapException(e);
    }
}

From source file:org.jahia.loganalyzer.writers.internal.JSONLogEntryWriter.java

public JSONLogEntryWriter(File htmlFile) throws IOException {
    JsonFactory factory = new JsonFactory();
    // configure, if necessary:
    factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    fileWriter = new FileWriter(htmlFile);
    jsonGenerator = factory.createGenerator(fileWriter);
    jsonGenerator.setCodec(new ObjectMapper());
    jsonGenerator.useDefaultPrettyPrinter();
    jsonGenerator.writeStartArray();//from www  .j  ava 2  s .com
}

From source file:org.apache.orc.bench.convert.json.JsonWriter.java

public JsonWriter(Path path, TypeDescription schema, Configuration conf, CompressionKind compression)
        throws IOException {
    OutputStream file = path.getFileSystem(conf).create(path, true);
    outStream = new OutputStreamWriter(compression.create(file), StandardCharsets.UTF_8);
    JsonFactory factory = new JsonFactory();
    factory.setRootValueSeparator("\n");
    writer = factory.createGenerator(outStream);
    this.schema = schema;
}