use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class JDKStringLikeTypesTest method testStackTraceElementWithCustom.
// [databind#429]
public void testStackTraceElementWithCustom() throws Exception {
// first, via bean that contains StackTraceElement
StackTraceBean bean = MAPPER.readValue(aposToQuotes("{'Location':'foobar'}"), StackTraceBean.class);
assertNotNull(bean);
assertNotNull(bean.location);
assertEquals(StackTraceBean.NUM, bean.location.getLineNumber());
// and then directly, iff registered
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(StackTraceElement.class, new MyStackTraceElementDeserializer());
mapper.registerModule(module);
StackTraceElement elem = mapper.readValue("123", StackTraceElement.class);
assertNotNull(elem);
assertEquals(StackTraceBean.NUM, elem.getLineNumber());
// and finally, even as part of real exception
IOException ioe = mapper.readValue(aposToQuotes("{'stackTrace':[ 123, 456 ]}"), IOException.class);
assertNotNull(ioe);
StackTraceElement[] traces = ioe.getStackTrace();
assertNotNull(traces);
assertEquals(2, traces.length);
assertEquals(StackTraceBean.NUM, traces[0].getLineNumber());
assertEquals(StackTraceBean.NUM, traces[1].getLineNumber());
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestArrayDeserialization method testCustomDeserializers.
/*
/**********************************************************
/* And custom deserializers too
/**********************************************************
*/
public void testCustomDeserializers() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule testModule = new SimpleModule("test", Version.unknownVersion());
testModule.addDeserializer(NonDeserializable[].class, new CustomNonDeserArrayDeserializer());
mapper.registerModule(testModule);
NonDeserializable[] result = mapper.readValue("[\"a\"]", NonDeserializable[].class);
assertNotNull(result);
assertEquals(1, result.length);
assertEquals("a", result[0].value);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestCustomDeserializers method testJsonNodeDelegating.
// [databind#623]
public void testJsonNodeDelegating() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(Immutable.class, new StdNodeBasedDeserializer<Immutable>(Immutable.class) {
@Override
public Immutable convert(JsonNode root, DeserializationContext ctxt) throws IOException {
int x = root.path("x").asInt();
int y = root.path("y").asInt();
return new Immutable(x, y);
}
});
mapper.registerModule(module);
Immutable imm = mapper.readValue("{\"x\":-10,\"y\":3}", Immutable.class);
assertEquals(-10, imm.x);
assertEquals(3, imm.y);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestCustomDeserializers method testContextReadValue.
// [#337]: convenience methods for custom deserializers to use
public void testContextReadValue() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(Bean375Outer.class, new Bean375OuterDeserializer());
module.addDeserializer(Bean375Inner.class, new Bean375InnerDeserializer());
mapper.registerModule(module);
// First, without property; doubles up value:
Bean375Outer outer = mapper.readValue("13", Bean375Outer.class);
assertEquals(26, outer.inner.x);
// then with property; should find annotation, turn negative
Bean375Wrapper w = mapper.readValue("{\"value\":13}", Bean375Wrapper.class);
assertNotNull(w.value);
assertNotNull(w.value.inner);
assertEquals(-13, w.value.inner.x);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class NullHandlingTest method testMapOfNulls.
// Test for [#407]
public void testMapOfNulls() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(String.class, new FunnyNullDeserializer());
mapper.registerModule(module);
JavaType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, String.class);
// should get non-default null directly:
Map<?, ?> deser = mapper.readValue("{\"key\":null}", type);
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals("funny", deser.get("key"));
// as well as via ObjectReader
ObjectReader reader = mapper.readerFor(type);
deser = reader.readValue("{\"key\":null}");
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals("funny", deser.get("key"));
}
Aggregations