use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class SimpleModuleTest method testSimpleInterfaceSerializer.
public void testSimpleInterfaceSerializer() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
mod.addSerializer(new BaseSerializer());
// and another variant here too
List<Module> mods = Arrays.asList((Module) mod);
mapper.registerModules(mods);
assertEquals(quote("Base:1"), mapper.writeValueAsString(new Impl1()));
assertEquals(quote("Base:2"), mapper.writeValueAsString(new Impl2()));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class SimpleModuleTest method testSimpleBeanDeserializer.
/*
/**********************************************************
/* Unit tests; simple deserializers
/**********************************************************
*/
public void testSimpleBeanDeserializer() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
mod.addDeserializer(CustomBean.class, new CustomBeanDeserializer());
mapper.registerModule(mod);
CustomBean bean = mapper.readValue(quote("xyz|3"), CustomBean.class);
assertEquals("xyz", bean.str);
assertEquals(3, bean.num);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class SimpleModuleTest method testMultipleModules.
public void testMultipleModules() throws Exception {
MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
SimpleModule mod2 = new SimpleModule("test2", Version.unknownVersion());
mod1.addSerializer(SimpleEnum.class, new SimpleEnumSerializer());
mod1.addDeserializer(CustomBean.class, new CustomBeanDeserializer());
Map<Class<?>, JsonDeserializer<?>> desers = new HashMap<>();
desers.put(SimpleEnum.class, new SimpleEnumDeserializer());
mod2.setDeserializers(new SimpleDeserializers(desers));
mod2.addSerializer(CustomBean.class, new CustomBeanSerializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(mod1);
mapper.registerModule(mod2);
assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
SimpleEnum result = mapper.readValue(quote("a"), SimpleEnum.class);
assertSame(SimpleEnum.A, result);
// also let's try it with different order of registration, just in case
mapper = new ObjectMapper();
mapper.registerModule(mod2);
mapper.registerModule(mod1);
assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
result = mapper.readValue(quote("a"), SimpleEnum.class);
assertSame(SimpleEnum.A, result);
}
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);
}
Aggregations