The framework won't ignore the payload: Any field (gives a compile error) and I can't even write a custom serializer because defining an element of type Any in a customer serializer (for the descriptor) gives the same run-time error of "no serializer registered for Any." In the docs they recommend a polymorphic approach, which sorta works but you have to make the packet typed: data class Packet(val dataType: String, val payload: T) : SomeBaseClassīut this kinda sucks because it weighs down a lot of code paths with inline reified typing, plus this doesn't solve that the receiving end won't know what type to try to deserialize the payload as without being to look at the dataType field first. Easy breazy.īut Kotlinx Serialization (with ProtoBuf) is a stickler about this Any type for reasons that aren't obvious to me. The receiver can't know the type of the payload but Java deserializes it just fine, and then I use when(dataType) as a lookup to correctly cast the Any object to its correct type. I've used Java serialization to send it over the wire. class Packet(val dataType: String, val payload: Any) I have a class that gets serialized for network traffic.
0 Comments
Leave a Reply. |