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