Search in sources :

Example 56 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project fastjson by alibaba.

the class Jackson2Codec method encode.

@Override
public void encode(OutputStream out, Object object) throws Exception {
    Class<?> clazz = object.getClass();
    JsonGenerator generator = constructGenerator(out);
    JavaType type = mapper.getTypeFactory().constructType(clazz);
    ObjectWriter writer = mapper.writerFor(type);
    writer.writeValue(generator, object);
    generator.flush();
    generator.close();
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 57 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project languagetool by languagetool-org.

the class ApiV2 method getLanguages.

String getLanguages() throws IOException {
    StringWriter sw = new StringWriter();
    try (JsonGenerator g = factory.createGenerator(sw)) {
        g.writeStartArray();
        List<Language> languages = new ArrayList<>(Languages.get());
        Collections.sort(languages, (o1, o2) -> o1.getName().compareTo(o2.getName()));
        for (Language lang : languages) {
            g.writeStartObject();
            g.writeStringField("name", lang.getName());
            g.writeStringField("code", lang.getShortCode());
            g.writeStringField("longCode", lang.getShortCodeWithCountryAndVariant());
            g.writeEndObject();
        }
        g.writeEndArray();
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) Language(org.languagetool.Language) ArrayList(java.util.ArrayList) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 58 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project languagetool by languagetool-org.

the class RuleMatchesAsJsonSerializer method ruleMatchesToJson.

/**
   * @param incompleteResults use true to indicate that results are incomplete (e.g. due to a timeout) - a 'warnings'
   *                          section will be added to the JSON
   * @since 3.7
   */
@Experimental
public String ruleMatchesToJson(List<RuleMatch> matches, String text, int contextSize, Language lang, boolean incompleteResults) {
    ContextTools contextTools = new ContextTools();
    contextTools.setEscapeHtml(false);
    contextTools.setContextSize(contextSize);
    contextTools.setErrorMarkerStart(START_MARKER);
    contextTools.setErrorMarkerEnd("");
    StringWriter sw = new StringWriter();
    try {
        try (JsonGenerator g = factory.createGenerator(sw)) {
            g.writeStartObject();
            writeSoftwareSection(g);
            writeWarningsSection(g, incompleteResults);
            writeLanguageSection(g, lang);
            writeMatchesSection(g, matches, text, contextTools);
            g.writeEndObject();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) Experimental(org.languagetool.Experimental)

Example 59 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project blueprints by tinkerpop.

the class GraphSONWriter method outputGraph.

public void outputGraph(final OutputStream jsonOutputStream, final Set<String> vertexPropertyKeys, final Set<String> edgePropertyKeys, final GraphSONMode mode, final boolean normalize) throws IOException {
    final JsonGenerator jg = jsonFactory.createGenerator(jsonOutputStream);
    // don't let the JsonGenerator close the underlying stream...leave that to the client passing in the stream
    jg.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    final GraphSONUtility graphson = new GraphSONUtility(mode, null, ElementPropertyConfig.includeProperties(vertexPropertyKeys, edgePropertyKeys, normalize));
    jg.writeStartObject();
    jg.writeStringField(GraphSONTokens.MODE, mode.toString());
    jg.writeArrayFieldStart(GraphSONTokens.VERTICES);
    final Iterable<Vertex> vertices = vertices(normalize);
    for (Vertex v : vertices) {
        jg.writeTree(graphson.objectNodeFromElement(v));
    }
    jg.writeEndArray();
    jg.writeArrayFieldStart(GraphSONTokens.EDGES);
    final Iterable<Edge> edges = edges(normalize);
    for (Edge e : edges) {
        jg.writeTree(graphson.objectNodeFromElement(e));
    }
    jg.writeEndArray();
    jg.writeEndObject();
    jg.flush();
    jg.close();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Edge(com.tinkerpop.blueprints.Edge)

Example 60 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project YCSB by brianfrankcooper.

the class CouchbaseClient method encode.

/**
   * Encode the object for couchbase storage.
   *
   * @param source the source value.
   * @return the storable object.
   */
private Object encode(final HashMap<String, ByteIterator> source) {
    HashMap<String, String> stringMap = StringByteIterator.getStringMap(source);
    if (!useJson) {
        return stringMap;
    }
    ObjectNode node = JSON_MAPPER.createObjectNode();
    for (Map.Entry<String, String> pair : stringMap.entrySet()) {
        node.put(pair.getKey(), pair.getValue());
    }
    JsonFactory jsonFactory = new JsonFactory();
    Writer writer = new StringWriter();
    try {
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);
        JSON_MAPPER.writeTree(jsonGenerator, node);
    } catch (Exception e) {
        throw new RuntimeException("Could not encode JSON value");
    }
    return writer.toString();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) Map(java.util.Map) StringWriter(java.io.StringWriter) Writer(java.io.Writer) DBException(com.yahoo.ycsb.DBException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)174 StringWriter (java.io.StringWriter)75 IOException (java.io.IOException)48 JsonFactory (com.fasterxml.jackson.core.JsonFactory)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Map (java.util.Map)16 OutputStream (java.io.OutputStream)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonParser (com.fasterxml.jackson.core.JsonParser)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 OutputStreamWriter (java.io.OutputStreamWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 TaskManagerLocation (org.apache.flink.runtime.taskmanager.TaskManagerLocation)6 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)6 ExecutionState (org.apache.flink.runtime.execution.ExecutionState)5 AccessExecutionVertex (org.apache.flink.runtime.executiongraph.AccessExecutionVertex)5 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5