Skip to content

Commit 97cf990

Browse files
committed
DOCSP-41865: datetime serializers
1 parent e331367 commit 97cf990

File tree

6 files changed

+129
-3
lines changed

6 files changed

+129
-3
lines changed

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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,84 @@ 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 allows
317+
you to work with dates and times. If you want a high level of control
318+
over how your date and time values are serialized, you can add the
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+
The following ``Appointment`` data class has the ``@Serializable``
358+
annotation and the ``@Contextual`` annotation on the ``date`` field
359+
to use the ``kotlinx-datetime`` serializer for ``LocalDate`` values.
360+
Values of the ``time`` field are stored as strings, because the driver
361+
does not use a serializer without the ``@Contextual`` annotation:
362+
363+
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-data-class.kt
364+
:language: kotlin
365+
366+
The following example inserts an instance of the ``Appointment`` data
367+
class into MongoDB:
368+
369+
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-insertone.kt
370+
:language: kotlin
371+
372+
In MongoDB, the ``LocalDate`` value is stored as a BSON date, and the
373+
``time`` field is stored by using default serialization as a string:
374+
375+
.. code-block:: json
376+
377+
{
378+
"_id": ...,
379+
"name": "Daria Smith",
380+
"date": {
381+
"$date": "2024-10-15T00:00:00.000Z"
382+
},
383+
"time": "11:30",
384+
}

source/whats-new.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ improvements, and fixes:
3636
:ref:`kotlin-search-indexes` in the Indexes guide
3737

3838
- Adds support for serializers from the ``kotlinx-datetime`` library
39-
that let you map to BSON as the expected types. To learn more, see the
40-
:ref:`fundamentals-kotlin-serialization` guide.
39+
that let you map to BSON as the expected types instead of as strings.
40+
To learn more, see the :ref:`kotlin-datetime-serialization` section of
41+
the Kotlin Serialization guide.
4142

4243
.. _kotlin-coroutine-version-5.1.3:
4344

0 commit comments

Comments
 (0)