Search in sources :

Example 1 with StdDelegatingSerializer

use of com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer 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)1 StdDelegatingSerializer (com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer)1 StdConverter (com.fasterxml.jackson.databind.util.StdConverter)1