Skip to content

DOCSP-41865: datetime serializers #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions source/fundamentals/data-formats/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ work with date and time types.
kotlinx-datetime Library
~~~~~~~~~~~~~~~~~~~~~~~~

``kotlinx-datetime`` is a {+language+} library that allows
you to work with dates and times. If you want a high level of control
over how your date and time values are serialized, you can add the
``kotlinx-datetime`` is a {+language+} library that offers
a high level of control over how your date and time values
are serialized. To use the library, add the ``kotlinx-datetime``
dependency to your project's dependency list.

Select from the following tabs to see how to add the ``kotlinx-datetime``
Expand Down Expand Up @@ -354,11 +354,17 @@ After you add the library dependency, you can implement serializers from
the ``kotlinx-datetime`` library that map your data class field values
to the expected types in BSON.

The following ``Appointment`` data class has the ``@Serializable``
annotation and the ``@Contextual`` annotation on the ``date`` field
to use the ``kotlinx-datetime`` serializer for ``LocalDate`` values.
Values of the ``time`` field are stored as strings, because the driver
does not use a serializer without the ``@Contextual`` annotation:
In the following example, the driver serializes the fields of
the ``Appointment`` data class with the following behavior:

- ``name``: The driver serializes the value as a string.

- ``date``: The driver uses the ``kotlinx-datetime`` serializer
because the field has the ``@Contextual`` annotation. So,
``LocalDate`` values are serialized as BSON dates.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S:

Suggested change
because the field has the ``@Contextual`` annotation. So,
``LocalDate`` values are serialized as BSON dates.
because the field has the ``@Contextual`` annotation.
``LocalDate`` values are serialized as BSON dates.


- ``time``: The driver serializes the value as a string because it does
not have the ``@Contextual`` annotation.

.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-data-class.kt
:language: kotlin
Expand All @@ -370,7 +376,7 @@ class into MongoDB:
:language: kotlin

In MongoDB, the ``LocalDate`` value is stored as a BSON date, and the
``time`` field is stored by using default serialization as a string:
``time`` field is stored as a string by default serialization:

.. code-block:: json

Expand Down
7 changes: 4 additions & 3 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ improvements, and fixes:
:ref:`kotlin-search-indexes` in the Indexes guide

- Adds support for serializers from the ``kotlinx-datetime`` library
that let you map to BSON as the expected types instead of as strings.
To learn more, see the :ref:`kotlin-datetime-serialization` section of
the Kotlin Serialization guide.
that let you map {+language+} date and time types to BSON as the
expected types instead of as strings. To learn more, see the
:ref:`kotlin-datetime-serialization` section of the {+language+}
Serialization guide.

.. _kotlin-coroutine-version-5.1.3:

Expand Down
Loading