question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialize Protobuf list of objects

See original GitHub issue

I started working on custom serializer since this: val protoSports = ProtoBuf.load(ProtoSports.serializer(), byteArrayRes) only works correctly on Android (iOS throws NotImplementedException due to lack of reflection).

ProtoSports has only one property: @SerialId(1) val sports: List<Sport>.

I’ve successfully deserialized ‘simpler’ objects with properties of type String, Long, etc. like this:

override fun deserialize(input: Decoder): Sport {
            val inp: CompositeDecoder = input.beginStructure(descriptor)
            var id: Long = 0
            lateinit var name: String
            loop@ while (true) {
                when (val i = inp.decodeElementIndex(descriptor)) {
                    CompositeDecoder.READ_DONE -> break@loop
                    0 -> id = inp.decodeLongElement(descriptor, i)
                    1 -> name = inp.decodeStringElement(descriptor, i)
                    else -> throw SerializationException("Unknown index $i")
                }
            }
            inp.endStructure(descriptor)
            return Sport(id, name)
        }

My question is, how can I deserialize list of objects e.g. Sport? I’ve tried like this, but it doesn’t work:

loop@ while (true) {
    when (val i = inp.decodeElementIndex(descriptor)) {
    0 -> {
        val sport = inp.decodeSerializableElement(descriptor, i, Sport.serializer())
        sports = sports.plus(sport)
    }
}

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sandwwraithcommented, Dec 18, 2018

I’ve finally figured this problem out, and it is not related to bytebuffer.

In the Native serialization plugin, there is no support for @SerialInfo annotations in the 1.3.11 versions, therefore protobuf ids were auto-assigned using property index: https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/native/src/main/kotlin/kotlinx/serialization/protobuf/ProtobufPlatform.kt#L22

Unfortunately, properties are enumerated from 0 and 0 is not a valid protobuf id, therefore externally produced bytes can’t be correctly parsed (fun fact, if bytes were produced by kotlinx.serialization itself, parser does not complain).

1.3.20 will have support for custom annotations on Native, and this problem is fixed in dev: https://github.com/Kotlin/kotlinx.serialization/commit/e62e23b559b4943d2b61a13b1ceaea939937ff4e

1reaction
sandwwraithcommented, Jan 24, 2019

This is also included in stable 0.10.0 release, which would be eventually compatible with all other libraries

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize list of objects using protobuf - Stack Overflow
Allright, I found solution if anyone is interested. The trick is to create a new message type and add original one as repeated....
Read more >
Serializing List of Objects - Google Groups
How can we serialize list of objects by using protobuf-net? Is protobuf-net support list of objects like public List(ABC) DEF
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
Generates JSON objects. Message field names are mapped to lowerCamelCase and become JSON object keys. If the json_name field option is specified, the...
Read more >
Protobuf - Quick Guide - Tutorialspoint
Google Protobuf performs the serialization and deserialization of the objects to bytes which can be transferred over the network.
Read more >
Protobuf In C# .NET - Part 2 - Serializing/Deserializing
Are you looking for a way to serialize and deserialize a C# object to/from a protocol buffer in .NET.? This article is perfect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found