Hi... is it impossible to serialize empty maps (`mapOf()` ) using kotlinx serialization? ```Class 'E...
t
Hi... is it impossible to serialize empty maps (
mapOf()
) using kotlinx serialization?
Copy code
Class 'EmptyMap' is not registered for polymorphic serialization in the scope of 'Map'.
To be registered automatically, class 'EmptyMap' has to be '@Serializable', and the base class 'Map' has to be sealed and '@Serializable'.
a
Can you send the code where this is happening? `Map`s already have a builtin
MapSerializer
, so it would be strange to serialize them polymorphically
t
I've looked into it a bit more... 1. It works when I just write
Json.encodeToString(mapOf())
2. It doesn't work when I try to serialize it with ktor client... 3. After some debugging, it seems like ktor is not finding the
LinkedHashMapSerializer
. It's instead trying to find a
PolymorphicSerializer
...
And it's not just EmptyMap that doesn't work, I can't even serialize a LinkedHashMap using Ktor...
g
Try to specify the generic type
Json.encodeToString<Map<Xxx>>(myMap)
If you don't specify the generic type, it's automatically inferred but it will depend on how serializers are setup by default. In you log you said it doesn't find LinkedHashMapSerializer, so I presume your parameter is a LinkedHashMap, and it's possible it's not explicitely serialized with MapSerializer.
150 Views