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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations