Search in sources :

Example 36 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackrabbit-oak by apache.

the class JsonRepresentation method render.

@Override
public void render(PropertyState property, HttpServletResponse response) throws IOException {
    JsonGenerator generator = startResponse(response);
    render(property, generator);
    generator.close();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 37 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project spring-framework by spring-projects.

the class AbstractJackson2HttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        writePrefix(generator, object);
        Class<?> serializationView = null;
        FilterProvider filters = null;
        Object value = object;
        JavaType javaType = null;
        if (object instanceof MappingJacksonValue) {
            MappingJacksonValue container = (MappingJacksonValue) object;
            value = container.getValue();
            serializationView = container.getSerializationView();
            filters = container.getFilters();
        }
        if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
            javaType = getJavaType(type, null);
        }
        ObjectWriter objectWriter;
        if (serializationView != null) {
            objectWriter = this.objectMapper.writerWithView(serializationView);
        } else if (filters != null) {
            objectWriter = this.objectMapper.writer(filters);
        } else {
            objectWriter = this.objectMapper.writer();
        }
        if (javaType != null && javaType.isContainerType()) {
            objectWriter = objectWriter.forType(javaType);
        }
        SerializationConfig config = objectWriter.getConfig();
        if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            objectWriter = objectWriter.with(this.ssePrettyPrinter);
        }
        objectWriter.writeValue(generator, value);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 38 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project spring-framework by spring-projects.

the class MappingJackson2MessageConverter method convertToInternal.

@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
    try {
        Class<?> view = getSerializationView(conversionHint);
        if (byte[].class == getSerializedPayloadClass()) {
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            JsonEncoding encoding = getJsonEncoding(getMimeType(headers));
            JsonGenerator generator = this.objectMapper.getFactory().createGenerator(out, encoding);
            if (view != null) {
                this.objectMapper.writerWithView(view).writeValue(generator, payload);
            } else {
                this.objectMapper.writeValue(generator, payload);
            }
            payload = out.toByteArray();
        } else {
            Writer writer = new StringWriter();
            if (view != null) {
                this.objectMapper.writerWithView(view).writeValue(writer, payload);
            } else {
                this.objectMapper.writeValue(writer, payload);
            }
            payload = writer.toString();
        }
    } catch (IOException ex) {
        throw new MessageConversionException("Could not write JSON: " + ex.getMessage(), ex);
    }
    return payload;
}
Also used : JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 39 with JsonGenerator

use of org.apache.flink.shaded.jackson2.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)

Example 40 with JsonGenerator

use of org.apache.flink.shaded.jackson2.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)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)711 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 IOException (java.io.IOException)170 StringWriter (java.io.StringWriter)145 JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 ByteArrayOutputStream (java.io.ByteArrayOutputStream)66 Map (java.util.Map)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 HashMap (java.util.HashMap)41 Test (org.junit.Test)30 File (java.io.File)27 ArrayList (java.util.ArrayList)26 OutputStream (java.io.OutputStream)25 Writer (java.io.Writer)22 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)21 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)20 FileOutputStream (java.io.FileOutputStream)19