Add SubType information at runtime using Jackson for polymorphism

强颜欢笑 提交于 2019-12-10 12:53:49

问题


I am using Jackson to unmarshal polymorphic types from JSON. I am using the @JsonTypeInfo, @JsonSubTypes, and @JsonTypeName annotations similar to Example 4 in this post. My question is, say now I need someone else to extend my code and add a 3rd class: public class Duck extends Animal outside of the original code base. How can I (or the others) add the SubType information without modifying the source code (annotation) of the public abstract Animal class?

UPDATE:

I am forced to use @JsonTypeName to solve the POJO version changes. For example:

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 {
    ...
}

// in another java package/project
package another.zoo;
import my.zoo.*;

@JsonTypeName("my.zoo.dog@2.0")
public class Dogv2 extends Animal {
    ...
}

Now the problem I am facing is that I cannot unmarshal a JSON that has type name "my.zoo.dog@2.0" without adding @Type(value = another.zoo.Dogv2.class, name = "my.zoo.Dog@2.0")}) to the Animal class. So doing this with annotation is obviously not possible. Is there a way to do this at runtime?

UPDATE 2:

I just found this SO question with the same/similar use case. My concern here is that using annotation will prevent people from extending/implementing your base class/interface. I would like a way to still keep extensibility of my base class/interface and make sure my (un)marshalling logic will work with future concrete types.


回答1:


I ended up using Reflections library to find all subtype of Animal class, and register JsonSubTypes with mapper.registerSubtypes(Class<?>... classes) method.




回答2:


Don't use @JsonSubTypes. Use @JsonTypeInfo#use with JsonTypeInfo.Id.CLASS to identify types for serialization and deserialization.

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
abstract class Animal { /* whatever */ }

Jackson will then store the fully qualified name to serialize the object and use it again to determine a target class for deserialization.

You'll have to make sure none of the subclasses have a property named type.


It's possible to make this work with @JsonSubTypes but it involves mixins and is not a very maintainable solution.



来源:https://stackoverflow.com/questions/34079050/add-subtype-information-at-runtime-using-jackson-for-polymorphism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!