Using koltinx.serialization
, is there a way to enable the "value" serializer to take it's ID from the "key" of the map entry?
For example, given the following JSON:
{
"data": {
"key1": { "name": "value1" },
"key2": { "name": "value2" },
}
}
And the serializable data class:
@Serializable
data class DataEntry(val id: String, val name: String)
How can I write a KSerializer
for the map (or for the map "value") that will enable the DataEntry
serializer to take the value of it's id
field from the map "key"?
The result being a List
(or Map
) of:
DataEntry("key1", "value1"),
DataEntry("key2", "value2"),
I know that I can deserialise the map using DataEntry(val name: String)
(without id
) and then build a list of entries with id
's from that. But in my case, for the map "value" I actually have many large data types, and this would require duplicating a lot of code. I want a generic way to handle this scenario given any data type.