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

Add kotlin.time.Instant serializers #2945

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

dkhalanskyjb
Copy link

Can be merged after moving to Kotlin 2.1.20, which introduces
kotlin.time.Instant.

kotlinx.datetime.Instant entered the stdlib as kotlin.time.Instant,
and so kotlinx.serialization takes over its serializers.
See Kotlin/KEEP#387

Can be merged after moving to Kotlin 2.1.20, which introduces
kotlin.time.Instant.

kotlinx.datetime.Instant entered the stdlib as kotlin.time.Instant,
and so kotlinx.serialization takes over its serializers.
See Kotlin/KEEP#387
@sandwwraith
Copy link
Member

@sandwwraith
Copy link
Member

For now, you can use serializer(typeOf<Instant>()) to force runtime lookup instead of the intrinsic one. Do not forget to add smth like // serializer<Instant>() TODO: uncomment in 2.1.20

@dkhalanskyjb
Copy link
Author

Something like this? JetBrains/kotlin#5412

@sandwwraith
Copy link
Member

Note that even after merging Kotlin PR, changes in this one are required, because compiler is not updated instantly

dkhalanskyjb added a commit to JetBrains/kotlin that referenced this pull request Mar 6, 2025
After moving `Instant` from `kotlinx.datetime` to
`kotlin.time`, we also need to preserve it having a default
`kotlinx.serialization` serializer.
See Kotlin/KEEP#387

The corresponding request in `kotlinx.serialization`:
Kotlin/kotlinx.serialization#2945
@dkhalanskyjb dkhalanskyjb requested a review from sandwwraith March 6, 2025 12:09
@dkhalanskyjb
Copy link
Author

Sure thing! Are there any other changes I should make in addition to that? For example, I copied the test from kotlinx-datetime directly, without trying to adjust its structure to that of surrounding tests, and I'm not sure that I've even put it in the right place.

KotlinBuild pushed a commit to JetBrains/kotlin that referenced this pull request Mar 6, 2025
…rializers

After moving `Instant` from `kotlinx.datetime` to
`kotlin.time`, we also need to preserve it having a default
`kotlinx.serialization` serializer.
See Kotlin/KEEP#387

The corresponding request in `kotlinx.serialization`:
Kotlin/kotlinx.serialization#2945

Additionally, fix a test that was not being run correctly.

Merge-request: KT-MR-20493
Merged-by: Dmitry Khalanskiy <[email protected]>
import kotlin.time.Instant

@ExperimentalTime
public object InstantComponentSerializer : KSerializer<Instant> {
Copy link
Member

Choose a reason for hiding this comment

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

Documentation is missing

@@ -251,6 +253,19 @@ public fun UShort.Companion.serializer(): KSerializer<UShort> = UShortSerializer
*/
public fun Duration.Companion.serializer(): KSerializer<Duration> = DurationSerializer

/**
* Returns serializer for [Instant].
* It is serialized as a string that represents an instant in the format described in ISO-8601-1:2019, 5.4.2.1b).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* It is serialized as a string that represents an instant in the format described in ISO-8601-1:2019, 5.4.2.1b).
* It is serialized as a string that represents an instant in the format same as the result of [Instant.toString] operation, described in ISO-8601-1:2019, 5.4.2.1b.

I think hardly anyone remembers what exactly is written in section 5.4.2.1b of a paid document

when (val index = decodeElementIndex(descriptor)) {
0 -> epochSeconds = decodeLongElement(descriptor, 0)
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1)
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
Copy link
Member

Choose a reason for hiding this comment

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

This ticket is said to be fixed in Kotlin 1.4.30

Pair(Instant.fromEpochSeconds(987654321, 0),
"\"2001-04-19T04:25:21Z\""),
)) {
assertEquals(json, Json.encodeToString(serializer, instant))
Copy link
Member

Choose a reason for hiding this comment

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

You can use assertJsonFormAndRestored test helper function as in e.g. here:

assertJsonFormAndRestored(Uuid.serializer(), uuid, "\"$uuid\"")

Copy link
Member

Choose a reason for hiding this comment

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

It would check that all Json flavors (string, inputStream, JsonElement) behave identically

val sb = StringBuilder()
val out = KeyValueOutput(sb)

val instant = Instant.parse("2020-12-09T09:16:56.000124Z")
Copy link
Member

Choose a reason for hiding this comment

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

Generally, having Json string tests is enough, but if you think these are necessary as well, you can keep them.

@dkhalanskyjb dkhalanskyjb requested a review from sandwwraith March 7, 2025 11:12
@dkhalanskyjb dkhalanskyjb force-pushed the dkhalanskyjb/instant-serializers branch from c28031f to 89a6f95 Compare March 7, 2025 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants