Skip to content

Commit 4f23cb1

Browse files
committed
snake case example
1 parent 3920b16 commit 4f23cb1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

source/data-formats/serialization.txt

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

230+
Enforcing Snake Case Naming Example
231+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232+
233+
The following example shows how to convert field names from their data class
234+
fields into snake case by setting the ``bsonNamingStrategy`` parameter:
235+
236+
.. literalinclude:: /includes/data-formats/serialization.kt
237+
:language: kotlin
238+
:start-after: start-snake-case
239+
:end-before: end-snake-case
240+
:dedent:
241+
242+
.. note::
243+
244+
The ``bsonNamingStrategy`` parameter is available for {+driver-short+} v5.4 and later.
245+
230246
To learn more about the methods and classes mentioned in this section,
231247
see the following API documentation:
232248

@@ -238,6 +254,8 @@ see the following API documentation:
238254

239255
- `BsonConfiguration
240256
<{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__
257+
258+
- `BsonNamingStrategy <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-naming-strategy/index.html>`__
241259

242260
.. _kotlin-sync-polymorphic:
243261

source/includes/data-formats/serialization.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ 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+
// end-snake-case
109+
104110
// start-poly-operations
105111
val collection = database.getCollection<Person>("school")
106112

0 commit comments

Comments
 (0)