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

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

Introduction

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

Prototype

public abstract void writeStartObject() throws IOException, JsonGenerationException;

Source Link

Document

Method for writing starting marker of a JSON Object value (character '{'; plus possible white space decoration if pretty-printing is enabled).

Usage

From source file:com.google.openrtb.json.OpenRtbNativeJsonWriter.java

public final void writeRespLink(NativeResponse.Link link, JsonGenerator gen) throws IOException {
    gen.writeStartObject();
    writeRespLinkFields(link, gen);//from  w  w  w. j a v a 2  s  .  c om
    writeExtensions(link, gen);
    gen.writeEndObject();
}

From source file:com.predic8.membrane.core.interceptor.administration.AdminRESTInterceptor.java

@Mapping("/admin/rest/exchanges(/?\\?.*)?")
public Response getExchanges(QueryParameter params, String relativeRootPath) throws Exception {

    if (params.getString("waitForModification") != null) {
        getRouter().getExchangeStore().waitForModification(params.getLong("waitForModification"));
    }/*w  w w.  j a  va2s  . com*/

    List<AbstractExchange> exchanges;
    synchronized (getRouter().getExchangeStore().getAllExchangesAsList()) {
        exchanges = new ArrayList<AbstractExchange>(getRouter().getExchangeStore().getAllExchangesAsList());
    }

    exchanges = filter(params, exchanges);

    Collections.sort(exchanges, ComparatorFactory.getAbstractExchangeComparator(
            params.getString("sort", "time"), params.getString("order", "desc")));

    int offset = params.getInt("offset", 0);
    int max = params.getInt("max", exchanges.size());

    final int total = exchanges.size();
    final List<AbstractExchange> paginated = exchanges.subList(offset,
            Math.min(offset + max, exchanges.size()));

    return json(new JSONContent() {
        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("exchanges");
            for (AbstractExchange e : paginated) {
                writeExchange(e, gen);
            }
            gen.writeEndArray();
            gen.writeNumberField("total", total);
            gen.writeNumberField("lastModified", getRouter().getExchangeStore().getLastModified());
            gen.writeEndObject();
        }
    });
}

From source file:org.neo4j.ontology.server.unmanaged.AnnotationResource.java

private void writeJsonNodeObjectifiedObject(JsonGenerator jg, Node term, Label annotationLabel)
        throws IOException {
    jg.writeStartObject(); // {
    jg.writeStringField("uri", term.getProperty("uri").toString()); // uri: "http://www.w3.org/2002/07/owl#Thing"
    jg.writeStringField("ontId", term.getProperty("name").toString()); // ontId: "OWL:Thing"
    jg.writeStringField("label", term.getProperty("rdfs:label", term.getProperty("name")).toString()); // ontId: "OWL:Thing"
    jg.writeFieldName("dataSets"); // dataSets:
    jg.writeStartObject(); // {
    jg.writeEndObject(); // }
    writeJsonNodeObjectifiedObjectParents(jg, term, annotationLabel);
    jg.writeEndObject(); // }
}

From source file:ratpack.codahale.metrics.internal.WebSocketReporter.java

private void writeMeters(JsonGenerator json, SortedMap<String, Meter> meters) throws IOException {
    json.writeArrayFieldStart("meters");
    for (Map.Entry<String, Meter> entry : meters.entrySet()) {
        Meter meter = entry.getValue();//  w  ww  . ja v  a2  s .c o  m

        json.writeStartObject();
        json.writeStringField("name", entry.getKey());
        json.writeNumberField("count", meter.getCount());
        json.writeNumberField("meanRate", convertRate(meter.getMeanRate()));
        json.writeNumberField("oneMinuteRate", convertRate(meter.getOneMinuteRate()));
        json.writeNumberField("fiveMinuteRate", convertRate(meter.getFiveMinuteRate()));
        json.writeNumberField("fifteenMinuteRate", convertRate(meter.getFifteenMinuteRate()));
        json.writeEndObject();
    }
    json.writeEndArray();
}

From source file:com.google.openrtb.json.OpenRtbNativeJsonWriter.java

/**
 * Serializes a {@link NativeRequest} to JSON, with a provided {@link JsonGenerator}
 * which allows several choices of output and encoding.
 *//*from w  ww .ja v  a2s. co  m*/
public final void writeNativeRequest(NativeRequest req, JsonGenerator gen) throws IOException {
    gen.writeStartObject();
    if (factory().isRootNativeField()) {
        gen.writeObjectFieldStart("native");
    }
    writeNativeRequestFields(req, gen);
    writeExtensions(req, gen);
    if (factory().isRootNativeField()) {
        gen.writeEndObject();
    }
    gen.writeEndObject();
    gen.flush();
}

From source file:com.google.openrtb.json.OpenRtbNativeJsonWriter.java

/**
 * Serializes a {@link NativeResponse} to JSON, with a provided {@link JsonGenerator}
 * which allows several choices of output and encoding.
 *//*w ww .j a va2 s  .  co  m*/
public final void writeNativeResponse(NativeResponse resp, JsonGenerator gen) throws IOException {
    gen.writeStartObject();
    if (factory().isRootNativeField()) {
        gen.writeObjectFieldStart("native");
    }
    writeNativeResponseFields(resp, gen);
    writeExtensions(resp, gen);
    if (factory().isRootNativeField()) {
        gen.writeEndObject();
    }
    gen.writeEndObject();
    gen.flush();
}

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}//from  w  w  w  . j  a  v a2  s .c  o m
 *  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:test.com.azaptree.services.json.JsonUtilsTest.java

@Test
public void test_serialize_JSONEncoding() throws JsonGenerationException, IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final JsonGenerator generator = JsonUtils.createJsonGenerator(bos, JsonEncoding.UTF8);
    generator.writeStartObject();
    generator.writeStringField("stringField", "stringFieldValue");
    generator.writeNumberField("numberField", 5);
    generator.writeEndObject();/*  ww  w .ja  va2s.co  m*/
    generator.close();

    final Map<String, Object> test = JsonUtils.parse(new ByteArrayInputStream(bos.toByteArray()));
    Assert.assertEquals(test.get("stringField"), "stringFieldValue");
    Assert.assertEquals(test.get("numberField"), 5);
}

From source file:ratpack.codahale.metrics.internal.WebSocketReporter.java

@SuppressWarnings("rawtypes")
private void writeGauges(JsonGenerator json, SortedMap<String, Gauge> gauges) throws IOException {
    json.writeArrayFieldStart("gauges");
    for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
        Gauge gauge = entry.getValue();//from   www  .  j  a  v  a2s .c  o  m

        json.writeStartObject();
        json.writeStringField("name", entry.getKey());
        try {
            json.writeFieldName("value");
            json.writeObject(gauge.getValue());
        } catch (Exception e) {
            LOGGER.log(Level.FINE, "Exception encountered while reporting [" + entry.getKey() + "]: "
                    + e.getLocalizedMessage());
            json.writeNull();
        }
        json.writeEndObject();

    }
    json.writeEndArray();
}

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  w  w .  j ava 2s.co m
 * @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());
}