-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Type Id mappings where two ids map to same Class
#312
Comments
The critical point in Polymorphism is the deserialization. When I have an JsonValue and I want to convert it to a class, is important that the object is an instance of the desired one. This is a basic example. Only knowing the Type, provided by the enum should be enough to deserialize to the correct subclas:
In the serialization process I don't see the problem:
Will this deserialization be ambiguous? |
I don't see ambituity in that example, as all ids for given base type are unique. Critical point in round-trip handling may be serialization or deserialization, not just deserialization. Point being that if two types produce same id in serialization, deserialization will not be able to produce original types. However: I am not saying that ambiguity is always problematic; because it is not. There are use cases where Jackson is only used for one direction; so -- for example -- deserializing multiple ids into one type is perfectly fine, and even desireable. All of which is to say: yes, I hope to relax these limitations, to allow use of non-bijective mappings. |
The ids are unique, but they're mapping the same classes. Following the example, how would it be possible to deserialized the polymorphisc objects using jackson with annotations? I would do it using @JsonTypeInfo like this
Something like that should work, in case several values could be asigned to the same class. However, it only works for ZEBRA and PARROT (the first values asigned to clases). It should be possible to do it with those annotations. When is not, there is another solution (avoiding writting my own Serializator/Deserializator) ? Thanks |
@umbreak Your example will not work currently; but if this RFE was implemented, it would, for deserialization. |
For serialization animalType property should be checked in the class Animal. It contains the mapping information in any case. Only when you have an Animal class without any animalType there could be ambiguity.
|
@umbreak I don't think you are reading what I have written above. As I have repeatedly pointed out, mapping is not bijective which is WHY CURRENT IMPLEMENTATION WILL THROW AN EXCEPTION. And that further, with changes, IT WOULD START WORKING FOR DESERIALIZATION. |
Implemented for 2.6.0. |
It is still issue if you doing with |
@gimlet2 If there are follow up problems, could you please file a follow-up issue? It may reference this issue as background. I don't usually re-open old issues if fix in question has been published, as doing so complicates figuring out which version fix(es) go in. |
@gimlet2 Has this follow-up issue ever been created? We are running exactly into the issue that registerSubtypes(..) doesn't work for use with mapping multiple types to the same class while it worked with annotating the super class. The latter we don't want to do anymore as it is a generic class not supposed to know about subtypes (also see #2104). |
Currently default type id handlers do require mapping between ids and types (Classes) to be bijective; that is, neither side can have duplicates. This makes sense if both serialization and deserialization is required; if there are multiple choices, mapping would be ambiguous.
But for cases where Jackson is only used for deserializing, it may be useful to map more than one type id to a single class. It would be good if default type id serializer could support such usage; it is acceptable (and desirable) to throw an exception if such mapping was being used for serialization.
The text was updated successfully, but these errors were encountered: