use of org.codehaus.jackson.JsonGenerator in project meteo by pierre.
the class StreamResource method buildJsonpResponse.
private Response buildJsonpResponse(final String attribute, final Cache<Object, Object> samples, final String callback) {
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(out);
generator.writeStartObject();
generator.writeFieldName("attribute");
generator.writeString(attribute);
generator.writeFieldName("samples");
generator.writeStartArray();
if (samples != null) {
final ConcurrentMap<Object, Object> samplesForType = samples.asMap();
final List<DateTime> timestamps = new ArrayList<DateTime>();
for (final Object timestamp : samplesForType.keySet()) {
timestamps.add((DateTime) timestamp);
}
Collections.sort(timestamps);
for (final DateTime timestamp : timestamps) {
final Object dataPoint = samplesForType.get(timestamp);
// Might have been evicted already
if (dataPoint != null) {
generator.writeNumber(unixSeconds(timestamp));
generator.writeObject(dataPoint);
}
}
}
generator.writeEndArray();
generator.writeEndObject();
generator.close();
final JSONPObject object = new JSONPObject(callback, out.toString());
return Response.ok(object).build();
} catch (IOException e) {
log.error("Error", e);
return Response.serverError().build();
}
}
use of org.codehaus.jackson.JsonGenerator in project spring-data-document-examples by spring-projects.
the class CouchDbMappingJacksonHttpMessageConverter method writeInternal.
@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
JsonEncoding encoding = getEncoding(outputMessage.getHeaders().getContentType());
JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
try {
if (this.prefixJson) {
jsonGenerator.writeRaw("{} && ");
}
this.objectMapper.writeValue(jsonGenerator, o);
} catch (JsonGenerationException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
use of org.codehaus.jackson.JsonGenerator in project spring-data-document-examples by spring-projects.
the class CouchDbMappingJacksonHttpMessageConverter method writeInternal.
@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
JsonEncoding encoding = getEncoding(outputMessage.getHeaders().getContentType());
JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
try {
if (this.prefixJson) {
jsonGenerator.writeRaw("{} && ");
}
this.objectMapper.writeValue(jsonGenerator, o);
} catch (JsonGenerationException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
use of org.codehaus.jackson.JsonGenerator in project jstorm by alibaba.
the class JSONUtil method formatMap2JSON.
/**
* 将map转化为json
*
* @param map
* @return
*/
public String formatMap2JSON(Map<String, Object> map) {
StringWriter stringWriter = new StringWriter();
String json = "";
try {
JsonGenerator gen = new JsonFactory().createJsonGenerator(stringWriter);
MAPPER.writeValue(gen, map);
gen.close();
json = stringWriter.toString();
} catch (Exception e) {
LOGGER.error("", e);
}
return json;
}
use of org.codehaus.jackson.JsonGenerator in project cassandra by apache.
the class JsonTransformer method keysToJson.
public static void keysToJson(ISSTableScanner currentScanner, Stream<DecoratedKey> keys, boolean rawTime, TableMetadata metadata, OutputStream out) throws IOException {
try (JsonGenerator json = jsonFactory.createJsonGenerator(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
JsonTransformer transformer = new JsonTransformer(json, currentScanner, rawTime, metadata);
json.writeStartArray();
keys.forEach(transformer::serializePartitionKey);
json.writeEndArray();
}
}
Aggregations