Search in sources :

Example 16 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project EnrichmentMapApp by BaderLab.

the class AbstractChart method getObjectMapper.

private ObjectMapper getObjectMapper() {
    // Lazy initialization of ObjectMapper, to make sure any other instance property is already initialized
    if (mapper == null) {
        final SimpleModule module = new SimpleModule();
        addJsonSerializers(module);
        addJsonDeserializers(module);
        mapper = new ObjectMapper();
        mapper.registerModule(module);
    }
    return mapper;
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TestSubtypes method testDefaultImplViaModule.

// [JACKSON-505]: ok to also default to mapping there might be for base type
public void testDefaultImplViaModule() throws Exception {
    final String JSON = "{\"a\":123}";
    // first: without registration etc, epic fail:
    try {
        MAPPER.readValue(JSON, SuperTypeWithoutDefault.class);
        fail("Expected an exception");
    } catch (InvalidTypeIdException e) {
        verifyException(e, "missing type id property '#type'");
    }
    // but then succeed when we register default impl
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addAbstractTypeMapping(SuperTypeWithoutDefault.class, DefaultImpl505.class);
    mapper.registerModule(module);
    SuperTypeWithoutDefault bean = mapper.readValue(JSON, SuperTypeWithoutDefault.class);
    assertNotNull(bean);
    assertEquals(DefaultImpl505.class, bean.getClass());
    assertEquals(123, ((DefaultImpl505) bean).a);
    bean = mapper.readValue("{\"#type\":\"foobar\"}", SuperTypeWithoutDefault.class);
    assertEquals(DefaultImpl505.class, bean.getClass());
    assertEquals(0, ((DefaultImpl505) bean).a);
}
Also used : InvalidTypeIdException(com.fasterxml.jackson.databind.exc.InvalidTypeIdException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 18 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TestSubtypes method testSubtypesViaModule.

// [JACKSON-748]: also works via modules
public void testSubtypesViaModule() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.registerSubtypes(SubB.class, SubC.class, SubD.class);
    mapper.registerModule(module);
    String json = mapper.writeValueAsString(new PropertyBean(new SubC()));
    PropertyBean result = mapper.readValue(json, PropertyBean.class);
    assertSame(SubC.class, result.value.getClass());
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 19 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class UntypedDeserializationTest method testUntypedWithCustomScalarDesers.

public void testUntypedWithCustomScalarDesers() throws IOException {
    SimpleModule m = new SimpleModule("test-module");
    m.addDeserializer(String.class, new UCStringDeserializer());
    m.addDeserializer(Number.class, new CustomNumberDeserializer(13));
    final ObjectMapper mapper = new ObjectMapper().registerModule(m);
    Object ob = mapper.readValue("{\"a\":\"b\", \"nr\":1 }", Object.class);
    assertTrue(ob instanceof Map);
    Object value = ((Map<?, ?>) ob).get("a");
    assertNotNull(value);
    assertTrue(value instanceof String);
    assertEquals("B", value);
    value = ((Map<?, ?>) ob).get("nr");
    assertNotNull(value);
    assertTrue(value instanceof Number);
    assertEquals(Integer.valueOf(13), value);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 20 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class UntypedDeserializationTest method testUntypedWithMapDeser.

public void testUntypedWithMapDeser() throws IOException {
    SimpleModule m = new SimpleModule("test-module");
    m.addDeserializer(Map.class, new MapDeserializer());
    final ObjectMapper mapper = new ObjectMapper().registerModule(m);
    // And then list...
    Object ob = mapper.readValue("{\"a\":true}", Object.class);
    assertTrue(ob instanceof Map<?, ?>);
    Map<?, ?> map = (Map<?, ?>) ob;
    assertEquals(1, map.size());
    assertEquals("Ytrue", map.get("a"));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)112 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)41 Test (org.junit.Test)13 Version (com.fasterxml.jackson.core.Version)8 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)8 Test (org.testng.annotations.Test)8 TestingTypeDeserializer (com.facebook.presto.spi.type.TestingTypeDeserializer)7 TestingTypeManager (com.facebook.presto.spi.type.TestingTypeManager)7 Type (com.facebook.presto.spi.type.Type)7 Block (com.facebook.presto.spi.block.Block)6 TestingBlockEncodingSerde (com.facebook.presto.spi.block.TestingBlockEncodingSerde)6 TestingBlockJsonSerde (com.facebook.presto.spi.block.TestingBlockJsonSerde)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 IOException (java.io.IOException)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)3 Module (com.fasterxml.jackson.databind.Module)3 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)3