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

Enforcing Snake Case Naming Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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:

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
6 changes: 6 additions & 0 deletions source/includes/data-formats/serialization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ fun main() {
)
// end-codec

// start-snake-case
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
)
// 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 support for converting field names to snake case during serialization. To
Copy link
Contributor

Choose a reason for hiding this comment

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

S: consider bumping this out into a "Serialization Features" subheading

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
Expand Down
Loading