kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Serializer for Nothing

Open sandwwraith opened this issue 5 years ago • 3 comments

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.

sandwwraith avatar Jul 24 '20 13:07 sandwwraith

Can this issue at least get some love?

andylamax avatar Aug 03 '22 23:08 andylamax

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

qwwdfsad avatar Aug 04 '22 08:08 qwwdfsad

Target Kotlin release 1.8.0

shanshin avatar Aug 05 '22 18:08 shanshin

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

ShreckYe avatar Jun 10 '23 08:06 ShreckYe

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.

sandwwraith avatar Jun 12 '23 10:06 sandwwraith