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

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

Introduction

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

Prototype

public final void writeObjectFieldStart(String fieldName) throws IOException, JsonGenerationException 

Source Link

Document

Convenience method for outputting a field entry ("member") (that will contain a JSON Object value), and the START_OBJECT marker.

Usage

From source file:com.predic8.membrane.core.interceptor.apimanagement.statistics.AMStatisticsCollector.java

private String collectExchangeDataFrom(Exchange exc, String apiKey) throws IOException {
    JsonGenerator gen = getAndResetJsonGenerator();

    try {/*  w  ww  .  j av  a2 s  .  c  om*/
        gen.writeStartObject();
        gen.writeObjectField("excId", exc.getId());
        gen.writeObjectField("excApiKey", apiKey);
        gen.writeObjectFieldStart("Request");
        collectFromMessage(gen, exc.getRequest());
        gen.writeEndObject();
        gen.writeObjectFieldStart("Response");
        collectFromMessage(gen, exc.getResponse());
        gen.writeEndObject();
        gen.writeEndObject();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return getStringFromJsonGenerator();
}

From source file:com.joliciel.jochre.search.highlight.HighlightManagerImpl.java

@Override
public void highlight(Highlighter highlighter, Set<Integer> docIds, Set<String> fields, Writer out) {
    try {/*from w  ww  . j a  v  a  2  s. com*/
        Map<Integer, Set<HighlightTerm>> termMap = highlighter.highlight(docIds, fields);
        JsonFactory jsonFactory = new JsonFactory();
        JsonGenerator jsonGen = jsonFactory.createGenerator(out);

        jsonGen.writeStartObject();

        for (int docId : docIds) {
            Document doc = indexSearcher.doc(docId);
            jsonGen.writeObjectFieldStart(doc.get("id"));
            jsonGen.writeStringField("path", doc.get("path"));
            jsonGen.writeNumberField("docId", docId);

            jsonGen.writeArrayFieldStart("terms");
            for (HighlightTerm term : termMap.get(docId)) {
                fields.add(term.getField());
                term.toJson(jsonGen, df);
            }
            jsonGen.writeEndArray();

            if (includeText) {
                for (String field : fields) {
                    jsonGen.writeObjectFieldStart("field" + field);
                    Set<HighlightTerm> terms = termMap.get(docId);
                    jsonGen.writeStringField("contents", this.displayHighlights(docId, field, terms));
                    jsonGen.writeEndObject();
                }
            }

            jsonGen.writeEndObject();
        }

        jsonGen.writeEndObject();
        jsonGen.flush();
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider.java

@Override
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {

    Object[] args = event.getArgumentArray();

    if (args == null || args.length == 0) {
        return;//from   w ww.j a  v  a 2 s . com
    }

    boolean hasWrittenFieldName = false;

    for (int argIndex = 0; argIndex < args.length; argIndex++) {

        Object arg = args[argIndex];

        if (arg instanceof StructuredArgument) {
            if (!hasWrittenFieldName && getFieldName() != null) {
                generator.writeObjectFieldStart(getFieldName());
                hasWrittenFieldName = true;
            }
            StructuredArgument structuredArgument = (StructuredArgument) arg;
            structuredArgument.writeTo(generator);
        } else if (includeNonStructuredArguments) {
            if (!hasWrittenFieldName && getFieldName() != null) {
                generator.writeObjectFieldStart(getFieldName());
                hasWrittenFieldName = true;
            }
            String fieldName = nonStructuredArgumentsFieldPrefix + argIndex;
            generator.writeObjectField(fieldName, arg);
        }
    }

    if (hasWrittenFieldName) {
        generator.writeEndObject();
    }
}

From source file:com.joliciel.jochre.search.highlight.HighlightManagerImpl.java

@Override
public void findSnippets(Highlighter highlighter, Set<Integer> docIds, Set<String> fields, Writer out) {
    try {/*from w  w  w  .j  a v a  2s .  c o  m*/
        Map<Integer, Set<HighlightTerm>> termMap = highlighter.highlight(docIds, fields);
        Map<Integer, List<Snippet>> snippetMap = this.findSnippets(docIds, fields, termMap,
                this.getSnippetCount(), this.getSnippetSize());

        JsonFactory jsonFactory = new JsonFactory();
        JsonGenerator jsonGen = jsonFactory.createGenerator(out);

        jsonGen.writeStartObject();

        for (int docId : docIds) {
            Document doc = indexSearcher.doc(docId);
            jsonGen.writeObjectFieldStart(doc.get("id"));
            jsonGen.writeStringField("path", doc.get("path"));
            jsonGen.writeNumberField("docId", docId);

            jsonGen.writeArrayFieldStart("snippets");
            for (Snippet snippet : snippetMap.get(docId)) {
                snippet.toJson(jsonGen, df);
            }
            jsonGen.writeEndArray();

            if (includeText) {
                jsonGen.writeArrayFieldStart("snippetText");
                for (Snippet snippet : snippetMap.get(docId)) {
                    jsonGen.writeStartObject();
                    jsonGen.writeStringField("snippet", this.displaySnippet(docId, snippet));
                    jsonGen.writeEndObject();
                }
                jsonGen.writeEndArray();
            }

            if (includeGraphics) {
                jsonGen.writeArrayFieldStart("snippetGraphics");
                for (Snippet snippet : snippetMap.get(docId)) {
                    jsonGen.writeStartObject();
                    ImageSnippet imageSnippet = this.getImageSnippet(snippet);
                    jsonGen.writeNumberField("left", imageSnippet.getRectangle().getLeft());
                    jsonGen.writeNumberField("top", imageSnippet.getRectangle().getTop());
                    jsonGen.writeNumberField("right", imageSnippet.getRectangle().getRight());
                    jsonGen.writeNumberField("bottom", imageSnippet.getRectangle().getBottom());

                    jsonGen.writeArrayFieldStart("highlights");
                    for (Rectangle highlight : imageSnippet.getHighlights()) {
                        jsonGen.writeStartObject();
                        jsonGen.writeNumberField("left", highlight.getLeft());
                        jsonGen.writeNumberField("top", highlight.getTop());
                        jsonGen.writeNumberField("right", highlight.getRight());
                        jsonGen.writeNumberField("bottom", highlight.getBottom());
                        jsonGen.writeEndObject();
                    }
                    jsonGen.writeEndArray();
                    jsonGen.writeEndObject();
                }
                jsonGen.writeEndArray();
            }
            jsonGen.writeEndObject();
        } // next doc

        jsonGen.writeEndObject();
        jsonGen.flush();
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

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

/**
 * Write the GeoJSON properties./*from w  w w.  j a  va 2s.co m*/
 *
 * @param jsonGenerator
 * @param rs
 * @throws IOException
 */
private void writeProperties(JsonGenerator jsonGenerator, ResultSet rs) throws IOException, SQLException {
    if (columnCountProperties != -1) {
        jsonGenerator.writeObjectFieldStart("properties");
        for (Map.Entry<String, Integer> entry : cachedColumnNames.entrySet()) {
            String string = entry.getKey();
            Integer fieldId = entry.getValue();
            jsonGenerator.writeObjectField(string, rs.getObject(fieldId));
        }
        jsonGenerator.writeEndObject();
    }
}

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

/**
 * Write the CRS in the geojson//from   w w w .  j  a  v a  2  s.c o m
 *
 * @param jsonGenerator
 * @param authorityAndSRID
 * @throws IOException
 */
private void writeCRS(JsonGenerator jsonGenerator, String[] authorityAndSRID) throws IOException {
    if (authorityAndSRID[1] != null) {
        jsonGenerator.writeObjectFieldStart("crs");
        jsonGenerator.writeStringField("type", "name");
        jsonGenerator.writeObjectFieldStart("properties");
        StringBuilder sb = new StringBuilder("urn:ogc:def:crs:");
        sb.append(authorityAndSRID[0]).append("::").append(authorityAndSRID[1]);
        jsonGenerator.writeStringField("name", sb.toString());
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndObject();
    }
}

From source file:com.netflix.hystrix.serial.SerialHystrixRequestEvents.java

private static void convertExecutionToJson(JsonGenerator json,
        HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies)
        throws IOException {
    json.writeStartObject();/*from  w  ww.  ja  va2  s.  c  o  m*/
    json.writeStringField("name", executionSignature.getCommandName());
    json.writeArrayFieldStart("events");
    ExecutionResult.EventCounts eventCounts = executionSignature.getEventCounts();
    for (HystrixEventType eventType : HystrixEventType.values()) {
        if (!eventType.equals(HystrixEventType.COLLAPSED)) {
            if (eventCounts.contains(eventType)) {
                int eventCount = eventCounts.getCount(eventType);
                if (eventCount > 1) {
                    json.writeStartObject();
                    json.writeStringField("name", eventType.name());
                    json.writeNumberField("count", eventCount);
                    json.writeEndObject();
                } else {
                    json.writeString(eventType.name());
                }
            }
        }
    }
    json.writeEndArray();
    json.writeArrayFieldStart("latencies");
    for (int latency : latencies) {
        json.writeNumber(latency);
    }
    json.writeEndArray();
    if (executionSignature.getCachedCount() > 0) {
        json.writeNumberField("cached", executionSignature.getCachedCount());
    }
    if (executionSignature.getEventCounts().contains(HystrixEventType.COLLAPSED)) {
        json.writeObjectFieldStart("collapsed");
        json.writeStringField("name", executionSignature.getCollapserKey().name());
        json.writeNumberField("count", executionSignature.getCollapserBatchSize());
        json.writeEndObject();
    }
    json.writeEndObject();
}

From source file:org.springframework.cloud.netflix.metrics.atlas.AtlasMetricObserver.java

private int writeMetrics(JsonGenerator gen, List<Metric> metrics) throws IOException {
    int totalMetricsInBatch = 0;
    gen.writeArrayFieldStart("metrics");

    for (Metric m : metrics) {
        if (!validTags(m.getConfig().getTags()))
            continue;

        if (!Number.class.isAssignableFrom(m.getValue().getClass()))
            continue;

        gen.writeStartObject();//from w  w  w  . java  2s  .c o m

        gen.writeObjectFieldStart("tags");
        gen.writeStringField("name", m.getConfig().getName());
        for (Tag tag : m.getConfig().getTags())
            gen.writeStringField(tag.getKey(), tag.getValue());
        gen.writeEndObject();

        gen.writeNumberField("start", m.getTimestamp());
        gen.writeNumberField("value", m.getNumberValue().doubleValue());

        gen.writeEndObject();

        totalMetricsInBatch++;
    }

    gen.writeEndArray();
    return totalMetricsInBatch;
}

From source file:com.netflix.hystrix.contrib.requests.stream.HystrixRequestEventsJsonStream.java

private static void convertExecutionToJson(JsonGenerator json,
        HystrixRequestEvents.ExecutionSignature executionSignature, List<Integer> latencies)
        throws IOException {
    json.writeStartObject();//from w  ww .j a v a 2s.  c o  m
    json.writeStringField("name", executionSignature.getCommandName());
    json.writeArrayFieldStart("events");
    ExecutionResult.EventCounts eventCounts = executionSignature.getEventCounts();
    for (HystrixEventType eventType : HystrixEventType.values()) {
        if (eventType != HystrixEventType.COLLAPSED) {
            if (eventCounts.contains(eventType)) {
                int eventCount = eventCounts.getCount(eventType);
                if (eventCount > 1) {
                    json.writeStartObject();
                    json.writeStringField("name", eventType.name());
                    json.writeNumberField("count", eventCount);
                    json.writeEndObject();
                } else {
                    json.writeString(eventType.name());
                }
            }
        }
    }
    json.writeEndArray();
    json.writeArrayFieldStart("latencies");
    for (int latency : latencies) {
        json.writeNumber(latency);
    }
    json.writeEndArray();
    if (executionSignature.getCachedCount() > 0) {
        json.writeNumberField("cached", executionSignature.getCachedCount());
    }
    if (executionSignature.getEventCounts().contains(HystrixEventType.COLLAPSED)) {
        json.writeObjectFieldStart("collapsed");
        json.writeStringField("name", executionSignature.getCollapserKey().name());
        json.writeNumberField("count", executionSignature.getCollapserBatchSize());
        json.writeEndObject();
    }
    json.writeEndObject();
}

From source file:com.github.lynxdb.server.api.http.handlers.EpQuery.java

private void saveResponse(OutputStream _output, List<Query> _queries) throws IOException {
    JsonFactory jFactory = new JsonFactory();
    JsonGenerator jGenerator;

    jGenerator = jFactory.createGenerator(_output, JsonEncoding.UTF8);
    jGenerator.writeStartArray();//from www .j a  va2 s . c  o m

    for (Query q : _queries) {
        TimeSerie ts;

        ts = engine.query(q);

        jGenerator.writeStartObject();

        jGenerator.writeStringField("metric", q.getName());

        //tags
        jGenerator.writeObjectFieldStart("tags");
        if (q.getTags() != null) {
            for (String tagk : q.getTags().keySet()) {
                jGenerator.writeStringField(tagk, q.getTags().get(tagk));
            }
        }
        jGenerator.writeEndObject();

        //dps
        jGenerator.writeObjectFieldStart("dps");
        while (ts.hasNext()) {
            Entry e = ts.next();
            jGenerator.writeNumberField(String.valueOf(e.getTime()), e.getValue());
        }
        jGenerator.writeEndObject();

        //endQuery
        jGenerator.writeEndObject();
    }
    jGenerator.writeEndArray();
    jGenerator.close();
    _output.flush();
    _output.close();

}