使用Jackson序列化自定义地图

使用Jackson序列化自定义地图

本文介绍了使用Jackson序列化自定义地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将自定义Map序列化为JSON。

I want to serialize a custom Map to JSON.

实现地图界面的类如下:

The class with implements the map interface is the following:

public class MapImpl extends ForwardingMap<String, String> {
//ForwardingMap comes from Guava
    private String                  specialInfo;

    private HashMap<String, String> delegate;

    @Override
    protected Map<String, String> delegate() {
        return this.delegate;
    }
// some getters....
 }

如果我现在打电话

    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(new File("/somePath/myJson.json"), objectOfMapImpl);

杰克逊将序列化地图并忽略变量 specialInfo

Jackson will serialize the map and ignores the variable specialInfo

我尝试使用 JsonSerializer 的自定义实现,但我最终得到了这个片段:

I tried some things with a custom implementation of JsonSerializer but I ended up with this snippet:

    ObjectMapper mapper = new ObjectMapper();

    SimpleModule module = new SimpleModule("someModule");



       module.addSerializer(CheapestResponseDates.class, new JsonSerializer<MapImpl>() {

            @Override
            public void serialize(final MapImpl value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
CheapestResponseDurations.class);
// how to serialize the map here? maybe be in a data node...
                jgen.writeStartObject();
                jgen.writeObjectField("info", value.getInfo());
                jgen.writeEndObject();
            }
        });


    mapper.registerModule(module);

我使用的是JDK 1.7和Jackson 2.3.1

I am using JDK 1.7 and Jackson 2.3.1

推荐答案

您可以利用@ JsonAnySetter / @JsonAnyGetter注释,如。因为,正如您所提到的,您的自定义地图类必须实现Map接口,您可以提取一个单独的bean接口,并告诉Jackson在通过@JsonSerialize(as = ...)注释进行序列化时使用它。

You can leverage the @JsonAnySetter/ @JsonAnyGetter annotations, as described in this blog post. Since, as you mentioned, your custom map class must implement a Map interface, you could extract a separate "bean" interface and tell Jackson to use it instead when serializing via @JsonSerialize(as = ...) annotation.

我稍微修改了你的例子来说明它是如何工作的。请注意,如果要将json字符串反序列化回地图对象,则可能需要执行其他一些操作。

I've slightly modified you example to illustrate how it could work. Note that if you want to deserialize the json string back to your map object, you may need to do some other tricks.

public class MapSerialize  {
public static interface MyInterface {
    String getSpecialInfo();

    @JsonAnyGetter
    Map<String, String> delegate();
}

@JsonSerialize(as = MyInterface.class)
public static class MyImpl extends ForwardingMap<String, String> implements MyInterface {

    private String                  specialInfo;
    private HashMap<String, String> delegate = new HashMap<String, String>();

    public Map<String, String> delegate() {
        return this.delegate;
    }

    @Override
    public String getSpecialInfo() {
        return specialInfo;
    }

    public void setSpecialInfo(String specialInfo) {
        this.specialInfo = specialInfo;
    }

    @Override
    public String put(String key, String value) {
        return delegate.put(key, value);
    }
}

public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    MyImpl objectOfMapImpl = new MyImpl();
    objectOfMapImpl.setSpecialInfo("specialInfo");
    objectOfMapImpl.put("XXX", "YYY");
    String json = mapper.writeValueAsString(objectOfMapImpl);
    System.out.println(json);
}

}

这篇关于使用Jackson序列化自定义地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:06