Search in sources :

Example 96 with SimpleModule

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

the class TestAbstractTypes method testDefaultingRecursive.

// [databind#700]
public void testDefaultingRecursive() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    // defaults: LinkedHashMap, ArrayList
    mod.addAbstractTypeMapping(Map.class, TreeMap.class);
    mod.addAbstractTypeMapping(List.class, LinkedList.class);
    mapper.registerModule(mod);
    Object result;
    result = mapper.readValue("[ {} ]", Object.class);
    assertEquals(LinkedList.class, result.getClass());
    Object v = ((List<?>) result).get(0);
    assertNotNull(v);
    assertEquals(TreeMap.class, v.getClass());
    result = mapper.readValue("{ \"x\": [ 3 ] }", Object.class);
    assertEquals(TreeMap.class, result.getClass());
    Map<?, ?> map = (Map<?, ?>) result;
    assertEquals(1, map.size());
    v = map.get("x");
    assertNotNull(v);
    assertEquals(LinkedList.class, v.getClass());
    assertEquals(1, ((List<?>) v).size());
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 97 with SimpleModule

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

the class TestAbstractTypes method testInterfaceDefaulting.

public void testInterfaceDefaulting() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    // let's ensure we get hierarchic mapping
    mod.addAbstractTypeMapping(CharSequence.class, MyString.class);
    mapper.registerModule(mod);
    Object result = mapper.readValue(quote("abc"), CharSequence.class);
    assertEquals(MyString.class, result.getClass());
    assertEquals("abc", ((MyString) result).value);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 98 with SimpleModule

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

the class AbstracTypeMapping1186Test method testDeserializeMyContainer.

public void testDeserializeMyContainer() throws Exception {
    Module module = new SimpleModule().addAbstractTypeMapping(IContainer.class, MyContainer.class);
    final ObjectMapper mapper = new ObjectMapper().registerModule(module);
    String json = "{\"ts\": [ { \"msg\": \"hello\"} ] }";
    final Object o = mapper.readValue(json, mapper.getTypeFactory().constructParametricType(IContainer.class, MyObject.class));
    assertEquals(MyContainer.class, o.getClass());
    MyContainer<?> myc = (MyContainer<?>) o;
    assertEquals(1, myc.ts.size());
    Object value = myc.ts.get(0);
    assertEquals(MyObject.class, value.getClass());
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 99 with SimpleModule

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

the class TypeResolverTest method testSubtypeResolution.

public static void testSubtypeResolution() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleAbstractTypeResolver resolver = new SimpleAbstractTypeResolver();
    resolver.addMapping(Map.class, MyMap.class);
    SimpleModule basicModule = new SimpleModule();
    basicModule.setAbstractTypes(resolver);
    mapper.registerModule(basicModule);
    String value = "{\"z\": {\"zz\": {\"a\": 42}}}";
    A a = mapper.readValue(value, A.class);
    Map map = a.getMap();
    assertEquals(MyMap.class, map.getClass());
    Object ob = map.get("zz");
    assertEquals(B.class, ob.getClass());
}
Also used : SimpleAbstractTypeResolver(com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 100 with SimpleModule

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

the class TestMixinMerging method testDisappearingMixins515.

/*
    /**********************************************************
    /* Unit tests
    /**********************************************************
     */
// for [Issue#515]
public void testDisappearingMixins515() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS).disable(MapperFeature.AUTO_DETECT_FIELDS).disable(MapperFeature.AUTO_DETECT_GETTERS).disable(MapperFeature.AUTO_DETECT_IS_GETTERS).disable(MapperFeature.INFER_PROPERTY_MUTATORS);
    SimpleModule module = new SimpleModule("Test");
    module.setMixInAnnotation(Person.class, PersonMixin.class);
    mapper.registerModule(module);
    assertEquals("{\"city\":\"Seattle\"}", mapper.writeValueAsString(new PersonImpl()));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)108 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)39 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 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)4 IOException (java.io.IOException)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonParser (com.fasterxml.jackson.core.JsonParser)3 Module (com.fasterxml.jackson.databind.Module)3 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2