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

http://wiki.fasterxml.com/JacksonPolymorphicDeserialization

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() {
    }
}


你可能感兴趣的:(Jackson多态类型数据的反序列化)