当前位置:编程学习 > JAVA >>

Jackson多态类型数据的反序列化

json串中要有property中指明的字段,如@class,才可以解析成功
 
public class Zoo {  
    public Animal animal;  
}  
  
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")  
@JsonSubTypes({  
        @JsonSubTypes.Type(value = Dog.class, name = "Dog"), @JsonSubTypes.Type(value = Cat.class, name = "Cat")})  
static class Animal {  
    // All animals have names, for our demo purposes...   
    public String name;  
  
    protected Animal() {  
    }  
}  
  
static class Dog extends Animal {  
    public double barkVolume;  
  
    // in decibels   
    public Dog() {  
    }  
}  
  
static class Cat extends Animal {  
    boolean likesCream;  
    public int lives;  
  
    public Cat() {  
    }  
}  

 


补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,