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());
}
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);
}
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());
}
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());
}
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()));
}
Aggregations