Deserialization of a polymorphic type based on presence of a property
See original GitHub issueI’d like a feature on jackson for deserialization of polymorphic type where type selection strategy should be based on presence of a property. I know this was mentioned before but there was no consensus about how it should be solved.
Example:
For jsons like:
{'foo':''}
{'bar':''}
and type definitions like:
@JsonSubTypes({
@JsonSubTypes.Type(value=Foo.class, name="foo"),
@JsonSubTypes.Type(value=Bar.class, name="bar"),
})
interface FooBar {
}
@Value
class Foo {
private final String foo;
}
@Value
class Bar {
private final String bar;
}
When I deserialise FooBar with {'foo':''}
I should get an instance of a Foo and similar for {'bar':''}
I should get an instance of a Bar.
I have a working prototype where you have to specify a new custom deserialiser like:
public class FooBarDeserialiser extends PresentPropertyPolymorphicDeserialiser<FooBar> {
public FooBarDeserialiser() {
super(FooBar.class);
}
}
and this is a valid approach, but a better one, in my opinion, would be if this had first class support for example with JsonTypeInfo:
@JsonTypeInfo(use = JsonTypeInfo.Id.PROPERTY_NAME) // or PROPERTY_NAME_PRESENCE
@JsonSubTypes({
@JsonSubTypes.Type(value=Foo.class, name="foo"),
@JsonSubTypes.Type(value=Bar.class, name="bar"),
})
interface FooBar {
}
Thoughts?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:36
- Comments:19 (12 by maintainers)
Top Results From Across the Web
Deserializing polymorphic types with Jackson based on the ...
Here's a solution I've come up with that expands a bit on Erik Gillespie's. It does exactly what you asked for and it...
Read more >Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson to figure out the right type during deserialization.
Read more >Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >How to serialize properties of derived classes with System ...
For polymorphic serialization to work, the type of the serialized value should be that of the polymorphic base type. This includes using the ......
Read more >Jackson 2.12 Most Wanted (1/5) - cowtowncoder - Medium
Deduction-Based Polymorphism ... The oldest open feature request jackson-databind#43 — filed in 2012 — was to support polymorphic types that do not have...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Upvoted. This would be extremely useful when you:
Yes I agree, but the functionality should be added in some form to the jackson itself, something like
@JsonTypeInfo(use=Id.PRESENCE, include=As.PROPERTY)
or something like that.