use of com.fasterxml.jackson.databind.ser.std.NumberSerializer in project spring-framework by spring-projects.
the class Jackson2ObjectMapperFactoryBeanTests method setModules.
@Test
public void setModules() {
NumberSerializer serializer = new NumberSerializer(Integer.class);
SimpleModule module = new SimpleModule();
module.addSerializer(Integer.class, serializer);
this.factory.setModules(Arrays.asList(new Module[] { module }));
this.factory.afterPropertiesSet();
ObjectMapper objectMapper = this.factory.getObject();
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null)).isSameAs(serializer);
}
use of com.fasterxml.jackson.databind.ser.std.NumberSerializer in project spring-framework by spring-projects.
the class Jackson2ObjectMapperBuilderTests method serializerByType.
@Test
void serializerByType() {
JsonSerializer<Number> serializer = new NumberSerializer(Integer.class);
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(// Disable well-known modules detection
new ArrayList<>()).serializerByType(Boolean.class, serializer).build();
assertThat(getSerializerFactoryConfig(objectMapper).hasSerializers()).isTrue();
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
assertThat(serializers.findSerializer(null, SimpleType.construct(Boolean.class), null)).isSameAs(serializer);
}
use of com.fasterxml.jackson.databind.ser.std.NumberSerializer in project spring-framework by spring-projects.
the class Jackson2ObjectMapperBuilderTests method modules.
@Test
void modules() {
NumberSerializer serializer1 = new NumberSerializer(Integer.class);
SimpleModule module = new SimpleModule();
module.addSerializer(Integer.class, serializer1);
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module).build();
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
assertThat(serializers.findSerializer(null, SimpleType.construct(Integer.class), null)).isSameAs(serializer1);
}
use of com.fasterxml.jackson.databind.ser.std.NumberSerializer 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);
}
use of com.fasterxml.jackson.databind.ser.std.NumberSerializer 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);
}
Aggregations