- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 @JsonTypeInfo
指示 Jackson 在 @class
属性中查找具体类型信息。但是,有时我不想指定 @class
,特别是当可以根据上下文推断出子类型时。 最好的方法是什么?
这是一个 JSON 示例:
{
"owner": {"name":"Dave"},
"residents":[
{"@class":"jacksonquestion.Dog","breed":"Greyhound"},
{"@class":"jacksonquestion.Human","name":"Cheryl"},
{"@class":"jacksonquestion.Human","name":"Timothy"}
]
}
我正在尝试将它们反序列化为这些类(全部在 jacksonquestion.*
中):
public class Household {
private Human owner;
private List<Animal> residents;
public Human getOwner() { return owner; }
public void setOwner(Human owner) { this.owner = owner; }
public List<Animal> getResidents() { return residents; }
public void setResidents(List<Animal> residents) { this.residents = residents; }
}
public class Animal {}
public class Dog extends Animal {
private String breed;
public String getBreed() { return breed; }
public void setBreed(String breed) { this.breed = breed; }
}
public class Human extends Animal {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
使用这个配置:
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
private static class AnimalMixin {
}
//...
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getDeserializationConfig().addMixInAnnotations(Animal.class, AnimalMixin.class);
Household household = objectMapper.readValue(json, Household.class);
System.out.println(household);
如您所见,所有者被声明为人类,而不是动物,因此我希望能够省略 @class
并让 Jackson 像往常一样推断类型。
当我运行它时,我得到了
org.codehaus.jackson.map.JsonMappingException: Unexpected token (END_OBJECT),
expected FIELD_NAME: missing property '@class' that is to contain type id (for class jacksonquestion.Human)
因为“所有者”没有指定@class
。
有什么想法吗?我最初的一个想法是在属性而不是类型上使用 @JsonTypeInfo
。
但是,这不能用于注释列表的元素类型。
(不正确,请参阅答案)
最佳答案
原来我误解了Javadoc for @JsonTypeInfo
.当我在我的问题中说
One initial thought I had was to use @JsonTypeInfo on the property rather than the type. However, this cannot be leveraged to annotate the element type of a list.
我是基于 Javadoc 中的这段引述:
When used for properties (fields, methods), this annotation applies to values: so when applied to structure types (like Collection, Map, arrays), will apply to contained values, not the container; for non-structured types there is no difference. (...) There is no per-property way to force type information to be included for type of container (structured type); for container types one has to use annotation for type declaration.
不知何故,我误读了相反的意思;类型信息将应用于容器而不是元素。显然那是错误的。所以我能够使用以下方法解决此问题:
public class Household {
//...
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public void setResidents(List<Animal> residents) {
this.residents = residents;
}
}
现在只有在 residents
属性中指定的 Animals 才需要 @class
。
关于java - 使@JsonTypeInfo 属性可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905975/
当尝试将通用对象序列化/反序列化为特定类时,我遇到了以下问题: 代码: // Generic holding the payload data linked to this event @JsonTy
我正在使用 Jersey 编写 REST 服务。我有一个带有注释的抽象类 Promotion: @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) 因此,当我返回一个
所以我的 JSON 看起来像这样: { "ActivityDisplayModel" : { "name" : "lunch with friends", "s
我正在尝试使用 Jackson 2.9.8 序列化/反序列化多态类型,除非我将此类类型的对象放入集合中,否则它可以正常工作,因为由于某种原因,当时没有写入类型信息。让我们考虑以下示例: @JsonIg
使用 Spring 3 和 Jackson 1.7.6,我可以序列化抽象类的实现并将类的完全限定名称输出为名为 @class 的属性。 .当我的 Spring Controller 从用 @Respo
使用这些依赖 compile "com.fasterxml.jackson.core:jackson-annotations:2.8.8" compile "org.codehaus.jackson:
我正在使用以下依赖项进行 JSON 序列化/反序列化 com.fasterxml.jackson.core jackson-databind 2.7.4 com.
我正在尝试序列化和反序列化(使用 Json 和 ObjectMapper )在基类上具有以下注释的对象的多态列表: @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, i
我正在尝试使用 Jackson 来序列化具有多态性的实体。序列化的 JSON 字符串应该包含一个附加的“type”属性,其中“groupA”或“groupB”作为值,但它没有。我的实体如下所示: @E
friend 们, Jackson 框架提供基于注释的方法来在序列化过程中发出类型信息。 我不想使用 @JsonTypeInfo 我的任何类(class)中的注释。 以上注释是否有任何替代方案。 如果
我发现 Jackson JSON Processor 库有一些奇怪的行为,我很好奇这是故意的还是错误。请看下面的代码: @JsonTypeInfo(use = Id.NAME) public clas
我正在使用 @JsonTypeInfo 指示 Jackson 在 @class 属性中查找具体类型信息。但是,有时我不想指定 @class,特别是当可以根据上下文推断出子类型时。 最好的方法是什么?
我的父类(super class)型注释为 @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "_typeid") - 以便
我有这样的回应: { "id":"decaa828741611e58bcffeff819cdc9f", "statement":"question statement", "
我有类层次结构 A <- B <- C。我不希望类 A 知道 C,所以我打算使用 A.type="B"表示它是类 B,然后是 B.type2 ="C"表示它是 C 类。 @JsonTypeInfo(u
我的目标是使用 Jackson 将 JSON 字符串字段转换为正确的类。 我有以下类(class): public class AnimalRecord { private String id
我正在使用 JsonTypeInfo 来处理系统读入的一些 JSON 对象的多态性。系统还将这些对象提供给其他服务。在某些情况下,我想要包括类型信息的详细对象,而在其他情况下,我想要对象的准系统最小
我尝试在应用程序的客户端序列化一个对象(来自 Enum ),并使用 jackson 在另一端(服务器端)将其反序列化为同一对象。我的客户端有这个枚举: @JsonTypeInfo(use = Json
假设我有一些像这样的 JSON [ { 'type' : { 'value': 'B' } }, {
我遇到了一种情况,我需要自定义某些 JSON 的序列化/反序列化。我已将其简化为一个可读的示例。我有一个 Container 类,其中包含实现 MyInterface 的对象。在我的示例中,Class
我是一名优秀的程序员,十分优秀!