Search in sources :

Example 1 with JsonDeserializer

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

use of com.fasterxml.jackson.databind.JsonDeserializer 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);
    assertTrue(this.factory.isSingleton());
    assertEquals(ObjectMapper.class, this.factory.getObjectType());
    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);
    assertFalse(getSerializerFactoryConfig(objectMapper).hasSerializers());
    assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
    this.factory.setSerializationInclusion(Include.NON_NULL);
    this.factory.afterPropertiesSet();
    assertSame(objectMapper, this.factory.getObject());
    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));
    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(Include.NON_NULL, objectMapper.getSerializationConfig().getSerializationInclusion());
}
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.Test)

Example 3 with JsonDeserializer

use of com.fasterxml.jackson.databind.JsonDeserializer in project java-chassis by ServiceComb.

the class TestAbstractDeserializer method testDeserialize.

@SuppressWarnings("unchecked")
@Test
public void testDeserialize() {
    boolean status = false;
    try {
        DeserializationContext ctxt = Mockito.mock(DeserializationContext.class);
        @SuppressWarnings("rawtypes") JsonDeserializer JsonDeserializer = Mockito.mock(JsonDeserializer.class);
        Object object = null;
        Mockito.when(jsonParser.nextFieldName()).thenReturn("abc", (String) null);
        Mockito.when(readerHelpData.getDeser()).thenReturn(JsonDeserializer);
        Mockito.when(JsonDeserializer.deserialize(jsonParser, ctxt)).thenReturn(object);
        Object deserializeObject = abstractDeserializer.deserialize(jsonParser, ctxt);
        Assert.assertNotNull(deserializeObject);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Test(org.junit.Test)

Example 4 with JsonDeserializer

use of com.fasterxml.jackson.databind.JsonDeserializer in project presto by prestodb.

the class TestLocalProperties method testJsonSerialization.

@Test
public void testJsonSerialization() throws Exception {
    ObjectMapper mapper = new ObjectMapperProvider().get().registerModule(new SimpleModule().addDeserializer(ColumnHandle.class, new JsonDeserializer<ColumnHandle>() {

        @Override
        public ColumnHandle deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            return new ObjectMapperProvider().get().readValue(jsonParser, TestingColumnHandle.class);
        }
    }));
    TestingColumnHandle columnHandle = new TestingColumnHandle("a");
    LocalProperty<ColumnHandle> property1 = new ConstantProperty<>(columnHandle);
    assertEquals(property1, mapper.readValue(mapper.writeValueAsString(property1), new TypeReference<LocalProperty<ColumnHandle>>() {
    }));
    LocalProperty<ColumnHandle> property2 = new SortingProperty<>(columnHandle, SortOrder.ASC_NULLS_FIRST);
    assertEquals(property2, mapper.readValue(mapper.writeValueAsString(property2), new TypeReference<LocalProperty<ColumnHandle>>() {
    }));
    LocalProperty<ColumnHandle> property3 = new GroupingProperty<>(ImmutableList.of(columnHandle));
    assertEquals(property3, mapper.readValue(mapper.writeValueAsString(property3), new TypeReference<LocalProperty<ColumnHandle>>() {
    }));
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) TestingColumnHandle(com.facebook.presto.sql.planner.TestingColumnHandle) ConstantProperty(com.facebook.presto.spi.ConstantProperty) GroupingProperty(com.facebook.presto.spi.GroupingProperty) SortingProperty(com.facebook.presto.spi.SortingProperty) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) TestingColumnHandle(com.facebook.presto.sql.planner.TestingColumnHandle) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.testng.annotations.Test)

Example 5 with JsonDeserializer

use of com.fasterxml.jackson.databind.JsonDeserializer in project presto by prestodb.

the class TestTupleDomain method testJsonSerialization.

@Test
public void testJsonSerialization() throws Exception {
    TestingTypeManager typeManager = new TestingTypeManager();
    TestingBlockEncodingSerde blockEncodingSerde = new TestingBlockEncodingSerde(typeManager);
    ObjectMapper mapper = new ObjectMapperProvider().get().registerModule(new SimpleModule().addDeserializer(ColumnHandle.class, new JsonDeserializer<ColumnHandle>() {

        @Override
        public ColumnHandle deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            return new ObjectMapperProvider().get().readValue(jsonParser, TestingColumnHandle.class);
        }
    }).addDeserializer(Type.class, new TestingTypeDeserializer(typeManager)).addSerializer(Block.class, new TestingBlockJsonSerde.Serializer(blockEncodingSerde)).addDeserializer(Block.class, new TestingBlockJsonSerde.Deserializer(blockEncodingSerde)));
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.all();
    assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
    }));
    tupleDomain = TupleDomain.none();
    assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
    }));
    tupleDomain = TupleDomain.fromFixedValues(ImmutableMap.of(A, NullableValue.of(BIGINT, 1L), B, NullableValue.asNull(VARCHAR)));
    assertEquals(tupleDomain, mapper.readValue(mapper.writeValueAsString(tupleDomain), new TypeReference<TupleDomain<ColumnHandle>>() {
    }));
}
Also used : TestingColumnHandle(com.facebook.presto.spi.TestingColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TestingBlockEncodingSerde(com.facebook.presto.spi.block.TestingBlockEncodingSerde) IOException(java.io.IOException) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) TestingColumnHandle(com.facebook.presto.spi.TestingColumnHandle) Type(com.facebook.presto.spi.type.Type) TestingTypeDeserializer(com.facebook.presto.spi.type.TestingTypeDeserializer) TestingTypeDeserializer(com.facebook.presto.spi.type.TestingTypeDeserializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Block(com.facebook.presto.spi.block.Block) TestingBlockJsonSerde(com.facebook.presto.spi.block.TestingBlockJsonSerde) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TestingTypeManager(com.facebook.presto.spi.type.TestingTypeManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.testng.annotations.Test)

Aggregations

JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)3 Test (org.junit.Test)3 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 NopAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)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 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)2 HashMap (java.util.HashMap)2 Test (org.testng.annotations.Test)2 ConstantProperty (com.facebook.presto.spi.ConstantProperty)1 GroupingProperty (com.facebook.presto.spi.GroupingProperty)1 SortingProperty (com.facebook.presto.spi.SortingProperty)1 TestingColumnHandle (com.facebook.presto.spi.TestingColumnHandle)1