ilyagulya
08/03/2020, 11:46 AM{
"data": {
"type1": [
{
"id": 1,
"payload": {
"fieldA": "123",
"fieldB": "321"
}
}
],
"type2": [
{
"id": 1,
"payload": {
"fieldC": "789",
"fieldD": "101112"
}
}
]
}
}
And I want to get this as result:
Map<String, List<Item>>
where key is type1, type2
and value list item is:
data class Item(
val id: Int,
val payload: ItemPayload
)
sealed class ItemPayload {
data class PayloadOne(
val fieldA: String,
val fieldB: String
)
data class PayloadTwo(
val fieldC: String
val fieldD: String
)
}
Right now I'm thinking about to deserialize the JSON to intermediate data class (which has JsonObject
instead of ItemPayload
), then determine which serializer do I need to use on each particular item and then deserialize the JsonObject
to exact ItemPayload
based on map key (type1, type2
)
But I don't understand how can I deserialize JsonObject
inside the KSerializer
.janvladimirmostert
08/03/2020, 3:46 PMjanvladimirmostert
08/03/2020, 3:52 PMNikky
08/03/2020, 4:50 PMobject MessageSerializer: JsonParametricSerializer<YourType>(YourType::class) {
override fun selectSerializer(element: JsonElement): KSerializer<out YourType> {
val obj = element.jsonObject
if(obj.contains("someKey")) {
return SomeEvent.seriualizer()
}
TODO("add more serializers")
}
}
Nikky
08/03/2020, 4:51 PMilyagulya
08/04/2020, 10:18 AMMap<String, List<Item>
)ilyagulya
08/04/2020, 10:19 AMJsonConfiguration.Default
because I have own configuration.Nikky
08/04/2020, 10:21 AMilyagulya
08/04/2020, 10:23 AMJsonInput
and it have Json
instance, awesome! 🙂Nikky
08/04/2020, 10:26 AMdeserialize thethis would also work if you usedto exactJsonObject
based on map key (ItemPayload
)type1, type2
@Serializable
data class Data(
val type1: Item<PayloadOne>
val type2: Item<PayloadTwo>
)
since it seems like all keys and assosciated types are known, if not you can still make it ignore unknown keysilyagulya
08/04/2020, 10:50 AM