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

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

Introduction

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

Prototype

@Deprecated
public JsonGenerator createJsonGenerator(File f, JsonEncoding enc) throws IOException 

Source Link

Document

Method for constructing JSON generator for writing JSON content to specified file, overwriting contents it might have (or creating it if such file does not yet exist).

Usage

From source file:edu.usd.btl.ontology.WriteTree.java

public static void main(String[] args) {

    try {//  w w  w. ja  va 2s .  c  o  m

        JsonFactory jfactory = new JsonFactory();

        /**
         * * write to file **
         */
        JsonGenerator jGenerator = jfactory.createJsonGenerator(
                new File("C:\\Users\\Tyler\\Documents\\GitHub\\BTL\\src\\user.json"), JsonEncoding.UTF8);
        jGenerator.writeStartObject(); // {

        jGenerator.writeStringField("name", "mkyong"); // "name" : "mkyong"
        jGenerator.writeNumberField("age", 29); // "age" : 29

        jGenerator.writeFieldName("messages"); // "messages" :
        jGenerator.writeStartArray(); // [

        jGenerator.writeString("msg 4134134"); // "msg 1"
        jGenerator.writeString("msg 2"); // "msg 2"
        jGenerator.writeString("msg 3"); // "msg 3"

        jGenerator.writeEndArray(); // ]

        jGenerator.writeEndObject(); // }

        jGenerator.close();

    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:com.cedarsoft.couchdb.io.RawCouchDocSerializer.java

@Nonnull
protected static JsonGenerator createJsonGenerator(@Nonnull OutputStream out) throws IOException {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    return jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);
}

From source file:com.cedarsoft.couchdb.io.ActionResponseSerializerTest.java

License:asdf

/**
 * Only used for tests// w w w  . j ava2s  .co  m
 * @param object
 * @param out
 * @throws IOException
 */
@Deprecated
public static void serialize(@Nonnull ActionResponse object, @Nonnull OutputStream out) throws IOException {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();

    JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);

    generator.writeStartObject();

    serialize(generator, object);
    generator.writeEndObject();

    generator.close();
}

From source file:org.apache.drill.exec.ref.rse.JSONDataWriter.java

public JSONDataWriter(OutputStream out) throws IOException {
    JsonFactory f = new JsonFactory();

    this.g = f.createJsonGenerator(out, JsonEncoding.UTF8);
    this.g.useDefaultPrettyPrinter();
}

From source file:com.cedarsoft.couchdb.io.ActionFailedExceptionSerializer.java

public void serialize(@Nonnull ActionFailedException object, @Nonnull OutputStream out) throws IOException {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);

    generator.writeStartObject();/*  ww w  . ja  v  a2s .  c o m*/

    serialize(generator, object);
    generator.writeEndObject();

    generator.close();
}

From source file:com.cedarsoft.couchdb.io.ViewResponseSerializer.java

public <K, V> void serialize(@Nonnull ViewResponse<K, V, ?> viewResponse,
        @Nonnull JacksonSerializer<? super K> keySerializer,
        @Nonnull JacksonSerializer<? super V> valueSerializer, @Nonnull OutputStream out) throws IOException {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);

    generator.writeStartObject();//www . j a  v  a 2 s  . c  om

    generator.writeNumberField(PROPERTY_TOTAL_ROWS, viewResponse.getTotalRows());
    generator.writeNumberField(PROPERTY_OFFSET, viewResponse.getOffset());

    //Now the rows
    generator.writeFieldName(PROPERTY_ROWS);
    generator.writeStartArray();

    for (Row<K, V, ?> row : viewResponse.getRows()) {
        rowSerializer.serialize(row, keySerializer, valueSerializer, generator);
    }

    generator.writeEndArray();

    generator.writeEndObject();
    generator.close();
}

From source file:com.cedarsoft.couchdb.io.RowSerializer.java

public <K, V, D> void serialize(@Nonnull Row<K, V, D> row, @Nonnull JacksonSerializer<? super K> keySerializer,
        @Nonnull JacksonSerializer<? super V> valueSerializer,
        @Nullable JacksonSerializer<? super D> documentSerializer, @Nonnull OutputStream out)
        throws IOException {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);

    serialize(row, keySerializer, valueSerializer, documentSerializer, generator);
    generator.close();//from  w  w  w  .ja  va  2  s.c  om
}

From source file:net.floodlightcontroller.configuration.ConfigurationManager.java

