Search code examples
polymorphismjackson

Jackson deserialization of polymorphic types


I've seen an example of jackson deserialization @JsonTypeInfo, that is:

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

I've tried it and it works fine. Now, the problem is that in the example classes Cat and Dog are referenced from Animal, which I want to avoid. Is there a way to move type binding from class Animal and still have deserialization work? Thanks


Solution

  • I found an answer here: http://jira.codehaus.org/browse/JACKSON-654. So I could use:

    mapper.registerSubtypes(Cat.class, Dog.class);