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:
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+
230248To learn more about the methods and classes mentioned in this section,
231249see 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -29,6 +29,10 @@ What's New in 5.4
2929The 5.4 driver release includes the following changes, fixes,
3030and 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
You can’t perform that action at this time.
0 commit comments