Inheritance - Deserialize a child from a parent representation #4054
Replies: 3 comments 1 reply
-
There may be a solution to your problem. So what you want is to deserialize a JSON object as a Dog object even though the JSON's type field indicates it is an Animal? (with `"type" : "animal") There is a new configuration feauture in Jackson 2.15, |
Beta Was this translation helpful? Give feedback.
-
May I ask for your opinion on the following, @cowtowncoder? If we:
would it make more sense to throw Here's an example with the above-mentioned classes: public void testConcrete() throws Exception {
String data = a2q("{'name': 'myAnimal','type': 'animal'}");
Dog d = JsonMapper.builder()
.disable(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES)
.build()
.readValue(data, Dog.class);
} |
Beta Was this translation helpful? Give feedback.
-
As to the original question: no, if type id given is
I am not dead set against improvements to error messages tho if someone wants to submit a PR. But I don't think handling itself should be changed or changeable in this case. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm working with the following case:
Given the following JSON:
I know it's a bit of a stretch use-case, but I would like to work at Dog level in the application reading this JSON.
Once deserialized, as the object built is an Animal, it can no longer be instantiated or casted as something else.
I'm wondering if it would be possible to deserialize it directly as a Dog, and not an Animal.
As the "type" = "animal", when trying to do so I'm getting:
And I can't change the "type" to be something else (it's generated by someone else).
Would there be a way to achieve this ?
Beta Was this translation helpful? Give feedback.
All reactions