Skip to content

Commit 4e7eae0

Browse files
[Kotlin CR] Support naming strategy for bson-kotlinx (#209)
* add snake case serialization eg * version note * driver reference * what's new * release note * dependency name * bluehawk * RR feedback * MW feedback --------- Co-authored-by: rustagir <rea.rustagi@mongodb.com>
1 parent 92f18dd commit 4e7eae0

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

examples/src/test/kotlin/KotlinXSerializationTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.bson.codecs.configuration.CodecRegistries
2626
import org.bson.codecs.kotlinx.BsonConfiguration
2727
import org.bson.codecs.kotlinx.BsonDecoder
2828
import org.bson.codecs.kotlinx.BsonEncoder
29+
import org.bson.codecs.kotlinx.BsonNamingStrategy
2930
import org.bson.codecs.kotlinx.KotlinSerializerCodec
3031
import org.bson.codecs.kotlinx.ObjectIdSerializer
3132
import org.bson.types.ObjectId
@@ -133,6 +134,34 @@ internal class KotlinXSerializationTest {
133134
collection.drop()
134135
}
135136

137+
@Test
138+
fun snakeCaseNamingTest() = runBlocking {
139+
@Serializable
140+
data class PaintOrder(
141+
val ManufacturerName: String,
142+
val QuantityOfCans: Int,
143+
)
144+
145+
val collection = database.getCollection<PaintOrder>("orders2")
146+
147+
// :snippet-start: snake-case-naming
148+
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
149+
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
150+
)
151+
152+
val registry = CodecRegistries.fromRegistries(
153+
CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry
154+
)
155+
// :snippet-end:
156+
157+
val paint = PaintOrder("Acme", 10)
158+
collection.withCodecRegistry(registry).insertOne(paint)
159+
val result = collection.withDocumentClass<Document>().find().first().toJson()
160+
assertTrue(result.contains("quantity_of_cans"))
161+
assertFalse(result.contains("ManufacturerName"))
162+
collection.drop()
163+
}
164+
136165
// :snippet-start: kserializer
137166
object InstantAsBsonDateTime : KSerializer<Instant> {
138167
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InstantAsBsonDateTime", PrimitiveKind.LONG)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
val myCustomCodec = KotlinSerializerCodec.create<PaintOrder>(
2+
bsonConfiguration = BsonConfiguration(bsonNamingStrategy = BsonNamingStrategy.SNAKE_CASE)
3+
)
4+
5+
val registry = CodecRegistries.fromRegistries(
6+
CodecRegistries.fromCodecs(myCustomCodec), collection.codecRegistry
7+
)

source/fundamentals/data-formats/serialization.txt

Lines changed: 23 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,33 @@ 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+
.. _kotlin-serialization-snake-case-eg:
246+
247+
Implement Snake Case Naming Strategy
248+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
249+
250+
When using ``bson-kotlinx`` package v5.4 or later, you can direct the driver to
251+
serialize data class field names written in camel case to snake case in MongoDB.
252+
The following example shows how to create and register a custom codec
253+
to convert data class field names into snake case by setting the
254+
``bsonNamingStrategy`` parameter in a codec:
255+
256+
.. code-block:: kotlin
257+
:copyable: true
258+
259+
import org.bson.codecs.kotlinx.BsonConfiguration
260+
import org.bson.codecs.kotlinx.BsonNamingStrategy
261+
262+
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.snake-case-naming.kt
263+
:language: kotlin
264+
244265
For more information about the methods and classes mentioned in this section,
245266
see the following API documentation:
246267

247268
- `KotlinSerializerCodec <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/index.html>`__
248269
- `KotlinSerializerCodec.create() <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/create.html>`__
249270
- `BsonConfiguration <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__
271+
- `BsonNamingStrategy <{+api-root+}/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-naming-strategy/index.html>`__
250272

251273
.. _kotlin-polymorphic:
252274

source/whats-new.txt

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

34+
- Adds ``BsonConfiguration`` support for ``bson-kotlinx`` snake case conversion
35+
during serialization. To learn more, see the
36+
:ref:`kotlin-serialization-snake-case-eg` section on the Serialization page.
37+
3438
.. sharedinclude:: dbx/jvm/v5.4-wn-items.rst
3539

3640
.. replacement:: install-bom-link

0 commit comments

Comments
 (0)