简体   繁体   English

Jackson中的自定义Json Deserializer仅适用于Hashmap

[英]Custom Json Deserializer in Jackson for Hashmap only

I'm writing json serialization (using Jackson) for a hierarchy of Java classes, ie the classes are composed of other classes. 我正在为Java类的层次结构编写json序列化(使用Jackson),即这些类由其他类组成。 Since, I'm not serializing all properties, I've used JsonViews and have annotated only those properties that I want to serialize. 因为我没有序列化所有属性,所以我使用了JsonViews并仅注释了要序列化的那些属性。 The class at the top of this hierarchy contains a Map which also needs to be serialized/deserialized. 此层次结构顶部的类包含一个Map,它也需要序列化/反序列化。 Is it possible to write a serializer/deserializer only for the Map ? 是否可以仅为Map编写序列化器/解串器? I want the default serializer to take care of serializing the rest of the objects 我希望默认的序列化程序负责序列化其余对象

Why this requirement ? 为什么会有这个要求? If I define a serializer for the topmost class, then I would need to do the serialization for all the objects. 如果为最高级的类定义一个序列化器,则需要对所有对象进行序列化。 The JsonGenerator object seems to ignore the JsonView annotations and serializes all properties. JsonGenerator对象似乎忽略了JsonView批注,并序列化了所有属性。

Sure its possible. 当然有可能。 You define your custom Serializer with the Map class generic type, then bind it using Jackson module subsystem. 您可以使用Map类的通用类型定义自定义序列化程序,然后使用Jackson模块子系统对其进行绑定。

Here is an example: (it produces silly custom serialization, but the principal is valid) 这是一个示例:(它产生了愚蠢的自定义序列化,但是主体有效)

public class Test
{
    // the "topmost" class
    public static class DTO {
        public String name = "name";
        public boolean b = false; 
        public int i = 100;

        @JsonView(MyView.class)
        public Map<String, String> map; {
            map = new HashMap<>();
            map.put("key1", "value1");
            map.put("key2", "value2");
            map.put("key3", "value3");
        }
    }

    // just to prove it works with views...
    public static class MyView {}

    // custom serializer for Map 
    public static class MapSerializer extends JsonSerializer<Map> {
        @Override
        public void serialize(Map map, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
            // your custom serialization goes here ....
            gen.writeStartObject();
            gen.writeFieldName("map-keys");
            gen.writeStartArray();
            gen.writeString(map.keySet().toString());
            gen.writeEndArray();
            gen.writeFieldName("map-valuess");
            gen.writeStartArray();
            gen.writeString(map.values().toString());
            gen.writeEndArray();
            gen.writeEndObject();
        }
    }

    public static void main(String[] args) {
        SimpleModule module = new SimpleModule();
        module.addSerializer(Map.class, new MapSerializer());
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
        mapper.registerModule(module);
        try {
            mapper.writerWithView(MyView.class).writeValue(System.out, new DTO());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM