Search in sources :

Example 1 with SerializationConfig

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

use of com.fasterxml.jackson.databind.SerializationConfig in project torodb by torodb.

the class AbstractBackendSerializer method acceptJsonFormatVisitor.

@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException {
    if (visitor == null) {
        return;
    }
    JsonObjectFormatVisitor v = visitor.expectObjectFormat(type);
    SerializerProvider prov = visitor.getProvider();
    final SerializationConfig config = prov.getConfig();
    BeanDescription beanDesc = config.introspect(type);
    JsonSubTypes jsonSubTypes;
    if (v != null) {
        for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
            if (propDef.isExplicitlyIncluded()) {
                jsonSubTypes = propDef.getPrimaryMember().getAnnotation(JsonSubTypes.class);
                if (jsonSubTypes != null) {
                    for (JsonSubTypes.Type jsonSubType : jsonSubTypes.value()) {
                        JavaType subType = TypeFactory.defaultInstance().constructType(jsonSubType.value());
                        depositSchemaProperty(v, jsonSubType.name(), subType);
                    }
                } else {
                    depositSchemaProperty(v, propDef.getName(), propDef.getPrimaryMember().getType(beanDesc.bindingsForBeanType()));
                }
            }
        }
    }
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JavaType(com.fasterxml.jackson.databind.JavaType) JsonObjectFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Example 3 with SerializationConfig

use of com.fasterxml.jackson.databind.SerializationConfig in project jackson-databind by FasterXML.

the class EnumValuesTest method testConstructWithToString.

@SuppressWarnings("unchecked")
public void testConstructWithToString() {
    SerializationConfig cfg = MAPPER.getSerializationConfig().with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
    Class<Enum<?>> enumClass = (Class<Enum<?>>) (Class<?>) ABC.class;
    EnumValues values = EnumValues.construct(cfg, enumClass);
    assertEquals("A", values.serializedValueFor(ABC.A).toString());
    assertEquals("b", values.serializedValueFor(ABC.B).toString());
    assertEquals("C", values.serializedValueFor(ABC.C).toString());
    assertEquals(3, values.values().size());
    assertEquals(3, values.internalMap().size());
}
Also used : SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig)

Example 4 with SerializationConfig

use of com.fasterxml.jackson.databind.SerializationConfig in project spring-framework by spring-projects.

the class Jackson2JsonEncoder method encodeValue.

private DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType type, Map<String, Object> hints) {
    TypeFactory typeFactory = this.mapper.getTypeFactory();
    JavaType javaType = typeFactory.constructType(type.getType());
    if (type.isInstance(value)) {
        javaType = getJavaType(type.getType(), null);
    }
    ObjectWriter writer;
    Class<?> jsonView = (Class<?>) hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
    if (jsonView != null) {
        writer = this.mapper.writerWithView(jsonView);
    } else {
        writer = this.mapper.writer();
    }
    if (javaType != null && javaType.isContainerType()) {
        writer = writer.forType(javaType);
    }
    Boolean sse = (Boolean) hints.get(ServerSentEventHttpMessageWriter.SSE_CONTENT_HINT);
    SerializationConfig config = writer.getConfig();
    if (Boolean.TRUE.equals(sse) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        writer = writer.with(this.ssePrettyPrinter);
    }
    DataBuffer buffer = bufferFactory.allocateBuffer();
    OutputStream outputStream = buffer.asOutputStream();
    try {
        writer.writeValue(outputStream, value);
    } catch (IOException ex) {
        throw new CodecException("Error while writing the data", ex);
    }
    return buffer;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CodecException(org.springframework.core.codec.CodecException) IOException(java.io.IOException) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 5 with SerializationConfig

use of com.fasterxml.jackson.databind.SerializationConfig in project jackson-databind by FasterXML.

the class TestJDKSerialization method testConfigs.

public void testConfigs() throws IOException {
    byte[] base = jdkSerialize(MAPPER.getDeserializationConfig().getBaseSettings());
    assertNotNull(jdkDeserialize(base));
    // first things first: underlying BaseSettings
    DeserializationConfig origDC = MAPPER.getDeserializationConfig();
    SerializationConfig origSC = MAPPER.getSerializationConfig();
    byte[] dcBytes = jdkSerialize(origDC);
    byte[] scBytes = jdkSerialize(origSC);
    DeserializationConfig dc = jdkDeserialize(dcBytes);
    assertNotNull(dc);
    assertEquals(dc._deserFeatures, origDC._deserFeatures);
    SerializationConfig sc = jdkDeserialize(scBytes);
    assertNotNull(sc);
    assertEquals(sc._serFeatures, origSC._serFeatures);
}
Also used : SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) DeserializationConfig(com.fasterxml.jackson.databind.DeserializationConfig)

Aggregations

SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)6 JavaType (com.fasterxml.jackson.databind.JavaType)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 DeserializationConfig (com.fasterxml.jackson.databind.DeserializationConfig)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1 JsonObjectFormatVisitor (com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor)1 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 CodecException (org.springframework.core.codec.CodecException)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 MediaType (org.springframework.http.MediaType)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1