Kotlinx.serialization: NULLs are not supported when writing to protobuf

Created on 24 Jan 2018  路  4Comments  路  Source: Kotlin/kotlinx.serialization

https://github.com/Kotlin/kotlinx.serialization/issues/58#issuecomment-359719900

@Serializable class SampleMessage {
    @Optional @SerialId(1) var name: String? = null
}

fun main(args: Array<String>) {
    val message = SampleMessage()
    println(ProtoBuf.dumps(message))
}

crashes with

Exception in thread "main" kotlinx.serialization.SerializationException: null is not supported
    at kotlinx.serialization.TaggedOutput.writeTaggedNull(Tagged.kt:38)
    at kotlinx.serialization.TaggedOutput.writeNullValue(Tagged.kt:80)
    at kotlinx.serialization.KOutput.writeNullableSerializableValue(Serialization.kt:151)
    at kotlinx.serialization.KOutput.writeNullableSerializableElementValue(Serialization.kt:198)
    at SampleMessage.write$Self(TestOneOf.kt)
    at SampleMessage$$serializer.save(TestOneOf.kt)
    at SampleMessage$$serializer.save(TestOneOf.kt:5)
    at kotlinx.serialization.KOutput.write(Serialization.kt:99)
    at kotlinx.serialization.protobuf.ProtoBuf.dump(ProtoBuf.kt:415)
    at TestOneOfKt.main(TestOneOf.kt:17)
bug design

Most helpful comment

Any news on this issue? Unfortunately, this bug makes Protobuf serialization unusable at the moment.

All 4 comments

Any news on this issue? Unfortunately, this bug makes Protobuf serialization unusable at the moment.

@wem It took longer than I though. I think it would be resolved alongside with #58 , but with one limitation: protobuf doesn't have a concept of null anyway, so the single way to record them is to just skip them in output stream. But when you have an optional value which default is not a null value, you can't really say was it missing from the stream because it had default value or because it was null, so such declaration will likely be prohibited by protobuf.

I propose an easy fix to this issue to unblock people willing to use kotlinx.serialization for protobuf purposes: https://github.com/Kotlin/kotlinx.serialization/pull/588 . Currently this has been a blocker for any actual Protobuf usage for almost 2 years, I think this simple fix can be used temporary until a proper redesign is in place.

Was this page helpful?
0 / 5 - 0 ratings