Search in sources :

Example 1 with JsonEncoding

use of com.fasterxml.jackson.core.JsonEncoding 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 2 with JsonEncoding

use of com.fasterxml.jackson.core.JsonEncoding 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)

Aggregations

JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 MediaType (org.springframework.http.MediaType)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1