3
我翻遍了每行代碼,但我認爲這是傑克遜如何處理多態性內部。傑克遜的@JsonTypeInfo(use = Id.CUSTOM,include = As.PROPERTY,property =「type」)讀取除「type」之外的JSON的所有字段
使用的Dog
和Cat
延伸Animal
典型的例子:
@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type")
@JsonTypeIdResolver(AnimalTypeIdResolver.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Animal implements Serializable {
public AnnotatorBundleConfig(String name) {
super();
this.name = name;
}
狗類:
public class DogAnimal extends Animal {
@JsonCreator
public DogAnimal(
@JsonProperty(value="name", required=true) String name,
@JsonProperty(value="bark_decibel") int bark_decibel)
{
super(name);
this.bark_decibel = bark_decibel;}
貓類:
public class CatAnimal extends Animal {
@JsonCreator
public CatAnimal(
@JsonProperty(value="name", required=true) String name,
@JsonProperty(value="meow_level") int meow_level)
{
super(name);
this.meow_level = meow_level;}
的AnimalTypeIdResolver
是一個典型的TypeIdResolver該範圍nds AbstractTypeIdResolver
。
一些非常奇怪的原因,bark_decibel
和meow_level
從JSON反序列化,但type
作爲null
越來越英寸有任何想法嗎?