Skip to content

[Kotlin Sync] Support naming strategy for bson-kotlinx #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions source/data-formats/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: remove extra line


.. 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:

Expand All @@ -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:

Expand Down
10 changes: 10 additions & 0 deletions source/includes/data-formats/serialization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ fun main() {
)
// end-codec

// start-snake-case
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
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<Person>("school")

Expand Down
4 changes: 4 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +32 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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.
- Adds ``BsonConfiguration`` support in the ``bson-kotlinx`` package for snake case
conversion during serialization. To learn more, see the
:ref:`kotlin-sync-serialization-snake-case-eg` section in the Serialization page.


.. sharedinclude:: dbx/jvm/v5.4-wn-items.rst

.. replacement:: install-bom-link
Expand Down
Loading