Search in sources :

Example 1 with ClassSerializer

use of com.fasterxml.jackson.databind.ser.std.ClassSerializer in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilderTests method completeSetup.

@Test
void completeSetup() throws JsonMappingException {
    NopAnnotationIntrospector introspector = NopAnnotationIntrospector.instance;
    Map<Class<?>, JsonDeserializer<?>> deserializerMap = new HashMap<>();
    JsonDeserializer<Date> deserializer = new DateDeserializers.DateDeserializer();
    deserializerMap.put(Date.class, deserializer);
    JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
    JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);
    Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().modules(// Disable well-known modules detection
    new ArrayList<>()).serializers(serializer1).serializersByType(Collections.singletonMap(Boolean.class, serializer2)).deserializersByType(deserializerMap).annotationIntrospector(introspector).annotationIntrospector(current -> AnnotationIntrospector.pair(current, introspector)).featuresToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.UNWRAP_ROOT_VALUE, JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS).featuresToDisable(MapperFeature.AUTO_DETECT_GETTERS, MapperFeature.AUTO_DETECT_FIELDS, JsonParser.Feature.AUTO_CLOSE_SOURCE, JsonGenerator.Feature.QUOTE_FIELD_NAMES).serializationInclusion(JsonInclude.Include.NON_NULL);
    ObjectMapper mapper = new ObjectMapper();
    builder.configure(mapper);
    assertThat(getSerializerFactoryConfig(mapper).hasSerializers()).isTrue();
    assertThat(getDeserializerFactoryConfig(mapper).hasDeserializers()).isTrue();
    Serializers serializers = getSerializerFactoryConfig(mapper).serializers().iterator().next();
    assertThat(serializers.findSerializer(null, SimpleType.construct(Class.class), null)).isSameAs(serializer1);
    assertThat(serializers.findSerializer(null, SimpleType.construct(Boolean.class), null)).isSameAs(serializer2);
    assertThat(serializers.findSerializer(null, SimpleType.construct(Number.class), null)).isNull();
    Deserializers deserializers = getDeserializerFactoryConfig(mapper).deserializers().iterator().next();
    assertThat(deserializers.findBeanDeserializer(SimpleType.construct(Date.class), null, null)).isSameAs(deserializer);
    AnnotationIntrospectorPair pair1 = (AnnotationIntrospectorPair) mapper.getSerializationConfig().getAnnotationIntrospector();
    AnnotationIntrospectorPair pair2 = (AnnotationIntrospectorPair) mapper.getDeserializationConfig().getAnnotationIntrospector();
    assertThat(pair1.allIntrospectors()).containsExactly(introspector, introspector);
    assertThat(pair2.allIntrospectors()).containsExactly(introspector, introspector);
    assertThat(mapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)).isTrue();
    assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)).isTrue();
    assertThat(mapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)).isTrue();
    assertThat(mapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)).isTrue();
    assertThat(mapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)).isFalse();
    assertThat(mapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
    assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
    assertThat(mapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)).isFalse();
    assertThat(mapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)).isFalse();
    assertThat(mapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES)).isFalse();
    assertThat(mapper.getSerializationConfig().getSerializationInclusion()).isSameAs(JsonInclude.Include.NON_NULL);
}
Also used : Module(com.fasterxml.jackson.databind.Module) Arrays(java.util.Arrays) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Locale(java.util.Locale) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Map(java.util.Map) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) DeserializerFactoryConfig(com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig) XmlFactory(com.fasterxml.jackson.dataformat.xml.XmlFactory) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Path(java.nio.file.Path) IntRange(kotlin.ranges.IntRange) NopAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector) TimeZone(java.util.TimeZone) Visibility(com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility) Version(com.fasterxml.jackson.core.Version) SerializerFactoryConfig(com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.jupiter.api.Test) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) BasicDeserializerFactory(com.fasterxml.jackson.databind.deser.BasicDeserializerFactory) BasicSerializerFactory(com.fasterxml.jackson.databind.ser.BasicSerializerFactory) Optional(java.util.Optional) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleType(com.fasterxml.jackson.databind.type.SimpleType) CBORFactory(com.fasterxml.jackson.dataformat.cbor.CBORFactory) AnnotationIntrospectorPair(com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Deserializers(com.fasterxml.jackson.databind.deser.Deserializers) ClassSerializer(com.fasterxml.jackson.databind.ser.std.ClassSerializer) DateDeserializers(com.fasterxml.jackson.databind.deser.std.DateDeserializers) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) StreamSupport(java.util.stream.StreamSupport) PropertyAccessor(com.fasterxml.jackson.annotation.PropertyAccessor) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) FatalBeanException(org.springframework.beans.FatalBeanException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) JsonFilter(com.fasterxml.jackson.annotation.JsonFilter) Paths(java.nio.file.Paths) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Collections(java.util.Collections) NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) StringUtils(org.springframework.util.StringUtils) AnnotationIntrospectorPair(com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair) NopAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector) HashMap(java.util.HashMap) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Date(java.util.Date) ClassSerializer(com.fasterxml.jackson.databind.ser.std.ClassSerializer) NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) Deserializers(com.fasterxml.jackson.databind.deser.Deserializers) DateDeserializers(com.fasterxml.jackson.databind.deser.std.DateDeserializers) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with ClassSerializer

