-
Notifications
You must be signed in to change notification settings - Fork 641
Allow error handling intervention in sealed class serializer #2841
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
Comments
As it stands now, the generated |
I think it's the same as #2721 / #1865 However, please note that we officially do not support catching exceptions in decoders — see 'Exception safety' section here: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.encoding/-decoder/. This is because state of decoder after throwing exception is undefined. |
So, I did find a provisional answer to this, which is to introduce an extra layer into object inheritance. This means we can override a higher level of parsing, and we can use
However I also note the comment above about exception safety and we will need to explore whether this is too much of a liability in our typical circumstances. |
What is your use-case and why do you need this feature?
We have sealed classes/interfaces, and we use the in-built polymorphism support, which means we don't have to do anything specific about declaring polymorphic types - the parser implementation follows the code. This is very valuable to us.
We have a resiliency requirement whereby we want to catch exceptions when parsing any one of these subtypes, and return either null or some default type (either is acceptable). This supports a business use case in a plugin architecture context where datamodel-incompatible objects within a particular context should be tolerable and ignored.
Currently this is technically possible, but it seems we have to sacrifice some or all of the benefits of seamless
sealed
support.We have explored a lot of possibilities, and the most viable one we have discovered is as follows.
We can write a 'null deserializer' wrapper function:
(consider that this could return a sealed class error type instance instead of null if necessary)
and then we can apply it to every type:
However, we incur various costs here:
@Polymorphic
annotations and make sure we add them comprehensivelysubclass()
call for their new typeDescribe the solution you'd like
We would like to be able to override or otherwise intervene in the plugin-generated polymorphic serializer for our sealed types.
This could possibly be achieved in a variety of ways:
@KeepGeneratedSerializer
support for this type - currently it is "not allowed on classes involved in polymorphic serialization: interfaces, sealed classes"polymorphicDefaultDeserializer()
configurationSealedClassSerializer
whilst leveraging as much as possible of the plugin-generated treeI don't know which of these, if any, might be most aligned to the roadmap.
Thanks for reading - advice welcome!
The text was updated successfully, but these errors were encountered: