Skip to content

Commit 92251db

Browse files
rachel-mackrustagir
authored andcommitted
[Kotlin Sync] Support naming strategy for bson-kotlinx (#90)
* snake case example * move release note * dependency name * RR feedback * update release note (cherry picked from commit 84aa7bb)
1 parent 94d98db commit 92251db

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

source/data-formats/serialization.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ encode defaults:
227227
:end-before: end-codec
228228
:dedent:
229229

230+
.. _kotlin-sync-serialization-snake-case-eg:
231+
232+
Implement Snake Case Naming Strategy
233+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234+
235+
When using ``bson-kotlinx`` package v5.4 or later, you can direct the driver to
236+
serialize data class fields names written in camel case to snake case in MongoDB.
237+
The following example shows how to create and register a custom codec
238+
to convert data class field names into snake case by setting the
239+
``bsonNamingStrategy`` parameter in a codec:
240+
241+
242+
.. literalinclude:: /includes/data-formats/serialization.kt
243+
:language: kotlin
244+
:start-after: start-snake-case
245+
:end-before: end-snake-case
246+
:dedent:
247+
230248
To learn more about the methods and classes mentioned in this section,
231249
see the following API documentation:
232250

@@ -238,6 +256,8 @@ see the following API documentation:
238256

239257
- `BsonConfiguration
240258
<{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__
259+
260+
- `BsonNamingStrategy <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-naming-strategy/index.html>`__
241261

242262
.. _kotlin-sync-polymorphic:
243263

source/includes/data-formats/serialization.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ fun main() {
101101
)
102102
// end-codec
103103

104+
// start-snake-case
105+
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
106+
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
107+
)
108+
109+
val registry = CodecRegistries.fromRegistries(
110+
CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry
111+
)
112+
// end-snake-case
113+
104114
// start-poly-operations
105115
val collection = database.getCollection<Person>("school")
106116

source/whats-new.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ What's New in 5.4
2929
The 5.4 driver release includes the following changes, fixes,
3030
and features:
3131

32+
- Adds ``BsonConfiguration`` support for bson-kotlinx snake case conversion
33+
during serialization. To learn more, see the
34+
:ref:`kotlin-sync-serialization-snake-case-eg` on the Serialization page.
35+
3236
.. sharedinclude:: dbx/jvm/v5.4-wn-items.rst
3337

3438
.. replacement:: install-bom-link

0 commit comments

Comments
 (0)