use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class NullHandlingTest method testListOfNulls.
// [databind#407]
public void testListOfNulls() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(String.class, new FunnyNullDeserializer());
mapper.registerModule(module);
List<String> list = Arrays.asList("funny");
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, String.class);
// should get non-default null directly:
List<?> deser = mapper.readValue("[null]", type);
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals(list.get(0), deser.get(0));
// as well as via ObjectReader
ObjectReader reader = mapper.readerFor(type);
deser = reader.readValue("[null]");
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals(list.get(0), deser.get(0));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class NullHandlingTest method testCustomRootNulls.
public void testCustomRootNulls() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(String.class, new FunnyNullDeserializer());
mapper.registerModule(module);
// should get non-default null directly:
String str = mapper.readValue("null", String.class);
assertNotNull(str);
assertEquals("funny", str);
// as well as via ObjectReader
ObjectReader reader = mapper.readerFor(String.class);
str = reader.readValue("null");
assertNotNull(str);
assertEquals("funny", str);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestCustomDeserializers method testDelegating.
// [Issue#87]: delegating deserializer
public void testDelegating() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(Immutable.class, new StdDelegatingDeserializer<Immutable>(new StdConverter<JsonNode, Immutable>() {
@Override
public Immutable convert(JsonNode value) {
int x = value.path("x").asInt();
int y = value.path("y").asInt();
return new Immutable(x, y);
}
}));
mapper.registerModule(module);
Immutable imm = mapper.readValue("{\"x\":3,\"y\":7}", Immutable.class);
assertEquals(3, imm.x);
assertEquals(7, imm.y);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class SimpleModuleTest method testSimpleBeanSerializer.
/*
/**********************************************************
/* Unit tests; simple serializers
/**********************************************************
*/
public void testSimpleBeanSerializer() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
mod.addSerializer(new CustomBeanSerializer());
mapper.registerModule(mod);
assertEquals(quote("abcde|5"), mapper.writeValueAsString(new CustomBean("abcde", 5)));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class SimpleModuleTest method testSimpleEnumSerializer.
public void testSimpleEnumSerializer() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
mod.addSerializer(new SimpleEnumSerializer());
// for fun, call "multi-module" registration
mapper.registerModules(mod);
assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
}
Aggregations