2016-09-27 61 views
3

我翻遍了每行代碼,但我認爲這是傑克遜如何處理多態性內部。傑克遜的@JsonTypeInfo(use = Id.CUSTOM,include = As.PROPERTY,property =「type」)讀取除「type」之外的JSON的所有字段

使用的DogCat延伸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_decibelmeow_level從JSON反序列化,但type作爲null越來越英寸有任何想法嗎?

回答

2

設置visible=true@JsonTypeInfo

@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type", visible=true) 

參考this post