Jackson序列化列表不包含子类型属性

时间:2015-02-17 17:14:38

标签: java json serialization jackson

我正在尝试使用Jackson序列化对象列表(这是子类),但我无法让Jackson序列化type-in​​fo-property。

这是我的代码的本质:

// Superclass A that instructs Jackson to include a type property in the serialized json string 
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true, defaultImpl = A.class)
@JsonSubTypes({
        @JsonSubTypes.Type(name = "B", value = B.class)
})
class A {
    String type;
}

// The subclass. I expect this to be serialized with a type="B" property
class B extends class A {
    List<Something> list;
}

// A (pagination) container that I use
class GenericContainer<T> {
    List<T> content;
}

// required to avoid type erasure
class ContainerOfB extends GenericContainer<B>

当我序列化这样的东西时

new ContainerOfB(new B())

输出看起来像这样

{
   "content" : [
        {
            // Does not contain the type property
            "list": [...]
        }
    ]
}

输出中不存在type属性

我不确定我在这里缺少什么?

0 个答案:

没有答案