简体   繁体   English

@JsonTypeInfo 和 @JsonSubTypes 在 jackson 中的用途是什么

[英]What are @JsonTypeInfo and @JsonSubTypes used for in jackson

What are the @JsonTypeInfo and @JsonSubTypes annotations used for in Jackson? Jackson 中使用的@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 is that it additionally preserves the concrete type of object being serialised along with the actual data.我的理解是,它还保留了与实际数据一起序列化的具体对象类型。

What is not clear to me is what is the actual advantage/gain during deserialization.不清楚的是反序列化过程中的实际优势/收益是什么。

Not getting any significant documentation apart from the java docs.除了 java 文档之外,没有获得任何重要的文档。 Can anyone please help out here or provide some docs around the same.任何人都可以在这里提供帮助或提供一些相同的文档。

The purpose of these annotations is to support polymorphism on deserialization.这些注释的目的是支持反序列化的多态性 When deserializing the actual code being executed will know the class of what it expects.当反序列化正在执行的实际代码时,将知道它所期望的 Eg, the type of some field being deserialized into.例如,被反序列化成的某个字段的类型。 But if that class has subclasses (ie, subtypes) how does the generic Jackson deserializer know which actual class the string being deserialized is?但是,如果该类具有子类(即子类型),通用 Jackson 反序列化器如何知道被反序列化的字符串是哪个实际类? 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 written into the serialization in the first place.它可以知道要创建哪个的唯一方法是该信息是否首先写入序列化。

As this answer says 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是其中一种方式 - 当您在编译时知道相关类可能存在的所有可能的子类型时,它会非常有效。

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

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