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