File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -227,6 +227,24 @@ encode defaults:
227
227
:end-before: end-codec
228
228
:dedent:
229
229
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
+
230
248
To learn more about the methods and classes mentioned in this section,
231
249
see the following API documentation:
232
250
@@ -238,6 +256,8 @@ see the following API documentation:
238
256
239
257
- `BsonConfiguration
240
258
<{+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>`__
241
261
242
262
.. _kotlin-sync-polymorphic:
243
263
Original file line number Diff line number Diff line change @@ -101,6 +101,16 @@ fun main() {
101
101
)
102
102
// end-codec
103
103
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
+
104
114
// start-poly-operations
105
115
val collection = database.getCollection<Person >(" school" )
106
116
Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ What's New in 5.4
29
29
The 5.4 driver release includes the following changes, fixes,
30
30
and features:
31
31
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
+
32
36
.. sharedinclude:: dbx/jvm/v5.4-wn-items.rst
33
37
34
38
.. replacement:: install-bom-link
You can’t perform that action at this time.
0 commit comments