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.messagehub.samples.servlet.MessageList.java

/**
 * Build message list dependent on the format Message Hub requires. The
 * message list is in the form: [{ "value": base_64_string }, ...]
 *
 * @return {String} String representation of a JSON object.
 * @throws IOException/*from   w  w w.  j ava  2s. c om*/
 */
public String build() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = null;

    jsonGenerator = jsonFactory.createGenerator(outputStream);

    jsonGenerator.writeStartArray();

    // Write each message as a JSON object in
    // the form:
    // { "value": base_64_string }
    for (int i = 0; i < this.messages.size(); i++) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("value");
        jsonGenerator.writeObject(this.messages.get(i));
        jsonGenerator.writeEndObject();
    }

    jsonGenerator.writeEndArray();

    // Close underlying streams and return string representation.
    jsonGenerator.close();
    outputStream.close();

    return new String(outputStream.toByteArray());
}

From source file:de.fraunhofer.iosb.ilt.sta.serialize.DataArrayResultSerializer.java

@Override
public void serialize(DataArrayResult value, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    gen.writeStartObject();// w ww  .j a va2s  .  co m
    long count = value.getCount();
    if (count >= 0) {
        gen.writeNumberField("@iot.count", count);
    }
    String nextLink = value.getNextLink();
    if (nextLink != null) {
        gen.writeStringField("@iot.nextLink", nextLink);
    }

    gen.writeFieldName("value");
    gen.writeObject(value.getValue());
    gen.writeEndObject();
}

From source file:org.canova.api.conf.Configuration.java

/**
 *  Writes out all the parameters and their properties (final and resource) to
 *  the given {@link Writer}//ww w  . ja  v a2  s . c om
 *  The format of the output would be
 *  { "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
 *  key2.isFinal,key2.resource}... ] }
 *  It does not output the parameters of the configuration object which is
 *  loaded from an input stream.
 * @param out the Writer to write to
 * @throws IOException
 */
public static void dumpConfiguration(Configuration conf, Writer out) throws IOException {
    Configuration config = new Configuration(conf, true);
    config.reloadConfiguration();
    JsonFactory dumpFactory = new JsonFactory();
    JsonGenerator dumpGenerator = dumpFactory.createGenerator(out);
    dumpGenerator.writeStartObject();
    dumpGenerator.writeFieldName("properties");
    dumpGenerator.writeStartArray();
    dumpGenerator.flush();
    for (Map.Entry<Object, Object> item : config.getProps().entrySet()) {
        dumpGenerator.writeStartObject();
        dumpGenerator.writeStringField("key", (String) item.getKey());
        dumpGenerator.writeStringField("value", config.get((String) item.getKey()));
        dumpGenerator.writeBooleanField("isFinal", config.finalParameters.contains(item.getKey()));
        dumpGenerator.writeStringField("resource", config.updatingResource.get(item.getKey()));
        dumpGenerator.writeEndObject();
    }
    dumpGenerator.writeEndArray();
    dumpGenerator.writeEndObject();
    dumpGenerator.flush();
}

From source file:org.teiid.olingo.TeiidODataJsonSerializer.java

public SerializerResult complexCollection(final ServiceMetadata metadata, final EdmComplexType type,
        final Property result, final ContextURL contextURL, final URI nextLink) throws SerializerException {
    CircleStreamBuffer buffer = new CircleStreamBuffer();
    try {/*  ww w . j a  va 2  s . com*/
        JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
        json.writeStartObject();

        if (contextURL != null && (isODataMetadataFull || !isODataMetadataNone)) {
            json.writeStringField(Constants.JSON_CONTEXT, ContextURLBuilder.create(contextURL).toASCIIString());
        }
        json.writeFieldName(Constants.VALUE);
        json.writeStartArray();
        for (Object value : result.asCollection()) {
            json.writeStartObject();
            writeComplexValue(metadata, type, ((ComplexValue) value).getValue(), null, json, null,
                    (ComplexValue) value, null, result.getName());
            json.writeEndObject();
        }
        json.writeEndArray();

        if (nextLink != null) {
            json.writeStringField(Constants.JSON_NEXT_LINK, nextLink.toASCIIString());
        }

        json.close();
    } catch (final IOException e) {
        throw new SerializerException("An I/O exception occurred.", e,
                SerializerException.MessageKeys.IO_EXCEPTION);
    }
    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
}

