I am trying to serialize a `List<Map<String,...
# serialization
l
I am trying to serialize a
List<Map<String, Any?>>
. Seems that I need to register a PolymorphicSerializer for that purpose, but I didn’t figure out how to exactly do it so that I can pass it to the
encodeToString()
method to get JSON output. Would very much appreciate help!
h
During creating the Json instance you have to pass the SerializerModule (defaults to empty) that contains your polymorphic serializers.
l
Many thanks @hfhbd for the reply. Here is what I do:
Copy code
val listOfMap: List<Map<String, Any?>> = ...
val json = Json {
    useArrayPolymorphism = true
    serializersModule = SerializersModule {
        polymorphic(Any::class) {
            subclass(List::class, ListSerializer(PolymorphicSerializer(Any::class).nullable))
        }
    }
}
val serializer = ListSerializer(PolymorphicSerializer(Map::class).nullable) 

println(json.encodeToString(serializer, listOfMap))
but as you can see I do not understand what I am doing and do get the following error
SerializationException: Serializer for subclass 'LinkedHashMap' is not found in the polymorphic scope of 'LinkedHashMap'.