Confusion how kotlinx and codec serialization flow is working in kotlin/java Mongodb driver

Every issue and blockage I had in the MongoDb driver was because I didn’t had and still don’t an insight into Mongodb’s internal workings that deal with serialization, so I have prepared a Sequence Diagram of how I think the Mongodb driver is working…

Is this how Mongodb driver actually works or am I missing something???
Here is the link where you can edit this image… Plant UML diagram editor

All help will be much appreciated.

This might be a better way to visualize it:

PlantUML link

I just did it for the encoding step but it should help.


This is much better way to visualize the flow! Great!!! This clarifies many errors that I was getting!

I have questions…

  1. org.bson.codecs.kotlinx.KotlinSerializerCodec is internal BSON serializer that tries to convert entities/values to Bson format AND he “ONLY” uses native Kotlinx serializers that are attached via @Serializable to actually convert internal values/entities to the corresponding Bson format? Is that true?

  2. What happens if KotlinSerializerCodec does not find appropriate kotlinx serializer will he raise an serialization error or start searching for the appropriate Codec serializer? I could not see the implementation logic so I joust assuming that based on your flow graf there should be an Error.

  3. If you register custom codecs via mongoClient.getDatabase(db_name).withCodecRegistry(codecRegistry) what status
    does that classes get is it ordinary Codec or KotlinSerializerCodec? And what should I do to register the other one?

Can you provide some helpful src links from GitHub - mongodb/mongo-java-driver: The official MongoDB drivers for Java, Kotlin, and Scala
which you think are crucial for the understanding of how serialization/deserialization works
so that I can take one day off and really study the internal workings of conversion…

Thank you so much!