Search in sources :

Example 1 with JsonSerializer

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

the class StandaloneMockMvcBuilderTests method springHandlerInstantiator.

// SPR-13375
@Test
@SuppressWarnings("rawtypes")
public void springHandlerInstantiator() {
    TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PersonController());
    builder.build();
    SpringHandlerInstantiator instantiator = new SpringHandlerInstantiator(builder.wac.getAutowireCapableBeanFactory());
    JsonSerializer serializer = instantiator.serializerInstance(null, null, UnknownSerializer.class);
    assertNotNull(serializer);
}
Also used : JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SpringHandlerInstantiator(org.springframework.http.converter.json.SpringHandlerInstantiator) Test(org.junit.Test)

Example 2 with JsonSerializer

use of com.fasterxml.jackson.databind.JsonSerializer 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());
}
Also used : NopAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) 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.Test)

Example 3 with JsonSerializer

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

the class Jackson2ObjectMapperFactoryBeanTests method customizeDefaultModulesWithSerializer.

// SPR-12634
@Test
public void customizeDefaultModulesWithSerializer() throws JsonProcessingException, UnsupportedEncodingException {
    Map<Class<?>, JsonSerializer<?>> serializers = new HashMap<>();
    serializers.put(Integer.class, new CustomIntegerSerializer());
    this.factory.setSerializersByType(serializers);
    this.factory.afterPropertiesSet();
    ObjectMapper objectMapper = this.factory.getObject();
    DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC);
    assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8"));
    assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid"));
}
Also used : HashMap(java.util.HashMap) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with JsonSerializer

use of com.fasterxml.jackson.databind.JsonSerializer in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageModuleProviderTest method testPageModule.

@Test
public void testPageModule() throws Exception {
    PageModuleProvider pmp = new PageModuleProvider();
    Module module = pmp.getModule();
    assertTrue(module instanceof SimpleModule);
    Field field = module.getClass().getDeclaredField("_serializers");
    field.setAccessible(true);
    SimpleSerializers simpleSerializers = (SimpleSerializers) field.get(module);
    field = simpleSerializers.getClass().getDeclaredField("_interfaceMappings");
    field.setAccessible(true);
    HashMap<ClassKey, JsonSerializer<?>> classMappings = (HashMap) field.get(simpleSerializers);
    assertTrue(classMappings.containsKey(new ClassKey(Page.class)));
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ClassKey(com.fasterxml.jackson.databind.type.ClassKey) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Aggregations

JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)2 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 Module (com.fasterxml.jackson.databind.Module)1 Deserializers (com.fasterxml.jackson.databind.deser.Deserializers)1 DateDeserializers (com.fasterxml.jackson.databind.deser.std.DateDeserializers)1 NopAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 Serializers (com.fasterxml.jackson.databind.ser.Serializers)1 ClassSerializer (com.fasterxml.jackson.databind.ser.std.ClassSerializer)1 NumberSerializer (com.fasterxml.jackson.databind.ser.std.NumberSerializer)1 ClassKey (com.fasterxml.jackson.databind.type.ClassKey)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 DateTime (org.joda.time.DateTime)1 SpringHandlerInstantiator (org.springframework.http.converter.json.SpringHandlerInstantiator)1