Skip to content

Commit e0b1e01

Browse files
authored
DOCSP-41865: datetime serializers (#173)
* DOCSP-41865: datetime serializers * DOCSP-41865: datetime serializers * fixes * MW PR fixes 1
1 parent 5854886 commit e0b1e01

File tree

7 files changed

+141
-15
lines changed

7 files changed

+141
-15
lines changed

.github/workflows/check-autobuilder.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/src/test/kotlin/KotlinXSerializationTest.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ internal class KotlinXSerializationTest {
204204
val department: String,
205205
) : Person
206206
// :snippet-end:
207-
208207
@Test
209208
fun polymorphicSerializationTest() = runBlocking {
210209

@@ -242,5 +241,34 @@ internal class KotlinXSerializationTest {
242241
collection.drop()
243242
}
244243

244+
// :snippet-start: datetime-data-class
245+
@Serializable
246+
data class Appointment(
247+
val name: String,
248+
@Contextual val date: LocalDate,
249+
val time: LocalTime,
250+
)
251+
// :snippet-end:
252+
253+
@Test
254+
fun dateTimeSerializationTest() = runBlocking {
255+
256+
// :snippet-start: datetime-insertone
257+
val collection = database.getCollection<Appointment>("appointments")
258+
259+
val apptDoc = Appointment(
260+
"Daria Smith",
261+
LocalDate(2024, 10, 15),
262+
LocalTime(hour = 11, minute = 30)
263+
)
264+
265+
collection.insertOne(apptDoc)
266+
// :snippet-end:
267+
268+
assertEquals(apptDoc.name, "Daria Smith")
269+
270+
collection.drop()
271+
}
272+
245273
}
246274

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ zstdVersion = "com.github.luben:zstd-jni:1.5.5-2"
3535
logbackVersion = "1.2.11"
3636
log4j2Version = "2.17.1"
3737
serializationVersion = "1.5.1"
38+
kotlinx-dt-version = "0.6.1"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Serializable
2+
data class Appointment(
3+
val name: String,
4+
@Contextual val date: LocalDate,
5+
val time: LocalTime,
6+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
val collection = database.getCollection<Appointment>("appointments")
2+
3+
val apptDoc = Appointment(
4+
"Daria Smith",
5+
LocalDate(2024, 10, 15),
6+
LocalTime(hour = 11, minute = 30)
7+
)
8+
9+
collection.insertOne(apptDoc)

source/fundamentals/data-formats/serialization.txt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,91 @@ deserializes them accordingly.
301301
Retrieving as Document type
302302
Document{{_id=..., _t=Teacher, name=Vivian Lee, department=History}}
303303
Document{{_id=..., _t=Student, name=Kate Parker, grade=10}}
304+
305+
.. _kotlin-datetime-serialization:
306+
307+
Serialize Dates and Times
308+
-------------------------
309+
310+
In this section, you can learn about using {+language+} serialization to
311+
work with date and time types.
312+
313+
kotlinx-datetime Library
314+
~~~~~~~~~~~~~~~~~~~~~~~~
315+
316+
``kotlinx-datetime`` is a {+language+} library that offers
317+
a high level of control over how your date and time values
318+
are serialized. To use the library, add the ``kotlinx-datetime``
319+
dependency to your project's dependency list.
320+
321+
Select from the following tabs to see how to add the ``kotlinx-datetime``
322+
dependency to your project by using the :guilabel:`Gradle` and
323+
:guilabel:`Maven` package managers:
324+
325+
.. tabs::
326+
327+
.. tab::
328+
:tabid: Gradle
329+
330+
.. code-block:: kotlin
331+
:caption: build.gradle.kts
332+
333+
implementation("org.jetbrains.kotlinx:kotlinx-datetime:{+kotlinx-dt-version+}")
334+
335+
.. tab::
336+
:tabid: Maven
337+
338+
.. code-block:: kotlin
339+
:caption: pom.xml
340+
341+
<dependency>
342+
<groupId>org.jetbrains.kotlinx</groupId>
343+
<artifactId>kotlinx-datetime-jvm</artifactId>
344+
<version>{+kotlinx-dt-version+}</version>
345+
</dependency>
346+
347+
To learn more about this library, see the :github:`kotlinx-datetime repository
348+
</Kotlin/kotlinx-datetime>` on GitHub.
349+
350+
Example Data Class with Dates and Times
351+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352+
353+
After you add the library dependency, you can implement serializers from
354+
the ``kotlinx-datetime`` library that map your data class field values
355+
to the expected types in BSON.
356+
357+
In the following example, the driver serializes the fields of
358+
the ``Appointment`` data class with the following behavior:
359+
360+
- ``name``: The driver serializes the value as a string.
361+
362+
- ``date``: The driver uses the ``kotlinx-datetime`` serializer
363+
because the field has the ``@Contextual`` annotation. ``LocalDate``
364+
values are serialized as BSON dates.
365+
366+
- ``time``: The driver serializes the value as a string because it does
367+
not have the ``@Contextual`` annotation. This is the default
368+
serialization behavior for ``LocalTime`` values.
369+
370+
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-data-class.kt
371+
:language: kotlin
372+
373+
The following example inserts an instance of the ``Appointment`` data
374+
class into MongoDB:
375+
376+
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-insertone.kt
377+
:language: kotlin
378+
379+
In MongoDB, the ``LocalDate`` value is stored as a BSON date, and the
380+
``time`` field is stored as a string by default serialization:
381+
382+
.. code-block:: json
383+
384+
{
385+
"_id": ...,
386+
"name": "Daria Smith",
387+
"date": {
388+
"$date": "2024-10-15T00:00:00.000Z"
389+
},
390+
"time": "11:30",
391+
}

source/whats-new.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ Learn what's new in:
2626
What's New in 5.2
2727
-----------------
2828

29-
New features of the 4.11 driver release include:
29+
The 5.2 driver release includes the following new features,
30+
improvements, and fixes:
3031

3132
.. sharedinclude:: dbx/jvm/v5.2-wn-items.rst
3233

3334
.. replacement:: avs-index-link
3435

3536
:ref:`kotlin-search-indexes` in the Indexes guide
3637

38+
- Adds support for serializers from the ``kotlinx-datetime`` library
39+
that let you map {+language+} date and time types to BSON as the
40+
expected types instead of as strings. To learn more, see the
41+
:ref:`kotlin-datetime-serialization` section of the {+language+}
42+
Serialization guide.
43+
3744
- Supports serialization of `JsonElement
3845
<{+kotlin-docs+}/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-element/>`__
3946
values. To work with the ``JsonElement`` type, you must add the

0 commit comments

Comments
 (0)