# serialization

Luc Girardin

03/22/2024, 10:42 AM
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
method to get JSON output. Would very much appreciate help!


03/22/2024, 12:00 PM
During creating the Json instance you have to pass the SerializerModule (defaults to empty) that contains your polymorphic serializers.

Luc Girardin

03/22/2024, 12:14 PM
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'.