Deduction deserialization - add support for pojo-hierarchies such that the absence of child fields infers a supertype
See original GitHub issueCurrently, deduction deserialization requires all values to have unique field. Because of that, there is no way to deserialize values to superclass and subclasses, when defaultImpl
is different than superclass.
Example:
For json:
"name": "Italy",
"places": {
"mountains": {
"name": "The Alps"
},
"cites": [
{
"name": "Rome",
"places": {
"colosseum": {
"name": "The Colosseum"
},
"romanForum": {
"name": "The Roman Forum"
}
}
},
{
"name": "Venice",
"places": {
"bridges": []
}
}
]
}
}
and pojo-hierarchy:
public static class Place implements WorthSeeing {
public String name;
}
public static class CompositePlace extends Place implements WorthSeeing {
public Map<String, WorthSeeing> places;
}
static class ListOfPlaces extends ArrayList<WorthSeeing> implements WorthSeeing {
}
and deduction deserialization with defaultImpl
differ then supertype:
@JsonTypeInfo(use = DEDUCTION, defaultImpl = ListOfPlaces.class)
@JsonSubTypes( {@Type(ListOfPlaces.class), @Type(CompositePlace.class), @Type(Place.class)})
interface WorthSeeing {}
the MismatchedInputException
is thrown, because the values with only name
fields are not classified to Place
.
Desired behaviour:
- value contains only fields from superclass -> it’s mapped to an object of superclass
- value contains fields from superclass and a subclass -> it’s mapped to an object of the supclass
- value contains fields from superclass and fields that don’t exist in any of subclasses -> the
UnrecognizedPropertyException
is thrown
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Deserializing polymorphic types with Jackson based on the ...
Such an approach is presented in this article. Update. Jackson 2.12.0 gets Polymorphic subtype deduction from available fields which adds @JsonTypeInfo(use = ...
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 >Jackson-js: Powerful JavaScript decorators to serialize ...
it supports more advanced Object concepts such as polymorphism and Object identity;; it supports cyclic object serialization/deserialization;; it supports ...
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. Let's ......
Read more >Security update for jackson-databind, jackson-dataformats ...
bindItem()' for same mapping + Add 'namespace' property for ... + Jackson does not support deserializing new Java 9 unmodifiable collections ...
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
Thankyou for thinking through the scenario and code. Honouring
required=true
is a good addition to the deduction process and goes onto my list.+1 would love to see this in the next version, can’t use the deduction now because the supertypes don’t get deducted