Search in sources :

Example 11 with JsonGenerator

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();
    }
}
Also used : ArrayList(java.util.ArrayList) JsonGenerator(org.codehaus.jackson.JsonGenerator) JSONPObject(org.codehaus.jackson.map.util.JSONPObject) JSONPObject(org.codehaus.jackson.map.util.JSONPObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DateTime(org.joda.time.DateTime)

Example 12 with JsonGenerator

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);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(org.codehaus.jackson.JsonEncoding) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 13 with JsonGenerator

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);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(org.codehaus.jackson.JsonEncoding) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 14 with JsonGenerator

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;
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 15 with JsonGenerator

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();
    }
}
Also used : JsonGenerator(org.codehaus.jackson.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

JsonGenerator (org.codehaus.jackson.JsonGenerator)52 JsonFactory (org.codehaus.jackson.JsonFactory)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IOException (java.io.IOException)11 StringWriter (java.io.StringWriter)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)8 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)6 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)5 JsonNode (org.codehaus.jackson.JsonNode)5 OutputStreamWriter (java.io.OutputStreamWriter)4 File (java.io.File)3 HashMap (java.util.HashMap)3 GET (javax.ws.rs.GET)3 Response (javax.ws.rs.core.Response)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BufferedWriter (java.io.BufferedWriter)2 DataOutputStream (java.io.DataOutputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2