From 4f23cb1e9e101dd379886fc18ced69f8510f4d40 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 10:13:41 -0400 Subject: [PATCH 1/5] snake case example --- source/data-formats/serialization.txt | 18 ++++++++++++++++++ source/includes/data-formats/serialization.kt | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/source/data-formats/serialization.txt b/source/data-formats/serialization.txt index fda712d0..b766ce46 100644 --- a/source/data-formats/serialization.txt +++ b/source/data-formats/serialization.txt @@ -227,6 +227,22 @@ encode defaults: :end-before: end-codec :dedent: +Enforcing Snake Case Naming Example +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The following example shows how to convert field names from their data class +fields into snake case by setting the ``bsonNamingStrategy`` parameter: + +.. literalinclude:: /includes/data-formats/serialization.kt + :language: kotlin + :start-after: start-snake-case + :end-before: end-snake-case + :dedent: + +.. note:: + + The ``bsonNamingStrategy`` parameter is available for {+driver-short+} v5.4 and later. + To learn more about the methods and classes mentioned in this section, see the following API documentation: @@ -238,6 +254,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..af92fb05 100644 --- a/source/includes/data-formats/serialization.kt +++ b/source/includes/data-formats/serialization.kt @@ -101,6 +101,12 @@ fun main() { ) // end-codec + // start-snake-case + val myCustomCodec = KotlinSerializerCodec.create( + bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE) + ) + // end-snake-case + // start-poly-operations val collection = database.getCollection("school") From ca2bddf87061c03c08a26da86b3a0428d7f78ed1 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 10:28:43 -0400 Subject: [PATCH 2/5] move release note --- source/data-formats/serialization.txt | 2 ++ source/whats-new.txt | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/source/data-formats/serialization.txt b/source/data-formats/serialization.txt index b766ce46..39f7d4ec 100644 --- a/source/data-formats/serialization.txt +++ b/source/data-formats/serialization.txt @@ -227,6 +227,8 @@ encode defaults: :end-before: end-codec :dedent: +.. _kotlin-sync-serialization-snake-case-eg: + Enforcing Snake Case Naming Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/source/whats-new.txt b/source/whats-new.txt index 01fb6511..de630f42 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 support for converting field names to snake case 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 From 4187c9e76b0c1accdfa3212c264e95289647bed8 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Wed, 2 Apr 2025 12:56:45 -0400 Subject: [PATCH 3/5] dependency name --- source/data-formats/serialization.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/data-formats/serialization.txt b/source/data-formats/serialization.txt index 39f7d4ec..2153b927 100644 --- a/source/data-formats/serialization.txt +++ b/source/data-formats/serialization.txt @@ -243,7 +243,7 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: .. note:: - The ``bsonNamingStrategy`` parameter is available for {+driver-short+} v5.4 and later. + The ``bsonNamingStrategy`` parameter is available for bson-kotlinx v5.4 and later. To learn more about the methods and classes mentioned in this section, see the following API documentation: From 0b3db3ea9ccd7e793582e2fc225a6aa08bc38e4f Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Thu, 3 Apr 2025 10:04:04 -0400 Subject: [PATCH 4/5] RR feedback --- source/data-formats/serialization.txt | 16 ++++++++-------- source/includes/data-formats/serialization.kt | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/source/data-formats/serialization.txt b/source/data-formats/serialization.txt index 2153b927..db749d55 100644 --- a/source/data-formats/serialization.txt +++ b/source/data-formats/serialization.txt @@ -229,11 +229,15 @@ encode defaults: .. _kotlin-sync-serialization-snake-case-eg: -Enforcing Snake Case Naming Example -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +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: -The following example shows how to convert field names from their data class -fields into snake case by setting the ``bsonNamingStrategy`` parameter: .. literalinclude:: /includes/data-formats/serialization.kt :language: kotlin @@ -241,10 +245,6 @@ fields into snake case by setting the ``bsonNamingStrategy`` parameter: :end-before: end-snake-case :dedent: -.. note:: - - The ``bsonNamingStrategy`` parameter is available for bson-kotlinx v5.4 and later. - To learn more about the methods and classes mentioned in this section, see the following API documentation: diff --git a/source/includes/data-formats/serialization.kt b/source/includes/data-formats/serialization.kt index af92fb05..909af5f3 100644 --- a/source/includes/data-formats/serialization.kt +++ b/source/includes/data-formats/serialization.kt @@ -105,6 +105,10 @@ fun main() { 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 From 129f46a274c32eddf05d7b8d2dc4b995a3f00444 Mon Sep 17 00:00:00 2001 From: Rachel Mackintosh Date: Thu, 3 Apr 2025 10:52:29 -0400 Subject: [PATCH 5/5] update release note --- source/whats-new.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/whats-new.txt b/source/whats-new.txt index de630f42..cf8002e8 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -29,9 +29,9 @@ What's New in 5.4 The 5.4 driver release includes the following changes, fixes, and features: -- Adds support for converting field names to snake case during serialization. To - learn more, see the :ref:`kotlin-sync-serialization-snake-case-eg` on the - Serialization page. +- 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