Java @JsonTypeInfo 和 @JsonSubTypes 在 jackson 中的用途是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45447276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are @JsonTypeInfo and @JsonSubTypes used for in Hymanson
提问by Harshit
What are @JsonTypeInfo and @JsonSubTypes annotations using for in Hymanson ?
在 Hymanson 中使用的 @JsonTypeInfo 和 @JsonSubTypes 注释是什么?
public class Lion extends Animal {
private String name;
@JsonCreator
public Lion(@JsonProperty("name") String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getSound() {
return "Roar";
}
public String getType() {
return "carnivorous";
}
public boolean isEndangered() {
return true;
}
@Override
public String toString() {
return "Lion [name=" + name + ", getName()=" + getName() + ", getSound()=" + getSound() + ", getType()=" + getType() + ", isEndangered()="
+ isEndangered() + "]";
}
}
========================================
========================================
public class Elephant extends Animal {
@JsonProperty
private String name;
@JsonCreator
public Elephant(@JsonProperty("name") String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getSound() {
return "trumpet";
}
public String getType() {
return "herbivorous";
}
public boolean isEndangered() {
return false;
}
@Override
public String toString() {
return "Elephant [name=" + name + ", getName()=" + getName() + ", getSound()=" + getSound() + ", getType()=" + getType()
+ ", isEndangered()=" + isEndangered() + "]";
}
}
==============================================
==============================================
@JsonTypeInfo (use = JsonTypeInfo.Id.CLASS, include = As.PROPERTY, property = "classNameExtenral")
@JsonSubTypes ({@Type (value = Lion.class, name = "lion"), @Type (value = Elephant.class, name = "elephant")})
public abstract class Animal {
@JsonProperty ("name")
String name;
@JsonProperty ("sound")
String sound;
@JsonProperty ("type")
String type;
@JsonProperty ("endangered")
boolean endangered;
}
public static void main(String[] args){
Lion lion = new Lion("Simba");
Elephant elephant = new Elephant("Manny");
List<Animal> animals = new ArrayList<>();
animals.add(lion);
animals.add(elephant);
}
What I understand isthat it additionally preserves the concrete type of object being serialised along with the actual data.
我的理解是,它还保留了与实际数据一起序列化的具体对象类型。
What is not clear to meis what is the actual advantage/gain during deserialization.
我不清楚的是反序列化过程中的实际优势/收益是什么。
Not getting any significant documentation apart from the java docs. Can any1 please help out here or provide some docs around the same.
除了 java 文档之外,没有获得任何重要的文档。任何人都可以在这里提供帮助或提供一些相同的文档。
采纳答案by davidbak
The purpose of these annotations is to support polymorphismon deserialization. When deserializing the actual code being executed will know the classof what it expects. E.g., the type of some field being deserialized into. But if that class has subclasses (i.e., subtypes) how does the generic Hymanson deserializer know which actual class the string being deserialized is? It's got to create an instance of some concrete type (the class or one of its subclasses) and fill it up. The only way it can know which one to create is if that information is writteninto the serialization in the first place.
这些注释的目的是支持反序列化的多态性。当反序列化正在执行的实际代码时,将知道它所期望的类。例如,被反序列化成的某个字段的类型。但是,如果该类具有子类(即子类型),通用 Hymanson 反序列化器如何知道被反序列化的字符串是哪个实际类?它必须创建某个具体类型(类或其子类之一)的实例并填充它。它可以知道要创建哪个的唯一方法是该信息是否首先写入序列化。
As this answersays there are three ways to do it - you pick the one that's appropriate for your use case. @JsonTypeInfo
+ @JsonSubtypes
is one of those ways - it works great when you know, at compile time, all of the possible subtypes that could exist for the class in question.
正如这个答案所说,有三种方法可以做到 - 您可以选择适合您的用例的方法。 @JsonTypeInfo
+@JsonSubtypes
是其中一种方式 - 当您在编译时知道相关类可能存在的所有可能子类型时,它会非常有效。