Custom serializer for dynamic map - possible?



I have a type like this one below, and I want to write CustomMapSerializer so that it deals with the fields property by handing the map values to different Serializers, depending on the key.

data class Foo(@Serializable(with=UUIDSerializer::class) 
               val fooID: UUID,
               val fields: Map<String, Any?>)

The docs for kotlinx.serialization explain a little about a custom serializer for a compound type like a data class, but I don’t see anything on how to handle a variable sized collection like this.

Is there documentation or example code on this anywhere?


You will have to have a look at the source code for the runtime library. It has the default map serializer that you can copy as much as possible.


Yeah, I was looking at the MapLikeSerializer in CollectionSerializers.kt.

One point of confusion is that the MapLikeSerializer returns a descriptor of type MapLikeDescriptor which has a constant number (2) of sub-descriptors. It would appear that I can’t mimic that, since I only know the number of sub-serializers I’m going to use when I see the object to encode. But, without docs to explain the purpose and meaning of the properties of the SerialDescriptor, like elementCount, it’s difficult to form a plan of attack.

I hope this isn’t considered “finished” documentation by the community/authors. I accept it temporarily for bleeding edge software, but ultimately if you have to read the source to learn to use a non-trivial library, something is wrong.