具有杰克逊序列化/反序列化的多级多态树

时间:2014-05-21 22:59:04

标签: java json inheritance serialization jackson

是否可以使用jackson对复杂的继承结构进行序列化和反序列化?它的注释是什么?例如,如果我有以下类

@Inheritance
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
     @Type(value = Dog.class,name = "dog")
     @Type(value = Cat.class,name = "cat")
})

public class Animal implements Serializable{

@id
long id;

String name;

}

这当然是父类。我知道如果猫和狗没有任何继承类,这是正确的。如果我想要狗的子类,我需要在动物类和狗类中改变什么?

这是第二个仅供参考的课程

@JsonTypeName("dog")
public class Dog extends Animal implements Serializable{
    //all my props etc here
{

我如何制作一个猎犬类和一个yorki类,它继承了动物和狗,我可以投射到任何一个并且让杰克逊不会对我不感兴趣。

1 个答案:

答案 0 :(得分:3)

多级多态树不应该是Jackson的问题。以下是序列化/反序列化类层次结构的示例,类似于您在the Jackson wiki page.中所提出的问题

public class JacksonPolymorphism3 {

    @JsonTypeInfo(
            use = JsonTypeInfo.Id.NAME,
            include = JsonTypeInfo.As.PROPERTY,
            property = "type")
    public abstract static class Animal {
        public final String animalField;

        @JsonCreator
        public Animal(@JsonProperty("animalField") String animalField) {
            this.animalField = animalField;
        }
    }

    @JsonTypeName("dog")
    public static class Dog extends Animal {
        public final String dogField;

        public Dog(@JsonProperty("animalField") String animalField,
                   @JsonProperty("dogField") String dogField) {
            super(animalField);
            this.dogField = dogField;
        }

        @Override
        public String toString() {
            return "Dog{" +
                    "dogField='" + dogField + '\'' +
                    '}';
        }
    }

    @JsonTypeName("husky")
    public static class Husky extends Dog {
       public final String huskyField;

        public Husky(@JsonProperty("animalField") String animalField,
                     @JsonProperty("dogField") String dogField,
                     @JsonProperty("huskyField") String huskyField) {
            super(animalField, dogField);
            this.huskyField = huskyField;
        }

        @Override
        public String toString() {
            return "Husky{" +
                    "huskyField='" + huskyField + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) throws IOException {
        List<Dog> dogs;
        dogs = Arrays.asList(new Dog("aField", "dogField"), new Husky("hField", "dField2", "hField"));

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerSubtypes(Dog.class, Husky.class);
        TypeReference<List<Dog>> referenceType = new TypeReference<List<Dog>>() {
        };
        String json = mapper.writerWithDefaultPrettyPrinter().withType(referenceType).writeValueAsString(dogs);
        System.out.println(json);
        System.out.println(mapper.readValue(json, referenceType));
    }
}

输出:

[ {
  "type" : "dog",
  "animalField" : "aField",
  "dogField" : "dogField"
}, {
  "type" : "husky",
  "animalField" : "hField",
  "dogField" : "dField2",
  "huskyField" : "hField"
} ]
[Dog{dogField='dogField'}, Husky{huskyField='hField'}]

如果它没有帮助,请提供更多代码。