Example usage for com.fasterxml.jackson.core JsonGenerator writeFieldName

List of usage examples for com.fasterxml.jackson.core JsonGenerator writeFieldName

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator writeFieldName.

Prototype

public abstract void writeFieldName(SerializableString name) throws IOException, JsonGenerationException;

Source Link

Document

Method similar to #writeFieldName(String) , main difference being that it may perform better as some of processing (such as quoting of certain characters, or encoding into external encoding if supported by generator) can be done just once and reused for later calls.

Usage

From source file:com.basho.riak.client.query.MapReduce.java

/**
 * Creates the JSON string of the M/R job for submitting to the
 * {@link RawClient}/*from  ww  w .  j  a  va2 s  .co  m*/
 * 
 * Uses Jackson to write out the JSON string. I'm not very happy with this
 * method, it is a candidate for change.
 * 
 * TODO re-evaluate this method, look for something smaller and more elegant.
 * 
 * @return a String of JSON
 * @throws RiakException
 *             if, for some reason, we can't create a JSON string.
 */
private String writeSpec() throws RiakException {

    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        JsonGenerator jg = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
        jg.setCodec(new ObjectMapper());

        jg.writeStartObject();

        jg.writeFieldName("inputs");
        writeInput(jg);

        jg.writeFieldName("query");
        jg.writeStartArray();

        writePhases(jg);

        jg.writeEndArray();
        if (timeout != null) {
            jg.writeNumberField("timeout", timeout);
        }

        jg.writeEndObject();
        jg.flush();

        return out.toString("UTF8");
    } catch (IOException e) {
        throw new RiakException(e);
    }
}

From source file:io.pdef.json.JsonJacksonFormat.java

private <T extends Message, V> void writeMessageField(final FieldDescriptor<? super T, V> field,
        final T message, final JsonGenerator generator) throws IOException {
    V value = field.get(message);//from   w  w  w.j  a v a2  s .c  om
    if (value == null) {
        // Skip null fields.
        return;
    }

    DataTypeDescriptor<V> type = field.getType();
    generator.writeFieldName(field.getName());
    write(value, type, generator);
}

From source file:ninja.leaping.configurate.json.JSONConfigurationLoader.java

private void generateObject(JsonGenerator generator, ConfigurationNode node) throws IOException {
    if (!node.hasMapChildren()) {
        throw new IOException("Node passed to generateObject does not have map children!");
    }//from  w  w  w. j  a v  a  2s . c  o  m
    generator.writeStartObject();
    for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) {
        generator.writeFieldName(ent.getKey().toString());
        generateValue(generator, ent.getValue());
    }
    generator.writeEndObject();

}

From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java

/**
 * Point coordinates are in x, y order (easting, northing for projected
 * coordinates, longitude, latitude for geographic coordinates):
 *
 * { "type": "Point", "coordinates": [100.0, 0.0] }
 *
 *
 * @param point/*from  ww w.j a v  a2  s.  co m*/
 * @param gen
 * @throws IOException
 */
private void write(Point point, JsonGenerator gen) throws IOException {
    gen.writeStringField("type", "Point");
    gen.writeFieldName("coordinates");
    writeCoordinate(point.getCoordinate(), gen);
}

From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java

/**
 * Coordinates of a MultiPoint are an array of positions:
 *
 * { "type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
 *
 *
 * @param points/*w  w w.ja  v a  2s  . c  o  m*/
 * @param gen
 * @throws IOException
 */
private void write(MultiPoint points, JsonGenerator gen) throws IOException {
    gen.writeStringField("type", "MultiPoint");
    gen.writeFieldName("coordinates");
    writeCoordinates(points.getCoordinates(), gen);
}

From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java

/**
 * Coordinates of LineString are an array of positions :
 *
 * { "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
 *
 * @param geom/*  w  w  w.j  av  a 2s.c o m*/
 * @param gen
 * @throws IOException
 */
private void write(LineString geom, JsonGenerator gen) throws IOException {
    gen.writeStringField("type", "LineString");
    gen.writeFieldName("coordinates");
    writeCoordinates(geom.getCoordinates(), gen);
}

From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java

/**
 * Coordinates of a Polygon are an array of LinearRing coordinate arrays.
 * The first element in the array represents the exterior ring. Any
 * subsequent elements represent interior rings (or holes).
 *
 * No holes:/*w  w w.jav  a 2 s  . c  o  m*/
 *
 * { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0],
 * [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }
 *
 * With holes:
 *
 * { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0],
 * [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ], [ [100.2, 0.2], [100.8, 0.2],
 * [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] ] }
 *
 * @param geom
 * @param gen
 * @throws IOException
 */
private void write(Polygon geom, JsonGenerator gen) throws IOException {
    gen.writeStringField("type", "Polygon");
    gen.writeFieldName("coordinates");
    gen.writeStartArray();
    writeCoordinates(geom.getExteriorRing().getCoordinates(), gen);
    for (int i = 0; i < geom.getNumInteriorRing(); ++i) {
        writeCoordinates(geom.getInteriorRingN(i).getCoordinates(), gen);
    }
    gen.writeEndArray();
}

From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java

/**
 * Coordinates of a MultiLineString are an array of LineString coordinate
 * arrays://from   w  w  w .ja  va  2 s  .  c o m
 *
 * { "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0,
 * 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }
 *
 * @param geom
 * @param gen
 * @throws IOException
 */
private void write(MultiLineString geom, JsonGenerator gen) throws IOException {
    gen.writeStringField("type", "MultiLineString");
    gen.writeFieldName("coordinates");
    gen.writeStartArray();
    for (int i = 0; i < geom.getNumGeometries(); ++i) {
        writeCoordinates(geom.getGeometryN(i).getCoordinates(), gen);
    }
    gen.writeEndArray();
}

From source file:org.h2gis.drivers.geojson.GeoJsonWriteDriver.java

/**
 *
 *
 * Coordinates of a MultiPolygon are an array of Polygon coordinate arrays:
 *
 * { "type": "MultiPolygon", "coordinates": [ [[[102.0, 2.0], [103.0, 2.0],
 * [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0],
 * [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2],
 * [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] }
 *
 * @param geom/* ww  w. jav  a  2 s . c om*/
 * @param gen
 * @throws IOException
 */
private void write(MultiPolygon geom, JsonGenerator gen) throws IOException {
    gen.writeStringField("type", "MultiPolygon");
    gen.writeFieldName("coordinates");
    gen.writeStartArray();
    for (int i = 0; i < geom.getNumGeometries(); ++i) {
        Polygon p = (Polygon) geom.getGeometryN(i);
        gen.writeStartArray();
        writeCoordinates(p.getExteriorRing().getCoordinates(), gen);
        for (int j = 0; j < p.getNumInteriorRing(); ++j) {
            writeCoordinates(p.getInteriorRingN(j).getCoordinates(), gen);
        }
        gen.writeEndArray();
    }
    gen.writeEndArray();
}

From source file:com.yahoo.elide.jsonapi.serialization.KeySerializer.java

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    String str;//from   ww  w  . j a v a 2 s.c  o  m
    Class<?> cls = value.getClass();

    if (cls == String.class) {
        str = (String) value;
    } else if (Date.class.isAssignableFrom(cls)) {
        provider.defaultSerializeDateKey((Date) value, jgen);
        return;
    } else if (cls == Class.class) {
        str = ((Class<?>) value).getName();
    } else if (cls.isEnum()) {
        str = ((Enum<?>) value).name();
    } else {
        str = value.toString();
    }
    jgen.writeFieldName(str);
}