Search in sources :

Example 81 with JsonGenerator

use of 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 82 with JsonGenerator

use of 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 83 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project CFLint by cflint.

the class TestCFLintConfig method test2.

@Test
public void test2() throws IOException {
    StringWriter writer = new StringWriter();
    JsonFactory jsonF = new JsonFactory();
    JsonGenerator jg = jsonF.createGenerator(writer);
    jg.writeStartArray();
    jg.writeEndArray();
    jg.close();
    writer.close();
    System.out.println(writer);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Test(org.junit.Test)

Example 84 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project dropbox-sdk-java by dropbox.

the class DbxRawClientV2 method headerSafeJson.

private static <T> String headerSafeJson(StoneSerializer<T> serializer, T value) {
    StringWriter out = new StringWriter();
    try {
        JsonGenerator g = JSON.createGenerator(out);
        // Escape 0x7F, because it's not allowed in an HTTP header.
        // Escape all non-ASCII because the new HTTP spec recommends against non-ASCII in headers.
        g.setHighestNonEscapedChar(0x7E);
        serializer.serialize(value, g);
        g.flush();
    } catch (IOException ex) {
        throw LangUtil.mkAssert("Impossible", ex);
    }
    return out.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) NetworkIOException(com.dropbox.core.NetworkIOException) IOException(java.io.IOException)

Example 85 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project geode by apache.

the class PdxToJSON method getJSON.

public String getJSON() {
    JsonFactory jf = new JsonFactory();
    // OutputStream os = new ByteArrayOutputStream();
    HeapDataOutputStream hdos = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator jg = jf.createJsonGenerator(hdos, JsonEncoding.UTF8);
        enableDisableJSONGeneratorFeature(jg);
        getJSONString(jg, m_pdxInstance);
        jg.close();
        return new String(hdos.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        hdos.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)187 StringWriter (java.io.StringWriter)81 IOException (java.io.IOException)52 JsonFactory (com.fasterxml.jackson.core.JsonFactory)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Map (java.util.Map)17 OutputStream (java.io.OutputStream)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonParser (com.fasterxml.jackson.core.JsonParser)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 File (java.io.File)7 OutputStreamWriter (java.io.OutputStreamWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)6 List (java.util.List)5 AccessExecutionVertex (org.apache.flink.runtime.executiongraph.AccessExecutionVertex)5 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)4