Thread
#serialization
    Paul Woitaschek

    Paul Woitaschek

    2 years ago
    What's the status on null values in protobuf right now? If I get it correctly protobuf 3 is capable of null values?
    gildor

    gildor

    2 years ago
    Isn’t all values in protobuf are essentially nullable (the only way to support full backward compatibility)?
    they can be non-nullable only if have default value
    Paul Woitaschek

    Paul Woitaschek

    2 years ago
    Well it seems null is not supported at all:
    @Serializable
    data class Test(
        @ProtoId(1)
        val type: String?,
        @ProtoId(2)
        val userId: String
    )
    
    fun main() {
        ProtoBuf.dump(Test.serializer(), Test(null, "b"))
    }
    This immediately throws an exception:
    at kotlinx.serialization.internal.TaggedEncoder.encodeTaggedNull(Tagged.kt:37)
    	at kotlinx.serialization.internal.TaggedEncoder.encodeNull(Tagged.kt:71)
    	at kotlinx.serialization.Encoder$DefaultImpls.encodeNullableSerializableValue(Encoding.kt:260)
    	at kotlinx.serialization.internal.TaggedEncoder.encodeNullableSerializableValue(Tagged.kt:22)
    	at kotlinx.serialization.internal.TaggedEncoder.encodeNullableSerializableElement(Tagged.kt:124)
    	at Test.write$Self(MessagePoly.kt)
    gildor

    gildor

    2 years ago
    Is it Proto2 or 3?
    Paul Woitaschek

    Paul Woitaschek

    2 years ago
    It's just the serialization library?
    gildor

    gildor

    2 years ago
    What do you mean? Null is supported in general
    I clearly see proto annotation on data class
    So you serialize/desereali3 it into Proto2?
    Paul Woitaschek

    Paul Woitaschek

    2 years ago
    Don't get it. How would I specify that?
    gildor

    gildor

    2 years ago
    As I know officially it supports Proto2 only
    It proto3 may work, just not sure
    Could you show whole stacktrace
    Paul Woitaschek

    Paul Woitaschek

    2 years ago
    Sure, but its completely self contained so you can just run it yourself:
    Exception in thread "main" kotlinx.serialization.SerializationException: null is not supported
    	at kotlinx.serialization.internal.TaggedEncoder.encodeTaggedNull(Tagged.kt:37)
    	at kotlinx.serialization.internal.TaggedEncoder.encodeNull(Tagged.kt:71)
    	at kotlinx.serialization.Encoder$DefaultImpls.encodeNullableSerializableValue(Encoding.kt:260)
    	at kotlinx.serialization.internal.TaggedEncoder.encodeNullableSerializableValue(Tagged.kt:22)
    	at kotlinx.serialization.internal.TaggedEncoder.encodeNullableSerializableElement(Tagged.kt:124)
    	at Test.write$Self(protobug.kt)
    	at Test$$serializer.serialize(protobug.kt)
    	at Test$$serializer.serialize(protobug.kt:9)
    	at kotlinx.serialization.protobuf.ProtoBuf$ProtobufWriter.encodeSerializableValue(ProtoBuf.kt:148)
    	at kotlinx.serialization.EncodingKt.encode(Encoding.kt:402)
    	at kotlinx.serialization.protobuf.ProtoBuf.dump(ProtoBuf.kt:541)
    	at kotlinx.serialization.protobuf.ProtoBuf$Default.dump(ProtoBuf.kt)
    	at ProtobugKt.main(protobug.kt:14)
    	at ProtobugKt.main(protobug.kt)
    gildor

    gildor

    2 years ago
    You should enable nulls:
    ProtoBuf(encodeDefaults = false)
    Essentially protobuf doesn’t have concept of null, only optional with default value, so this flag just skip serialization of this null value