diff --git a/source/data-formats/serialization.txt b/source/data-formats/serialization.txt index fda712d0..db749d55 100644 --- a/source/data-formats/serialization.txt +++ b/source/data-formats/serialization.txt @@ -227,6 +227,24 @@ encode defaults: :end-before: end-codec :dedent: +.. _kotlin-sync-serialization-snake-case-eg: + +Implement Snake Case Naming Strategy +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +When using ``bson-kotlinx`` package v5.4 or later, you can direct the driver to +serialize data class fields names written in camel case to snake case in MongoDB. +The following example shows how to create and register a custom codec +to convert data class field names into snake case by setting the +``bsonNamingStrategy`` parameter in a codec: + + +.. literalinclude:: /includes/data-formats/serialization.kt + :language: kotlin + :start-after: start-snake-case + :end-before: end-snake-case + :dedent: + To learn more about the methods and classes mentioned in this section, see the following API documentation: @@ -238,6 +256,8 @@ see the following API documentation: - `BsonConfiguration <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__ + +- `BsonNamingStrategy <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-naming-strategy/index.html>`__ .. _kotlin-sync-polymorphic: diff --git a/source/includes/data-formats/serialization.kt b/source/includes/data-formats/serialization.kt index 5a53afa2..909af5f3 100644 --- a/source/includes/data-formats/serialization.kt +++ b/source/includes/data-formats/serialization.kt @@ -101,6 +101,16 @@ fun main() { ) // end-codec + // start-snake-case + val myCustomCodec = KotlinSerializerCodec.create( + bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE) + ) + + val registry = CodecRegistries.fromRegistries( + CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry + ) + // end-snake-case + // start-poly-operations val collection = database.getCollection("school") diff --git a/source/whats-new.txt b/source/whats-new.txt index 01fb6511..cf8002e8 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -29,6 +29,10 @@ What's New in 5.4 The 5.4 driver release includes the following changes, fixes, and features: +- Adds ``BsonConfiguration`` support for bson-kotlinx snake case conversion + during serialization. To learn more, see the + :ref:`kotlin-sync-serialization-snake-case-eg` on the Serialization page. + .. sharedinclude:: dbx/jvm/v5.4-wn-items.rst .. replacement:: install-bom-link