-
Notifications
You must be signed in to change notification settings - Fork 18
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
DOCSP-41865: datetime serializers #173
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. | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: i had a little trouble parsing this all at first. i tried a few versions and ended up with a list, but feel free to use/discard parts as you see fit. it also sort of bleeds into the following example about the time being stored as a string.
Suggested change
|
||||||||||||||||||||||||
Values of the ``time`` field are stored as strings, because the driver | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Does this code example show that the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||||
does not use a serializer without the ``@Contextual`` annotation: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-data-class.kt | ||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
The following example inserts an instance of the ``Appointment`` data | ||||||||||||||||||||||||
class into MongoDB: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.datetime-insertone.kt | ||||||||||||||||||||||||
: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: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
.. code-block:: json | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
"_id": ..., | ||||||||||||||||||||||||
"name": "Daria Smith", | ||||||||||||||||||||||||
"date": { | ||||||||||||||||||||||||
"$date": "2024-10-15T00:00:00.000Z" | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
"time": "11:30", | ||||||||||||||||||||||||
} |
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: specify type(s)
Suggested change
|
||||||
To learn more, see the :ref:`kotlin-datetime-serialization` section of | ||||||
the Kotlin Serialization guide. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use source constant here
Suggested change
|
||||||
|
||||||
.. _kotlin-coroutine-version-5.1.3: | ||||||
|
||||||
What's New in 5.1.3 | ||||||
|
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