如何将多态性与 Jackson 中的展开结合起来?

How can I combine polymorphism with unwrapping in Jackson?

我正在使用 jackson 为 json serialization/deserialization 编写带注释的模型 类。

我有一个包含映射的 json,其中键是枚举,值可以是不同类型(包括数组),具体取决于键值。

一个简化的例子,这是我需要的:

{
 "key1": "string value",
 "key2": [{"id":"1", "value": "test1"}, {"id":"2", "value": "test2"}]
}

我试过了,我明白了:

{
  "KEY1": {"value": "string value"},
  "KEY2": {"list": [{"id": "1", "value": "test1"}, {"id": "2", "value": "test2"}]}
}

所以,解包不起作用。

谁能告诉我我做错了什么?

代码如下:

public class Main {

    public static void main(String[] args) throws Exception {

        HashMap<Keys, ValueType> map = new HashMap<>();

        map.put(Keys.KEY1, new StringValue("string value"));

        map.put(Keys.KEY2, new ListValue( Arrays.asList(new Element[] {
                new Element("1", "test1"),
                new Element("2", "test2")
        } )));

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(map);

        System.out.println(s);

    }
}

public enum Keys {

    KEY1("key1"),
    KEY2("key2");

    private String value;

    Keys(String s) {
        this.value = s;
    }
}

public interface ValueType {

}

public class StringValue implements ValueType {

    @JsonUnwrapped
    private String value;

    public StringValue(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public class ListValue implements ValueType {

    @JsonUnwrapped
    private List<Element> list;

    public ListValue(List<Element> list) {
        this.list = list;
    }

    public List<Element> getList() {
        return list;
    }

    public void setList(List<Element> list) {
        this.list = list;
    }
}

public class Element {

    @JsonProperty
    private String id;
    @JsonProperty
    private String value;

    public Element(String id, String value) {
        this.id = id;
        this.value = value;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

您可以使用 JsonValue 注释对 类' getter 方法进行注释,表明 注释访问器的值将用作单个值以序列化例如,而不是通常的收集 value:

属性的方法
public class StringValue implements ValueType {

    @JsonUnwrapped
    private String value;

    public StringValue(String value) {
        this.value = value;
    }
    @JsonValue //<-- added annotation to the original getter method
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public class ListValue implements ValueType {

    @JsonUnwrapped
    private List<Element> list;

    public ListValue(List<Element> list) {
        this.list = list;
    }

    @JsonValue //<-- added annotation to the original getter method
    public List<Element> getList() {
        return list;
    }

    public void setList(List<Element> list) {
        this.list = list;
    }
}