From source file:com.example.MessageList.java

/**
 * Build message list dependent on the format Message Hub requires. The
 * message list is in the form: [{ "value": base_64_string }, ...]
 * //from   w ww  .  ja v a 2s  . c  om
 * @return {String} String representation of a JSON object.
 * @throws IOException
 */
public String build() throws IOException {
    final JsonFactory jsonFactory = new JsonFactory();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = null;

    jsonGenerator = jsonFactory.createGenerator(outputStream);

    // [
    jsonGenerator.writeStartArray();

    // Write each message as a JSON object in
    // the form:
    // { "value": base_64_string }
    for (int i = 0; i < this.messages.size(); i++) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("value");
        jsonGenerator.writeObject(this.messages.get(i));
        jsonGenerator.writeEndObject();
    }

    // ]
    jsonGenerator.writeEndArray();

    // Close underlying streams and return string representation.
    jsonGenerator.close();
    outputStream.close();

    return new String(outputStream.toByteArray());
}

From source file:org.teiid.olingo.TeiidODataJsonSerializer.java

public SerializerResult complexCollection(final ServiceMetadata metadata,
        final List<List<ComplexReturnType>> result, final ContextURL contextURL, final URI nextLink)
        throws SerializerException {
    CircleStreamBuffer buffer = new CircleStreamBuffer();
    try {/*ww  w  . j  ava  2 s .c o m*/
        JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
        json.writeStartObject();

        if (contextURL != null && (isODataMetadataFull || !isODataMetadataNone)) {
            json.writeStringField(Constants.JSON_CONTEXT, ContextURLBuilder.create(contextURL).toASCIIString());
        }
        json.writeFieldName(Constants.VALUE);
        json.writeStartArray();
        for (List<ComplexReturnType> ct : result) {
            json.writeStartObject();
            for (final ComplexReturnType type : ct) {
                if (!type.isExpand()) {
                    json.writeStringField(type.getName() + Constants.JSON_NAVIGATION_LINK,
                            type.getEntity().getId().toASCIIString());
                } else {
                    json.writeFieldName(type.getName());
                    writeEntity(metadata, type.getEdmEntityType(), type.getEntity(), null, null, null, null,
                            false, null, type.getName(), json);
                }
            }
            json.writeEndObject();
        }
        json.writeEndArray();

        if (nextLink != null) {
            json.writeStringField(Constants.JSON_NEXT_LINK, nextLink.toASCIIString());
        }

        json.close();
    } catch (final IOException | DecoderException e) {
        throw new SerializerException("An I/O exception occurred.", e,
                SerializerException.MessageKeys.IO_EXCEPTION);
    }
    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
}

From source file:data.DefaultExchanger.java

protected void putBoolean(JsonGenerator generator, String fieldName, ResultSet rs, short index)
        throws SQLException, IOException {
    generator.writeFieldName(fieldName);
    generator.writeBoolean(rs.getBoolean(index));
}

From source file:net.nullschool.grains.jackson.datatype.GrainSerializer.java

private void serializeProperty(PropertyWriter writer, Grain grain, JsonGenerator jgen,
        SerializerProvider provider) throws IOException {

    String key = writer.name;//  w ww  .  j ava  2  s.  c  o m
    Object value = grain.get(key);
    if (value != null && !value.equals(defaultValue.get(key))) {
        // This property's value is not the default value, so write it to the stream.
        jgen.writeFieldName(key);
        writer.serializer.serialize(value, jgen, provider);
    }
}