```1. this should deserilize to Message<Simmple...
# serialization
g
Copy code
1. this should deserilize to Message<SimmplePayload>
{ 
  id = "1",
  type = "simple",
  payload =  {
    name = "Blah"
  }
}


2. this should deserilize to Message<AdvancedPayload>
{ 
  id = "2",
  type = "advanced",
  payload =  {
    name = "WOW",
    description = "WOW WOW"
  }
}
Wait, I didn't see
Message
. You should probably define a data type for deserializing the message using custom serial names, that has methods to transform it into
Message
g
@rnett since the type is outside the payload, I'm having issue deserializing it.
r
Something like:
Copy code
// your Payload classes

sealed class Message<out T: Payload>(val typeName: String){
  abstract val id: String
  abstract val payload: T

  @SerialName("advanced")
  data class AdvMessage(override val id: String, override val payload: AdvancedPayload): Message("advanced) 
  @SerialName("simple")
  data class SimpleMessage(override val id: String, override val payload: SimplePayload): Message("simple")
}
☝️ 1
There's probably more elegant ways, and I think there may be some bugs with sealed classes + type params, but that should work
Something like
Copy code
default{ when(it){
  "simple" -> Message.serializer(SimplePayload.serializer())
  "advanced" -> Message.serializer(AdvancedPayload.serializer())
  else -> error("") // not sure if there's a null serializer but you could use that
} }
Not really how it's intended to be used though, and I'm not sure if it would work
d
The sealed Message class that @rnett suggested (but without the generics) is the only way to do what you want without a custom serializer.
The default thing won't quite work, because you'll need to annotate the message class with a polymorphic serializer to use it, but if you do that, the serializer that you're trying to get in the default lambda won't exist.