use of com.fasterxml.jackson.databind.deser.Deserializers in project spring-framework by spring-projects.
the class Jackson2ObjectMapperBuilderTests method completeSetup.
@Test
public void completeSetup() throws JsonMappingException {
NopAnnotationIntrospector annotationIntrospector = 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.<Class<?>, JsonSerializer<?>>singletonMap(Boolean.class, serializer2)).deserializersByType(deserializerMap).annotationIntrospector(annotationIntrospector).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 objectMapper = new ObjectMapper();
builder.configure(objectMapper);
assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Class.class), null));
assertSame(serializer2, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));
Deserializers deserializers = getDeserializerFactoryConfig(objectMapper).deserializers().iterator().next();
assertSame(deserializer, deserializers.findBeanDeserializer(SimpleType.construct(Date.class), null, null));
assertSame(annotationIntrospector, objectMapper.getSerializationConfig().getAnnotationIntrospector());
assertSame(annotationIntrospector, objectMapper.getDeserializationConfig().getAnnotationIntrospector());
assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
assertTrue(objectMapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER));
assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
assertSame(JsonInclude.Include.NON_NULL, objectMapper.getSerializationConfig().getSerializationInclusion());
}
use of com.fasterxml.jackson.databind.deser.Deserializers in project spring-framework by spring-projects.
the class Jackson2ObjectMapperBuilderTests method deserializerByType.
@Test
public void deserializerByType() throws JsonMappingException {
JsonDeserializer<Date> deserializer = new DateDeserializers.DateDeserializer();
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(// Disable well-known modules detection
new ArrayList<>()).deserializerByType(Date.class, deserializer).build();
assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
Deserializers deserializers = getDeserializerFactoryConfig(objectMapper).deserializers().iterator().next();
assertSame(deserializer, deserializers.findBeanDeserializer(SimpleType.construct(Date.class), null, null));
}
Aggregations