-
Notifications
You must be signed in to change notification settings - Fork 645
Allow calling SerialDescriptor(name, original)
when original.kind
is PrimitiveKind
#2547
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
I'd be happy to implement this if it's accepted. |
Yes, I think this is logical and will make writing delegating serializers easier. I'll be happy to receive your PR! |
I stumbled accross the situation where a primitive descriptor is nullable: this won't be copied over to the wrapped serializer by the code shown above. So it isn't as easy as I thought it would be. |
You can use kotlinx.serialization/core/commonMain/src/kotlinx/serialization/descriptors/SerialDescriptors.kt Line 220 in 06aabd2
original.isNullable
|
and allow creating primitive descriptors via it as well. This constructor function is a way to create a descriptor for your custom serializer if it simply delegates to an existing one. Since it fits its purpose and is unlikely to change in the future, we can promote it to stable API alongside other descriptor builders. Fixes #2547
and allow creating primitive descriptors via it as well. This constructor function is a way to create a descriptor for your custom serializer if it simply delegates to an existing one. Since it fits its purpose and is unlikely to change in the future, we can promote it to stable API alongside other descriptor builders. Fixes #2547
and allow creating primitive descriptors via it as well. This constructor function is a way to create a descriptor for your custom serializer if it simply delegates to an existing one. Since it fits its purpose and is unlikely to change in the future, we can promote it to stable API alongside other descriptor builders. Fixes #2547
and allow creating primitive descriptors via it as well. This constructor function is a way to create a descriptor for your custom serializer if it simply delegates to an existing one. Since it fits its purpose and is unlikely to change in the future, we can promote it to stable API alongside other descriptor builders. Fixes #2547
and allow creating primitive descriptors via it as well. This constructor function is a way to create a descriptor for your custom serializer if it simply delegates to an existing one. Since it fits its purpose and is unlikely to change in the future, we can promote it to stable API alongside other descriptor builders. Fixes #2547
Thanks for implementing this @sandwwraith :) |
Uh oh!
There was an error while loading. Please reload this page.
What is your use-case and why do you need this feature?
This section of the Kotlin Serialization Guide recommends that delegating serializers should not reuse the descriptor of the serializer they are delegating to but instead wrap it with
SerialDescriptor("OtherName", delegate.descriptor)
. This works fine ifdelegate.descriptor.kind
is notPrimitiveKind
. But for serializers that have a primitive descriptor, this will throw an exception.This can cause problems if the delegated serializer just happens to have a primitive serial descriptor as an implementation detail.
An example for this is this serializer which delegates to
JsonPrimitive.serializer()
. However when writing this serializer, one shouldn't have to know what the serial descriptor forJsonPrimitive
looks like if all that one wants to do is delegate serialization toJsonPrimitive.serializer()
. Ideally you should always be able to callSerialDescriptor("OtherName", delegate.descriptor)
to implementKSerializer.descriptor
for a serializer that just delegates todelegate
.Describe the solution you'd like
Change the code for
SerialDescriptor()
to not throw when given a primitive descriptor. This could be done by simply delegating toPrimitiveSerialDescriptor()
:The text was updated successfully, but these errors were encountered: