Skip to content
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

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
13 changes: 0 additions & 13 deletions .github/workflows/check-autobuilder.yml

This file was deleted.

30 changes: 29 additions & 1 deletion examples/src/test/kotlin/KotlinXSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ internal class KotlinXSerializationTest {
val department: String,
) : Person
// :snippet-end:

@Test
fun polymorphicSerializationTest() = runBlocking {

Expand Down Expand Up @@ -242,5 +241,34 @@ internal class KotlinXSerializationTest {
collection.drop()
}

// :snippet-start: datetime-data-class
@Serializable
data class Appointment(
val name: String,
@Contextual val date: LocalDate,
val time: LocalTime,
)
// :snippet-end:

@Test
fun dateTimeSerializationTest() = runBlocking {

// :snippet-start: datetime-insertone
val collection = database.getCollection<Appointment>("appointments")

val apptDoc = Appointment(
"Daria Smith",
LocalDate(2024, 10, 15),
LocalTime(hour = 11, minute = 30)
)

collection.insertOne(apptDoc)
// :snippet-end:

assertEquals(apptDoc.name, "Daria Smith")

collection.drop()
}

}

1 change: 1 addition & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ zstdVersion = "com.github.luben:zstd-jni:1.5.5-2"
logbackVersion = "1.2.11"
log4j2Version = "2.17.1"
serializationVersion = "1.5.1"
kotlinx-dt-version = "0.6.1"
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)
81 changes: 81 additions & 0 deletions source/fundamentals/data-formats/serialization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Collaborator

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

Suggested change
``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.
``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``
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
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.

Values of the ``time`` field are stored as strings, because the driver
Copy link
Collaborator

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?

Copy link
Collaborator Author

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

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",
}
8 changes: 7 additions & 1 deletion source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

S: specify type(s)

Suggested change
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.

To learn more, see the :ref:`kotlin-datetime-serialization` section of
the Kotlin Serialization guide.
Copy link
Collaborator

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

Suggested change
the Kotlin Serialization guide.
the {+language+} Serialization guide.


.. _kotlin-coroutine-version-5.1.3:

What's New in 5.1.3
Expand Down
Loading