use of com.fasterxml.jackson.databind.util.StdConverter 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.util.StdConverter in project jackson-databind by FasterXML.
the class TestCustomSerializers method testDelegating.
// [databind#87]: delegating serializer
public void testDelegating() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(new StdDelegatingSerializer(Immutable.class, new StdConverter<Immutable, Map<String, Integer>>() {
@Override
public Map<String, Integer> convert(Immutable value) {
HashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("x", value.x());
map.put("y", value.y());
return map;
}
}));
mapper.registerModule(module);
assertEquals("{\"x\":3,\"y\":7}", mapper.writeValueAsString(new Immutable()));
}
Aggregations