C-Rad
C-Rad

Reputation: 355

multi level polymophic trees with jackson serialization/deserialization

is it possible to have a complex inheritance structure serialized and deserialized with jackson? what are the annotations for it? for example if I had the following classes

@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;

}

this of course is the parent class. I know this is correct if cat and dog do not have any inheriting classes. if I want subclasses of dogs what would I need to change in both the animal class and the dog class?

here is the second class just for reference

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

how would I make a retriever class and a yorki class that inherit from both animal and dog that I could cast to either one and have jackson not freak out at me.

Upvotes: 0

Views: 3167

Answers (1)

Alexey Gavrilov
Alexey Gavrilov

Reputation: 10853

Multi-level polymorphic tree should not be a problem with Jackson. Here is an example of the serializing / de-serializing class hierarchy similar to what you have in your question as it stands in 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));
    }
}

Output:

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

If it doesn't help please provide more code.

Upvotes: 4

Related Questions