-
Notifications
You must be signed in to change notification settings - Fork 20
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@Serializable | ||
data class Appointment( | ||
val name: String, | ||
@Contextual val date: LocalDate, | ||
val time: LocalTime, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
val collection = database.getCollection<Appointment>("appointments") | ||
|
||
val apptDoc = Appointment( | ||
"Daria Smith", | ||
LocalDate(2024, 10, 15), | ||
LocalTime(hour = 11, minute = 30) | ||
) | ||
|
||
collection.insertOne(apptDoc) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -301,3 +301,84 @@ deserializes them accordingly. | |||||||||||||||||||||||
Retrieving as Document type | ||||||||||||||||||||||||
Document{{_id=..., _t=Teacher, name=Vivian Lee, department=History}} | ||||||||||||||||||||||||
Document{{_id=..., _t=Student, name=Kate Parker, grade=10}} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. _kotlin-datetime-serialization: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Serialize Dates and Times | ||||||||||||||||||||||||
------------------------- | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
In this section, you can learn about using {+language+} serialization to | ||||||||||||||||||||||||
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 | ||||||||||||||||||||||||
dependency to your project's dependency list. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Select from the following tabs to see how to add the ``kotlinx-datetime`` | ||||||||||||||||||||||||
dependency to your project by using the :guilabel:`Gradle` and | ||||||||||||||||||||||||
:guilabel:`Maven` package managers: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. tabs:: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. tab:: | ||||||||||||||||||||||||
:tabid: Gradle | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. code-block:: kotlin | ||||||||||||||||||||||||
:caption: build.gradle.kts | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
implementation("org.jetbrains.kotlinx:kotlinx-datetime:{+kotlinx-dt-version+}") | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. tab:: | ||||||||||||||||||||||||
:tabid: Maven | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. code-block:: kotlin | ||||||||||||||||||||||||
:caption: pom.xml | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
<dependency> | ||||||||||||||||||||||||
<groupId>org.jetbrains.kotlinx</groupId> | ||||||||||||||||||||||||
<artifactId>kotlinx-datetime-jvm</artifactId> | ||||||||||||||||||||||||
<version>{+kotlinx-dt-version+}</version> | ||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
To learn more about this library, see the :github:`kotlinx-datetime repository | ||||||||||||||||||||||||
</Kotlin/kotlinx-datetime>` on GitHub. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Example Data Class with Dates and Times | ||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
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. | ||||||||||||||||||||||||
|
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. | |
In the following code example, the driver serializes all fields in | |
the ``Appointment`` data class in the following ways: | |
- ``name``: The driver serializes the value as a string. | |
- ``date``: The driver uses the ``kotlinx-datetime`` serializer because | |
the field includes the ``@Contextual`` annotation. The value is serialized | |
as a BSON date. | |
- ``time``: The driver serializes the value as a string. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Does this code example show that the time
field is stored as a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is the default behavior of the driver. The result is shown later in the section
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -26,14 +26,20 @@ Learn what's new in: | |||||
What's New in 5.2 | ||||||
----------------- | ||||||
|
||||||
New features of the 4.11 driver release include: | ||||||
The 5.2 driver release includes the following new features, | ||||||
improvements, and fixes: | ||||||
|
||||||
.. sharedinclude:: dbx/jvm/v5.2-wn-items.rst | ||||||
|
||||||
.. replacement:: avs-index-link | ||||||
|
||||||
: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. | ||||||
|
that let you map to BSON as the expected types instead of as strings. | |
that let you map ``LocalDate``[?] values to BSON as the expected types instead of as strings. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use source constant here
the Kotlin Serialization guide. | |
the {+language+} Serialization guide. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: maybe a little more straightforward