Hi! I have a problem deserializing a protobuf mess...
# serialization
i
Hi! I have a problem deserializing a protobuf message with kotlinx serialization. I have this data class
Copy code
@Serializable
data class ProtobufMsg(
    val type: Int,
    @SerialName("headerList") val headerList: List<HeaderSignalInfo> = emptyList(),
    @SerialName("dataList") val dataList: List<SignalsData> = emptyList(),
    @SerialName("signalsList") val signalsList: List<SignalObject> = emptyList(),
    @SerialName("serviceInfo") val serviceInfo: ServiceInfo? = null
) {
    companion object {
        // Extension function for deserializing from ByteArray
        fun parseFrom(bytes: ByteArray): ProtobufMsg {
            return Protobuf.decodeFromByteArray(serializer(), bytes)
        }

        // Extension function for deserializing from InputStream
        fun parseFrom(input: InputStream): ProtobufMsg {
            return kotlinx.serialization.json.Json.decodeFromString(serializer(), input.reader().readText())
        }

        // Extension function for deserializing from String
        fun parseFrom(str: String): ProtobufMsg {
            return kotlinx.serialization.json.Json.decodeFromString(serializer(), str)
        }
    }
}
My .proto file is like that
Copy code
message ProtobufMsg{

    required int32 type = 1;
    repeated HeaderSignalInfo headerList = 2;
    repeated SignalsData dataList = 3;
    repeated SignalObject signalsList = 4;
    optional ServiceInfo  serviceInfo  = 5;


}
When i try to deserialize the bytes i receive, i get the error "Expected wire type 0, but found 7"; how can i resolve it? I kept the json serialization cause i'm only passing ByteArray so i only need the first parseFrom() function. I also searched on google and found that kotlin serialization use repeated as packed by default while the ones i receive might not be packed. However, how should the data class be if i want that kotlin serialization don't use repeated as packed? Thanks
t
I don't know protobuf, but my suggestion would be to see if there's another tool that you could feed your bytes into to see if that one can decode it properly.
That way you could at least verify that the generated bytes are correct, at least according to one other parser. (This could be a bit of a rabbit hole, but I feel like there's probably an online tool that would make this easy.)
c
There's no wire type 7 🤔 https://protobuf.dev/programming-guides/encoding/#structure This seems to point to the direction of the bytestream being incorrect
nod 1
i
Now i also tried generating classes with protoc compiler but it gives me "Protocol message tag had invalid wire type"
c
That confirms it; it's the message that's wrong, not the code. Where are the bytes coming from?
i
It's a stream of bytes sent through a socket by a server
t
Not generated by your own code, then? It might not even be protobuf. (Seems like it isn't.)