Search in sources :

Example 1 with StdConverter

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);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) StdConverter(com.fasterxml.jackson.databind.util.StdConverter)

Example 2 with StdConverter

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()));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) StdDelegatingSerializer(com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer) StdConverter(com.fasterxml.jackson.databind.util.StdConverter)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 StdConverter (com.fasterxml.jackson.databind.util.StdConverter)2 StdDelegatingSerializer (com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer)1