I have a class that gets serialized for network traffic.
@Serializable
data class Packet(val dataType: String, val payload: Any)
I've used Java serialization to send it over the wire. 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. Easy breazy.
But Kotlinx Serialization (with ProtoBuf) is a stickler about this Any type for reasons that aren't obvious to me. I can't register a serializer for Any. In the docs they recommend a polymorphic approach, which sorta works but you have to make the packet typed:
data class Packet<out T : Any>(val dataType: String, val payload: T) : SomeBaseClass<T>
but 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.
This is the worst catch-22. 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."