简体   繁体   English

使用jackson对JSON进行多态反序列化,属性类型变为“null”

[英]polymorphic deserialization of JSON with jackson, property type becomes “null”

I am using Jackson to convert a big Json string into various classes and subclasses. 我正在使用Jackson将一个大的Json字符串转换为各种类和子类。

I have a list of objects, each containing a node object, a last result object and a children array. 我有一个对象列表,每个对象包含一个节点对象,一个最后的结果对象和一个子数组。 The children array contains a list of objects with exactly the same setup. children数组包含具有完全相同设置的对象列表。 This goes on for 3 or 4 layers. 这持续3或4层。

Each layers' node is of a different subclass, which all extend from a node superclass. 每个层的节点都是不同的子类,它们都是从节点超类扩展而来的。 I have annotated the superclass node with following annotations: 我用以下注释注释了超类节点:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Type1ResponseDto.class, name = "Type1"),
    @JsonSubTypes.Type(value = Type2ResponseDto.class, name = "Type2"),
    @JsonSubTypes.Type(value = Type3ResponseDto.class, name = "Type3"),
    @JsonSubTypes.Type(value = Type4ResponseDto.class, name = "Type4"),
    @JsonSubTypes.Type(value = Type5ResponseDto.class, name = "Type5")
})

This seems to work, since all subclasses get mapped. 这似乎有效,因为所有子类都被映射。

However, this somehow results in the "type" property being set to null. 但是,这会以某种方式导致“type”属性设置为null。

Any ideas as to why this happens? 关于为什么会这样的想法?

I needed to add visible=true for the type property to show up: 我需要为要显示的type属性添加visible=true

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type",visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = Type1ResponseDto.class,name =  "Type1"),
    @JsonSubTypes.Type(value = Type2ResponseDto.class, name = "Type2"),
    @JsonSubTypes.Type(value = Type3ResponseDto.class, name = "Type3"),
    @JsonSubTypes.Type(value = Type4ResponseDto.class, name = "Type4")
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM