Java - 使用 fasterXML 和 Jackson 的反序列化注释

Java - Deserialization Annotations with fasterXML and Jackson

这里有一个简单的例子,工作代码在构造函数中使用fasterXML/Jackson反序列化字符串列表:

private List<String> xyz;

@JsonCreator
public FooBar(@JsonProperty("blargs") List<String> xyz)
{
    this.xyz = xyz
}

所以,上面的代码工作正常,这是我对如何使用 Jackson 反序列化 json 字符串的理解,如下所示:{"blargs":["one","two","three"]}

所以,这是我的问题:

我的输入 json 现在看起来像这样:

{"blargs":[
    {"fooId":888,"barVal":"tacos"},
    {"fooId":222,"barVal":"hamburgers"},
    {"fooId":444,"barVal":"underpants"}
  ]
}

...但我不知道如何注释构造函数以将传入的 json 反序列化到我的地图中,其中 fooId 和 barVal 成为 key/value 对。

这是我目前正在处理的内容

private Map<Integer, String> xyz;

@JsonCreator
public FooBar(@JsonProperty("blargs") ????? Map<Integer, String> xyz)
{
    this.xyz = xyz
}

注意:我是这样调用上面显示的构造函数的:

ObjectMapper mapper = new ObjectMapper();
FooBar fooBar = mapper.readValue(jsonValue, FooBar.class);

您需要 class 数据

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws IOException {
        String json = "{\"blargs\":[\n"
                + "    {\"fooId\":888,\"barVal\":\"tacos\"},\n"
                + "    {\"fooId\":222,\"barVal\":\"hamburgers\"},\n"
                + "    {\"fooId\":444,\"barVal\":\"underpants\"}\n"
                + "  ]\n"
                + "}";
        ObjectMapper mapper = new ObjectMapper();
        FooBar fooBar = mapper.readValue(json, FooBar.class);
    }

    public static class FooBar {

        private List<MyObject> xyz;

        @JsonCreator
        public FooBar(@JsonProperty("blargs") List<MyObject> xyz) {
            this.xyz = xyz;
            System.out.println(this.xyz);
        }

    }

    public static class MyObject {

        private int fooId;
        private String barVal;

        public int getFooId() {
            return fooId;
        }

        public void setFooId(int fooId) {
            this.fooId = fooId;
        }

        public String getBarVal() {
            return barVal;
        }

        public void setBarVal(String barVal) {
            this.barVal = barVal;
        }

        @Override
        public String toString() {
            return "MyObject{" + "fooId=" + fooId + ", barVal=" + barVal + '}';
        }

    }
}

您可以编写自定义反序列化器:

public static class XyzDeserializer extends JsonDeserializer<Map<Integer, String>> {

    @Override
    public Map<Integer, String> deserialize(JsonParser p,
                                            DeserializationContext ctxt) throws IOException {
        JsonNode rootNode = p.getCodec().readTree(p);
        Map<Integer, String> map = new HashMap<>();
        rootNode.forEach(n -> map.put(
                n.get("fooId").intValue(),
                n.get("barVal").asText()
        ));
        return map;
    }
}

并这样使用:

@JsonCreator
public FooBar(
        @JsonProperty("blargs")
        @JsonDeserialize(using = XyzDeserializer.class) Map<Integer, String> xyz) {
    this.xyz = xyz;
}