kotlinx.serialization
kotlinx.serialization copied to clipboard
Serializer for Nothing
A KSerializer<Nothing?> should serve the same purposes that Nothing in type system: indicate that a particular item should never be serialized. There are several use-cases for it, mainly connected with generics:
https://github.com/Kotlin/kotlinx.serialization/issues/607
https://github.com/Kotlin/kotlinx.serialization/issues/393
https://github.com/Kotlin/kotlinx.serialization/issues/614
Describe the solution you'd like
A NothingSerializer that can be used in @Serializable(NothingSerializer) on types. Or NoSerializer serializer. Or @NoSerializer annotation as a shorthand.
Can this issue at least get some love?
It is fixed already, the only question is when it will be released. @shanshin knows for sure if it's 1.7.20 or 1.8.0
Target Kotlin release 1.8.0
Is this issue fixed on the JS target?
By running the code below on the JS target:
@Serializable
class TestData<T>(val value: T)
serializer<TestData<Nothing?>>()
Json.encodeToString(TestData(null))
I get:
UnsupportedOperationException: There's no native JS class for Nothing type
I see you even filed an issue about that: https://youtrack.jetbrains.com/issue/KT-51333/KJS-some-KType-equals-Nothings-KType-throws-an-exception-breaking-its-symmetry I think it should be fixed on Kotlin's side, because we have proper BUILTINS_SERIALIZERS map logic.