2

清单:

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 参数化,因此它们的数据字段作为对象返回(实际上是 LinkedHashMap)。

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

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

我的问题是,我将如何使用 Jackson json 让它工作?

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

4

1 回答 1

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;
    }
}
于 2013-04-20T12:25:43.230 回答