bbaldino
08/11/2022, 5:08 PMsealed class MessagePayload ...
data class PayloadOne(val foo: String) : MessagePayload ...
class Message(val msgType: MessageType, val payload: MessagePayload) { ... }
and I was hoping to get it to serialize as:
{
"msgType": "payloadOne",
"payload": {
"foo": "..."
}
}
that is: have the discriminant for the 'payload' field actually be at a layer "above", is that possible via just annotations? or would it require custom serializer/deserializer?Dominaezzz
08/11/2022, 7:30 PMbbaldino
08/11/2022, 7:31 PMDominaezzz
08/11/2022, 7:35 PMsealed class MessagePayload ...
data class PayloadOne(val foo: String) : MessagePayload ...
sealed class Message {
abstract val msgType: MessageType
abstract val payload: MessagePayload
}
data class PayloadOneMessage(override val msgType: MessageType, override val payload: PayloadOne) : Message()
bbaldino
08/11/2022, 7:36 PMPayloadOneMessage
and not Message
, right?Dominaezzz
08/11/2022, 7:37 PMMessage
.bbaldino
08/11/2022, 7:37 PMbbaldino
08/11/2022, 7:48 PMbbaldino
08/11/2022, 8:00 PMmsgType
field? i end up with something like:
{
"type": "MyMsgType",
"payload": {
"foo": "hello"
}
}
which is what i want, but why doesn't msgType
get serialized there?Dominaezzz
08/11/2022, 8:01 PMbbaldino
08/11/2022, 8:01 PMbbaldino
08/11/2022, 8:04 PMbbaldino
08/11/2022, 8:41 PMmsgType
field is omitted from the tostring as well, for example:
println(PayloadOneMessage(PayloadOne("hello")))
prints PayloadOneMessage(payload=PayloadOne(foo=hello))
Dominaezzz
08/11/2022, 8:43 PMbbaldino
08/11/2022, 8:44 PMDominaezzz
08/11/2022, 8:44 PM@Required
annotation to the field.bbaldino
08/11/2022, 8:45 PM