杰克逊杰森与嵌套参数类

时间:2013-04-20 01:47:04

标签: java json jackson

清单:

import java.util.List;

public class Listing<T> {
    List<Thing<T>> children;

    public List<Thing<T>> getChildren() {
        return children;
    }

    public void setChildren(List<Thing<T>> children) {
        this.children = children;
    }
}

件事:

public class Thing<T> {

    private String type;
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

链接:

public class Link {
    private String author;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

这是序列化和反序列化的一个例子......

public static void main(String[] args) throws IOException {
    Link link1 = new Link();
    link1.setAuthor("JohnDoe");

    Link link2 = new Link();
    link2.setAuthor("MaryJane");

    List<Thing<Link>> things = new ArrayList<Thing<Link>>();

    Thing<Link> thing1 = new Thing();
    thing1.setData(link1);
    thing1.setType("t3");

    Thing<Link> thing2 = new Thing();
    thing2.setData(link2);
    thing2.setType("t3");

    things.add(thing1);
    things.add(thing2);

    Listing<Link> listing = new Listing<Link>();
    listing.setChildren(things);


    Thing<Listing> thing = new Thing<Listing>();
    thing.setType("listing");
    thing.setData(listing);

    File jsonFile = new File("src/testMap.txt");
    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(jsonFile, thing);

    //String jsonString = "{\"type\":\"listing\",\"data\":{\"children\":[{\"type\":\"t3\",\"data\":{\"author\":\"JohnDoe\"}},{\"type\":\"t3\",\"data\":{\"author\":\"MaryJane\"}}]}}";
    JavaType jsonType = mapper.getTypeFactory().constructParametricType(Thing.class, Listing.class);
    Thing<Listing> readThing = mapper.readValue(jsonFile, jsonType);


}

我遇到的问题是上面示例代码中列表中包含的东西没有使用Link进行参数化,因此它们的数据字段作为Object(实际上是LinkedHashMap)返回。

我希望能够做到这样的事情:

List<Thing<Link>> readListingChildren = readThing.getData().getChildren();
String author = readListingChildren.get(0).getData().getAuthor();

我的问题是,如何使用杰克逊杰森让这个工作?

注意:Things会包含多种不同类型的对象,并且数据对象的“type”字段定义(或应该定义)Thing的数据成员类型,使用t1,t2,t3等字符串,等映射到不同类的。

1 个答案:

答案 0 :(得分:4)

实现像

这样的序列化字符串
{
    "data":{
        "type":"listing",
        "children":[
            {
                    "data":{
                    "type":"t3",
                    "author":"JohnDoe"
                }
            },
            {
                "data":{
                    "type":"t3",
                    "author":"MaryJane"
                }
            }
        ]
    }
}

并使用type信息正确反序列化您可能使用的具体类

@JsonTypeName("listing")
public class Listing<T> {
    List<Thing<T>> children;

    public List<Thing<T>> getChildren() {
        return children;
    }

    public void setChildren(final List<Thing<T>> children) {
        this.children = children;
    }
}

public class Thing<T> {
    private T data;

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({
      @JsonSubTypes.Type(Link.class),
      @JsonSubTypes.Type(Listing.class)
    })
    public T getData() {
        return data;
    }

    public void setData(final T data) {
        this.data = data;
    }
}

@JsonTypeName("t3")
public class Link {
    private String author;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(final String author) {
        this.author = author;
    }
}