Jackson 2.1多态反序列化:如何在pojo上填充类型字段?

时间:2021-05-28 18:01:23

I am pulling a tree of Categories and Items from a REST service. Categories have a "child" attribute that contains a list of Categories and/or Items. Their types are specified in the field "kind".

我从REST服务中提取了一个类别和项目树。类别具有“子”属性,其中包含类别和/或项目列表。它们的类型在“kind”字段中指定。

Jackson's polymorphic type handling is great and all working as expected, except one small hitch: the "kind" field itself is not populated. Is there a simple way to get this data onto the pojos? I hope not to have to write custom deserializers.

杰克逊的多态类型处理很棒,并且所有工作都按预期工作,除了一个小故障:“种类”字段本身没有填充。有没有一种简单的方法可以将这些数据输入pojos?我希望不必编写自定义反序列化器。

Here is the base class for Category and Item. The two subclasses add several scalar fields, and aren't very interesting.

这是Category和Item的基类。这两个子类添加了几个标量字段,并不是很有趣。

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonTypeInfo(  
    use = JsonTypeInfo.Id.NAME,  
    include = JsonTypeInfo.As.PROPERTY,  
    property = "kind",
    defaultImpl = EntityBase.Impl.class
    )
@JsonSubTypes({
    @Type(value = Item.class, name = "Item"),  
    @Type(value = Category.class, name = "Category")
    })  
public abstract class EntityBase {
    String title;
    String kind;

    public void setTitle(String title) { this.title = title; }
    public String getTitle() { return title; }

    public void setKind(String kind) { this.kind = kind; }
    public String getKind() { return kind; }

    public static class Impl extends EntityBase {}  
}

I'm doing the deserialization with an ObjectMapper something like this:

我正在使用这样的ObjectMapper进行反序列化:

ObjectMapper mapper = new ObjectMapper();
Category category = mapper.readValue(inputStream, Category.class);

I think it's so irrelevant that it doesn't even deserve a tag, but just in case, this is in an Android app.

我认为这是无关紧要的,它甚至不值得一个标签,但为了以防万一,这是在一个Android应用程序。

1 个解决方案

#1


10  

As usual, I spent a few more minutes searching just before posting this question to make sure I hadn't missed anything obvious.

像往常一样,我在发布这个问题之前花了几分钟时间搜索,以确保我没有错过任何明显的东西。

I wouldn't call it obvious, but I tracked down a resolved jira ticket with the answer. The ticket was linked in comments under a post on http://jackson-users.ning.com/, though I've lost the link to the post.

我不会说它很明显,但是我找到了一张已经解决的jira机票。该票据在http://jackson-users.ning.com/上的帖子中的评论中链接,但我已经丢失了帖子的链接。

There is a "visible" attribute on the JsonTypeInfo annotation which does just this.

JsonTypeInfo注释上有一个“可见”属性,它就是这样做的。

@JsonTypeInfo(  
    use = JsonTypeInfo.Id.NAME,  
    include = JsonTypeInfo.As.PROPERTY,  
    property = "kind",
    visible = true,                    // <----- add this
    defaultImpl = EntityBase.Impl.class
    )
public abstract class EntityBase {
    ...
}

Turns out, this is documented in the javadocs. I had missed it thanks to the excellent SEO on the old 1.5 docs (and the confusing dichotomy between jackson.codehaus.org and fasterxml.com doesn't help), but now I've learned my lesson and I'm looking at docs here: http://wiki.fasterxml.com/JacksonJavaDocs.

事实证明,这是在javadocs中记录的。由于旧的1.5文档上的优秀SEO(以及jackson.codehaus.org和fasterxml.com之间令人困惑的二分法没有帮助),我错过了它,但现在我已经吸取了教训,而且我正在查看文档这里:http://wiki.fasterxml.com/JacksonJavaDocs。

#1


10  

As usual, I spent a few more minutes searching just before posting this question to make sure I hadn't missed anything obvious.

像往常一样,我在发布这个问题之前花了几分钟时间搜索,以确保我没有错过任何明显的东西。

I wouldn't call it obvious, but I tracked down a resolved jira ticket with the answer. The ticket was linked in comments under a post on http://jackson-users.ning.com/, though I've lost the link to the post.

我不会说它很明显,但是我找到了一张已经解决的jira机票。该票据在http://jackson-users.ning.com/上的帖子中的评论中链接,但我已经丢失了帖子的链接。

There is a "visible" attribute on the JsonTypeInfo annotation which does just this.

JsonTypeInfo注释上有一个“可见”属性,它就是这样做的。

@JsonTypeInfo(  
    use = JsonTypeInfo.Id.NAME,  
    include = JsonTypeInfo.As.PROPERTY,  
    property = "kind",
    visible = true,                    // <----- add this
    defaultImpl = EntityBase.Impl.class
    )
public abstract class EntityBase {
    ...
}

Turns out, this is documented in the javadocs. I had missed it thanks to the excellent SEO on the old 1.5 docs (and the confusing dichotomy between jackson.codehaus.org and fasterxml.com doesn't help), but now I've learned my lesson and I'm looking at docs here: http://wiki.fasterxml.com/JacksonJavaDocs.

事实证明,这是在javadocs中记录的。由于旧的1.5文档上的优秀SEO(以及jackson.codehaus.org和fasterxml.com之间令人困惑的二分法没有帮助),我错过了它,但现在我已经吸取了教训,而且我正在查看文档这里:http://wiki.fasterxml.com/JacksonJavaDocs。