Alan Cabrera
Alan Cabrera

Reputation: 732

Jackson support for polymorphism without annotations and dedicated bean fields

Is there a way to perform serialization/deserialization in Jackson of polymorphic classes w/out using annotations or specialized bean fields? I have to support class hierarchies that I cannot modify and don't wish to use annotations.

I'd like to be able to designate a synthetic name, which would not be in the classes that I am serializing/deserializing, that would be inserted into the JSON representation and used to identify the type.

Upvotes: 4

Views: 3238

Answers (2)

Matyas
Matyas

Reputation: 13712

For anyone looking for polymorphic json unmarshaling problems, you should check out this post, which gives great examples & workarounds for JSON serialization / deserialization caveats.

And if mixins (step 5 at the above mentioned post) is not what you're looking for than go for the accepted answer by StaxMan.

Upvotes: 0

StaxMan
StaxMan

Reputation: 116620

If mix-ins are not to your liking, there isn't anything pre-defined to pass, but you can relatively easily achieve this by sub-classing JacksonAnnotationIntrospector and configure mapper with it.

In your implementation you can override all aspects of annotation access: in your case it's probably enough to override findTypeResolver() (and if you want per-property overrides, 'findPropertyTypeResolver()'). The method can then use whatever mechanism you want to construct TypeResolverBuilder (most likely StdTypeResolverBuilder) that contains same information as what would usually come from @JsonTypeInfo annotation.

Upvotes: 3

Related Questions