Skip to content

Commit 7f2f364

Browse files
committed
add snake case serialization eg
1 parent 31ef612 commit 7f2f364

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
2+
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
3+
)

source/fundamentals/data-formats/serialization.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ package to create a codec for your ``@Serializable`` data classes and
173173
customize what is stored.
174174

175175
Use the ``BsonConfiguration`` class to define the configuration,
176-
including whether to encode defaults, encode nulls, or define class discriminators.
176+
including whether to encode defaults, encode nulls, define class discriminators,
177+
or enforce snake case.
177178

178179
To create a custom codec, install the ``bson-kotlinx``
179180
dependency to your project. Select from the following tabs to see how to
@@ -241,12 +242,28 @@ The following example shows how to create a codec using the
241242
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.custom-serialization.kt
242243
:language: kotlin
243244

245+
Enforcing Snake Case Naming Example
246+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247+
248+
The following example shows how to convert field names from their data class
249+
fields into snake case by setting the ``bsonNamingStrategy`` parameter:
250+
251+
.. code-block:: kotlin
252+
:copyable: true
253+
254+
import org.bson.codecs.kotlinx.BsonConfiguration
255+
import org.bson.codecs.kotlinx.BsonNamingStrategy
256+
257+
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-serialization.kt
258+
:language: kotlin
259+
244260
For more information about the methods and classes mentioned in this section,
245261
see the following API documentation:
246262

247263
- `KotlinSerializerCodec <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/index.html>`__
248264
- `KotlinSerializerCodec.create() <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/create.html>`__
249265
- `BsonConfiguration <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__
266+
- `BsonNamingStrategy <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-naming-strategy/index.html>`__
250267

251268
.. _kotlin-polymorphic:
252269

0 commit comments

Comments
 (0)