use of com.fasterxml.jackson.databind.ser.std.ClassSerializer in project spring-framework by spring-projects.

the class Jackson2ObjectMapperFactoryBeanTests method completeSetup.

@Test
public void completeSetup() {
    NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
    ObjectMapper objectMapper = new ObjectMapper();
    this.factory.setObjectMapper(objectMapper);
    assertThat(this.factory.isSingleton()).isTrue();
    assertThat(this.factory.getObjectType()).isEqualTo(ObjectMapper.class);
    Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<>();
    deserializers.put(Date.class, new DateDeserializer());
    JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
    JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);
    // Disable well-known modules detection
    this.factory.setModules(new ArrayList<>());
    this.factory.setSerializers(serializer1);
    this.factory.setSerializersByType(Collections.singletonMap(Boolean.class, serializer2));
    this.factory.setDeserializersByType(deserializers);
    this.factory.setAnnotationIntrospector(annotationIntrospector);
    this.factory.setFeaturesToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.UNWRAP_ROOT_VALUE, JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);
    this.factory.setFeaturesToDisable(MapperFeature.AUTO_DETECT_GETTERS, MapperFeature.AUTO_DETECT_FIELDS, JsonParser.Feature.AUTO_CLOSE_SOURCE, JsonGenerator.Feature.QUOTE_FIELD_NAMES);
    assertThat(getSerializerFactoryConfig(objectMapper).hasSerializers()).isFalse();
    assertThat(getDeserializerFactoryConfig(objectMapper).hasDeserializers()).isFalse();
    this.factory.setSerializationInclusion(Include.NON_NULL);
    this.factory.afterPropertiesSet();
    assertThat(this.factory.getObject()).isSameAs(objectMapper);
    assertThat(getSerializerFactoryConfig(objectMapper).hasSerializers()).isTrue();
    assertThat(getDeserializerFactoryConfig(objectMapper).hasDeserializers()).isTrue();
    Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
    assertThat(serializers.findSerializer(null, SimpleType.construct(Class.class), null)).isSameAs(serializer1);
    assertThat(serializers.findSerializer(null, SimpleType.construct(Boolean.class), null)).isSameAs(serializer2);
    assertThat(serializers.findSerializer(null, SimpleType.construct(Number.class), null)).isNull();
    assertThat(objectMapper.getSerializationConfig().getAnnotationIntrospector()).isSameAs(annotationIntrospector);
    assertThat(objectMapper.getDeserializationConfig().getAnnotationIntrospector()).isSameAs(annotationIntrospector);
    assertThat(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)).isTrue();
    assertThat(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)).isTrue();
    assertThat(objectMapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)).isTrue();
    assertThat(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)).isTrue();
    assertThat(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)).isFalse();
    assertThat(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
    assertThat(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
    assertThat(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)).isFalse();
    assertThat(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)).isFalse();
    assertThat(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES)).isFalse();
    assertThat(objectMapper.getSerializationConfig().getSerializationInclusion()).isSameAs(Include.NON_NULL);
}
Also used : NopAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector) HashMap(java.util.HashMap) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) ClassSerializer(com.fasterxml.jackson.databind.ser.std.ClassSerializer) NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) DateDeserializer(com.fasterxml.jackson.databind.deser.std.DateDeserializers.DateDeserializer) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 NopAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector)2 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)2 Serializers (com.fasterxml.jackson.databind.ser.Serializers)2 ClassSerializer (com.fasterxml.jackson.databind.ser.std.ClassSerializer)2 NumberSerializer (com.fasterxml.jackson.databind.ser.std.NumberSerializer)2 HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 Visibility (com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility)1 JsonFilter (com.fasterxml.jackson.annotation.JsonFilter)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 PropertyAccessor (com.fasterxml.jackson.annotation.PropertyAccessor)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Version (com.fasterxml.jackson.core.Version)1 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1