I use Jackson to decouple polymorphic types from JSON. I use @JsonTypeInfo , @JsonSubTypes and @JsonTypeName , similar to Example 4, in this post . My question is: tell me, now I need someone else to extend my code and add a third class: public class Duck extends Animal outside the source code base. How can I (or others) add SubType information without changing the source code (annotation) of the public abstract Animal class ?
UPDATE:
I am forced to use @JsonTypeName to resolve POJO version changes. For instance:
package my.zoo; @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") @JsonSubTypes({ @Type(value = Cat.class, name = " my.zoo.cat@1.0 "), @Type(value = Dog.class, name = " my.zoo.dog@1.0 "), @Type(value = Catv2.class, name = " my.zoo.cat@2.0 ")}) public abstract class Animal { ... } @JsonTypeName(" my.zoo.cat@1.0 ") public class Cat extends Animal { ... } @JsonTypeName(" my.zoo.cat@2.0 ") public class Catv2 extends Animal { ... } @JsonTypeName(" my.zoo.dog@1.0 ") public class Dog extends Animal { ... }
Now the problem that I am facing is that I cannot untie JSON with a name like " my.zoo.dog@2.0 " without adding @Type(value = another.zoo.Dogv2.class, name = " my.zoo.Dog@2.0 ")}) to the Animal class. Therefore, it is obviously impossible to do this with the annotation. Is there any way to do this at runtime?
UPDATE 2:
I just found this SO question with the same / similar use case. My concern is that using annotation will not allow people to extend / implement your base class / interface. I would like to still maintain the extensibility of my base class / interface and make sure that my (un) marshalling logic works with future specific types.
source share