I need to serialize a class with a map so that the keys in the map are sorted in the json. So if there's a class
@Serializable
class Example(val map: Map<String, Int>)
and it's serialized with
val example = Example(mapOf("b" to 2, "a" to 1, "c" to 3))
println(Json.encodeToString(example))
then the resulting json should be
{
"map": {
"a": 1,
"b": 2,
"c": 3
}
}
I tried to use SortedMap instead of Map, but that throws an exception:
kotlinx.serialization.SerializationException: Class 'TreeMap' is not registered for polymorphic serialization in the scope of 'SortedMap'
How can I get a sorted json using kotlinx.serialization?
(kotlin 1.4.0, kotlinx.serialization 1.0.0-RC)