-
Notifications
You must be signed in to change notification settings - Fork 615
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
Polymorphic serializer, ignore unregistered objects when deserializing list of polymorphic objects #448
Comments
I think this feature is more about to skip deserialization of element in the list if an error occured. Because polymorphic serializer does not know whether it is in the list now or not, and it has to return something (e.g. if the same error occurs when deserializing the |
Yeah, that sounds right. maybe something along the lines of:
So in nonStrict mode at least, instead of throwing My suggestion sounds pretty hacky though. But might be something to consider until support for skipping malformed element can be added. |
Hi— do you know if that's something that is possible at the moment? Or a planned feature? It sounds like a good solution for my use case. |
I'd like to +1 this issue / feature request, as I am running into a very similar situation. In my particular use case, I am working with a common interchange format for a sports event hosted at the association's website. There is a list of competitors and the association supplies basic data (name, nationality). There is an option to include For example, one application may use this to store T-Shirt sizes. Another application may need to register whether the person is left- or right-handed etc. When they store that kind of information, it automatically becomes available in the general API for the competitors' list. I want to be able to write and parse (hence the |
@suushiemaniac I would say that the best option for your case is to create your own version of the Json format. The format is perfectly able to see that polymorphic serialization is needed and that no data type is needed. More importantly it may be able to use its own private way of determining the data type in the first place. |
@pdvrieze Thanks for the suggestion! Can you please specify what you mean by:
Unfortunately I don't have any control over the JSON specification used by the competition association website. The only API that I have access to yields a result like this (abridged): {
"eventName": "Super Serialize Competition",
"competitors": [
{
"name": "John Doe",
"nationality": "Nepal",
"extensions": [
{
"id": "polymorphicFoo",
"data": {
"yes": true,
"no": false
}
},
{
"id": "polymorphicBar",
"data": [
"one",
"two",
"three"
]
},
{
"id": "polymorphicMyStuff",
"data": {
"favoriteMusic": "Jazz",
"likesLasagna": true
}
}
]
},
{
"name": "Jane Doe",
"nationality": "Canada",
"extensions": [...]
}
]
} Here, only I would really prefer if there was a skip unknown polymorphic entity feature (that could be enabled/disabled by config) as suggested above. |
What I meant with format is basically an encoder/decoder pair. Looking at your data, you might however be able to get away with using an alternative (de)serializer instead, either for the list or the elements. |
Thanks for the explanation! I think I get your idea, but I believe it is a much larger effort than simply extending the library to allow for passing a Effectively right now I have to copy/paste your entire |
The trouble (also with
This custom serializer for elements could be given as much knowledge about the data as desired. You could also, in the serializer special case the json (or any) format and handle it differently - for example by first parsing the data to When understanding the serialization library, it is key to remember that fundamentally it is format independent. Because of that it cannot assume much about formats. For example, for some (binary) formats it is not possible to "skip" an unknown element as there is no size or end marker. The format (json in this case) could however have a mode where missing keys (or even missing polymorphic values) are ignored - or fed to a "handler" that allows the user to specify the behaviour. |
I definitely see your point here about the library being format-agnostic, @pdvrieze. For any other user who stumbles upon this issue, I also found the conversation in #514 to be very insightful. However, it still seems like a desirable change to allow for custom (dynamic!) hooks being invoked via Ultimately, with the current state of the project it seems like the best option to write a custom serializer. While that does achieve my goal, I still insist that it feels wrong to (effectively) copy so much code with so few changes to get what I'm looking for. Thanks for the constructive discussion! :) |
* As explained in #697, it appears that concatenating two modules as "behaviours" (as opposed to "bag of serializers") is way too dangerous for the end users and implies a lot of inconsistent behaviours and performance hits. So the most rational solution here is to prohibit such concatenations by closing the way to implement your own SerialModule * In order to still solve "fallback serializer" use-case, new 'default' DSL is introduced along with its support in PolymorphicSerializer Fixes #697 Fixes #448
* As explained in #697, it appears that concatenating two modules as "behaviours" (as opposed to "bag of serializers") is way too dangerous for the end users and implies a lot of inconsistent behaviours and performance hits. So the most rational solution here is to prohibit such concatenations by closing the way to implement your own SerialModule * In order to still solve "fallback serializer" use-case, new 'default' DSL is introduced along with its support in PolymorphicSerializer Fixes #697 Fixes #448
* As explained in #697, it appears that concatenating two modules as "behaviours" (as opposed to "bag of serializers") is way too dangerous for the end users and implies a lot of inconsistent behaviours and performance hits. So the most rational solution here is to prohibit such concatenations by closing the way to implement your own SerialModule * In order to still solve "fallback serializer" use-case, new 'default' DSL is introduced along with its support in PolymorphicSerializer Fixes #697 Fixes #448
* As explained in #697, it appears that concatenating two modules as "behaviours" (as opposed to "bag of serializers") is way too dangerous for the end users and implies a lot of inconsistent behaviours and performance hits. So the most rational solution here is to prohibit such concatenations by closing the way to implement your own SerialModule * In order to still solve "fallback serializer" use-case, new 'default' DSL is introduced along with its support in PolymorphicSerializer Fixes #697 Fixes #448
We solved this issue by adding a wrapper around polymorphic types:
using this serializer:
and we defined wrapping/unwrapping extension functions for ease of use:
This does work, all though of course it would be great if the library could perform this magic for us. |
I know this issue is pretty old by now, however, I just stumbled upon it since I had the same problem. We can register a default serializer for polymorphic types that will be used as a fallback. The downside compared to the wrapper solution is that we must register the serializer for every new polymorphic type we add. So it requires some discipline. val json = Json {
serializersModule += SerializersModule {
polymorphic(MyPolymorphicType::class) {
defaultDeserializer { MyPolymorphicType.Unknown.serializer() }
}
}
} |
I have made a serializer class for the json:api specificaiton.
included
can return many different types of objects which I am successfully using the polymorphic feature to deserialize. But this only works if all possible object types are registered in the scope of class JsonApiObject for polymorphic serialization.If any types are not registered an error like is thrown
Is it possible to ignore deserializing polymorphic type objects which are not registered for polymorphic serialization without throwing an error? This would really be helpful because it would prevent deserialization from throwing errors if any new objects added to the
included
list... and it would be convenient if don't actually need to deserialize some of the included objectsThe text was updated successfully, but these errors were encountered: