不忽略继承映射中的 JSONTypeInfo 属性
JSONTypeInfo to not ignore property in inheritance mapping
我正在为 JSON serialization/deserialization
使用以下依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>
我有继承映射。
以下是Parent
class.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "isOne")
@JsonSubTypes({ @Type(value = One.class, name = "true"), @Type(value = Two.class, name = "false") })
public class Parent extends AbstractValueObject {
private Boolean isOne;
}
我们有两个子 class One
和 Two
由 Parent
扩展。
当我在 属性 isOne
的帮助下 serialize/deserialize 时,此映射有效。
但是 问题 是当 JSON 转换为 class jackson 删除 属性 isOne
。有没有办法不删除 属性。它不推荐任何虚拟 属性 只要我能有实际的。
根据 JsonTypeInfo 的文档,您可以使用 visible=true
试试这个:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "isOne", visible = true)
我正在为 JSON serialization/deserialization
使用以下依赖项<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>
我有继承映射。
以下是Parent
class.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "isOne")
@JsonSubTypes({ @Type(value = One.class, name = "true"), @Type(value = Two.class, name = "false") })
public class Parent extends AbstractValueObject {
private Boolean isOne;
}
我们有两个子 class One
和 Two
由 Parent
扩展。
当我在 属性 isOne
的帮助下 serialize/deserialize 时,此映射有效。
但是 问题 是当 JSON 转换为 class jackson 删除 属性 isOne
。有没有办法不删除 属性。它不推荐任何虚拟 属性 只要我能有实际的。
根据 JsonTypeInfo 的文档,您可以使用 visible=true
试试这个:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "isOne", visible = true)