/**
 * Writes a configuration file based on information gathered from
 * the various configuration listeners./*from  w  ww  .j  a v  a  2  s .  c  om*/
 * 
 * @param file An optional configuration file name.
 */
private void writeJsonToFile(String fileName) throws IOException {
    String configFile = (fileName != null) ? fileName : this.fileName;
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode rootNode = createJsonRootNode();
    JsonFactory f = new JsonFactory();
    JsonGenerator g = null;

    try {
        g = f.createJsonGenerator(new File(configFile), JsonEncoding.UTF8);
        g.useDefaultPrettyPrinter();
        mapper.writeTree(g, rootNode);
    } catch (IOException e) {
        if (logger.isErrorEnabled()) {
            logger.error("Could not write the JSON configuration file.");
        }
        throw new IOException("Could not write the JSON configuration file");
    }
}

From source file:DAO.BestellingDAOJson.java

public void createBestelling(Bestelling bestelling, int klant_id) throws SQLException {
    try {//w ww  .ja  v  a2 s.co  m

        JsonFactory jfactory = new JsonFactory();

        /*** write to file ***/

        JsonGenerator jGenerator = jfactory.createJsonGenerator(
                new FileOutputStream("C:\\Users\\maurice\\Desktop\\Workshoptest.json"), JsonEncoding.UTF8);

        jGenerator.writeStartObject();
        jGenerator.writeNumberField("klantID", bestelling.getKlant_id());
        jGenerator.writeNumberField("bestelID", bestelling.getBestelling_id());
        jGenerator.writeFieldName("ArtikelBestellingArray");
        jGenerator.writeStartArray();
        for (ArtikelBestelling artikel : (ArrayList<ArtikelBestelling>) bestelling.getArtikelBestellingList()) {

            jGenerator.writeStartObject();
            jGenerator.writeNumberField("artikelID", artikel.getArtikelPojo().getArtikelID());
            jGenerator.writeNumberField("artikelAantal", artikel.getAantal_artikelen());
            jGenerator.writeEndObject();
        }
        jGenerator.writeEndArray();
        jGenerator.writeEndObject();
        jGenerator.close();
    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:io.airlift.jaxrs.JsonMapper.java

@Override
public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream)
        throws IOException {
    // Prevent broken browser from attempting to render the json as html
    httpHeaders.add(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff");

    JsonFactory jsonFactory = objectMapper.getJsonFactory();
    jsonFactory.setCharacterEscapes(HTMLCharacterEscapes.INSTANCE);

    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(outputStream, JsonEncoding.UTF8);

    // Important: we are NOT to close the underlying stream after
    // mapping, so we need to instruct generator:
    jsonGenerator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);

    // Pretty print?
    if (isPrettyPrintRequested()) {
        jsonGenerator.useDefaultPrettyPrinter();
    }/*from  w  ww.  j a v  a  2s . co m*/

    // 04-Mar-2010, tatu: How about type we were given? (if any)
    JavaType rootType = null;
    if (genericType != null && value != null) {
        // 10-Jan-2011, tatu: as per [JACKSON-456], it's not safe to just force root
        //    type since it prevents polymorphic type serialization. Since we really
        //    just need this for generics, let's only use generic type if it's truly
        //    generic.
        if (genericType.getClass() != Class.class) { // generic types are other implementations of 'java.lang.reflect.Type'
            // This is still not exactly right; should root type be further
            // specialized with 'value.getClass()'? Let's see how well this works before
            // trying to come up with more complete solution.
            rootType = objectMapper.getTypeFactory().constructType(genericType);
            // 26-Feb-2011, tatu: To help with [JACKSON-518], we better recognize cases where
            //    type degenerates back into "Object.class" (as is the case with plain TypeVariable,
            //    for example), and not use that.
            //
            if (rootType.getRawClass() == Object.class) {
                rootType = null;
            }
        }
    }

    String jsonpFunctionName = getJsonpFunctionName();
    if (jsonpFunctionName != null) {
        value = new JSONPObject(jsonpFunctionName, value, rootType);
        rootType = null;
    }

    ObjectWriter writer;
    if (rootType != null) {
        writer = objectMapper.writerWithType(rootType);
    } else {
        writer = objectMapper.writer();
    }

    writer.writeValue(jsonGenerator, value);

    // add a newline so when you use curl it looks nice
    outputStream.write('\n');
}