Ivan Di Sante
05/07/2024, 10:08 AM@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
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?
ThanksTim McCormack
05/07/2024, 12:30 PMTim McCormack
05/07/2024, 12:30 PMCLOVIS
05/07/2024, 12:31 PMIvan Di Sante
05/07/2024, 1:48 PMCLOVIS
05/07/2024, 1:49 PMIvan Di Sante
05/07/2024, 1:51 PMTim McCormack
05/08/2024, 1:49 AM