Skip to content

Add kotlin.time.Instant serializers #2945

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

Open
wants to merge 8 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

@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]>
@@ -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
@dkhalanskyjb dkhalanskyjb force-pushed the dkhalanskyjb/instant-serializers branch from 89a6f95 to 7d5981e Compare April 2, 2025 14:59
@dkhalanskyjb dkhalanskyjb marked this pull request as ready for review April 2, 2025 14:59
Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

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

Don't forget to squash commits on merging

/**
* Returns serializer for [Instant].
* It is serialized as a string that represents an instant in the format used by [Instant.toString]
* and 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
* and described in ISO-8601-1:2019, 5.4.2.1b).
* and described in ISO-8601-1:2019, 5.4.2.1b.

Copy link
Author

Choose a reason for hiding this comment

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

The parenthesis is not a typo, it's there because the format is called b) in that part of the ISO document.

Copy link
Contributor

@pdvrieze pdvrieze left a comment

Choose a reason for hiding this comment

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

I've added some suggestions on augmentation/correctness.

else -> throw SerializationException("Unexpected index: $index")
}
}
if (epochSeconds == null) throw MissingFieldException(
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid boxing it may be better to either have a separate primitive value that records whether epochSeconds was seen, or have a "sentinel" value that is not a valid Instant (e.g. Long.MAX or Long.MIN)

Comment on lines +50 to +57
if (value.nanosecondsOfSecond != 0) {
encodeIntElement(descriptor, 1, value.nanosecondsOfSecond)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should have a call to shouldEncodeElementDefault as it is up to the format to decide whether or not to encode things (while this doesn't matter for Json it would for a fixed length format).

Copy link
Member

@sandwwraith sandwwraith Apr 10, 2025

Choose a reason for hiding this comment

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

It depends on whether we really want to include nanoseconds:0 in the output even with encodeDefaults = true.

Current behavior would be equal to having @EncodeDefault(NEVER) annotation on Instant.nanosecondsOfSecond

Copy link
Contributor

Choose a reason for hiding this comment

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

@sandwwraith The issue is for a fixed length format that requires all attributes to be serialized (for example Android's Parcelable). In such case there must be bits written to represent the default value. It is not possible to write a placeholder as any placeholder value could be a valid value, so either the actual default is written or a marker is written (changing the wire format to add a marker byte).

Copy link
Contributor

Choose a reason for hiding this comment

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

@sandwwraith I've played around a bit and added binary tests to the jsonTests in #2979 . This is based upon the EfficientBinaryFormat from the documentation (but cross platform), and for now only in the test code (it was not designed to be used as a production format). To make the tests actually pass it is absolutely necessary to return false to shouldEncodeElementDefault (and EncodeDefault(Mode.NEVER) is not valid for this format).

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure what the problem is. Descriptor says that nanosecondsOfSecond is optional, so there is no contradiction here. I would say that supporting @EncodeDefault(Mode.NEVER) is a reasonable expectation from any format, including binary. Deserializer would be also fine if format would never return 1 from the decodeElementIndex.

Copy link
Member

Choose a reason for hiding this comment

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

decoder must know whether or not nanosecondsOfSecond is written

Why? InstantComponentSerializer.deserialize() would insert 0 automatically if the decoder won't return index 1.
For the encoder/decoder, everything will happen as if this field never existed in the class.

I think you're confusing optionality support with coercion support, which is done via ElementMarker in protobuf and Json and indeed requires tracking of whether the value was read. However, it is necessary for non-optional values so nulls can be placed there automatically.

Copy link
Contributor

@pdvrieze pdvrieze Apr 15, 2025

Choose a reason for hiding this comment

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

@sandwwraith It will not decode 0, it will decode whatever bytes are following the epochSeconds as the format does not have the ability to know that those following bytes do not belong to the Instant.

What I am describing is a format that must be read sequentially and does not have the ability to inspect the bytestream to determine the next element (decodeElementIndex() will have to be strictly sequential: i.e. it returns sequentially: 0, 1, CompositeDecoder.DONE).

You are assuming that the decoder never returns 1 from decodeElementIndex(), but for non-self-describing formats there is nothing to indicate that the nanosecondsOfSecond is not present in the serialized form. Note at least parsing will possibly fail as serialization will attempt more bytes than present in the serialized form.

Copy link
Contributor

Choose a reason for hiding this comment

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

To show it breaks I have created another branch applying the binary test to the InstantSerializer:
https://github.com/pdvrieze/kotlinx.serialization/tree/pdvrieze/instant-serializers-with-binary-test

This test fails to decode because it tries to decode the nanosecondsOfSecond component.

Btw. I've also updated the branch underlying #2979. It now triggers more often (and the binary format has been rewritten to support out-of-order serialization). There were two sets of changes needed. The error message test needed to be adjusted to check the correct message. More interesting is that for JsonCustomSerializersTest all custom serializers needed to be fixed to call shouldEncodeElementDefault.

Copy link
Member

Choose a reason for hiding this comment

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

I see what you mean. Do you have any real-world examples of such formats? IIRC, even binary formats with schema (such as Protobuf or Avro) have some way of denoting the id of the next element in the stream (e.g., protoId).

Still, it would mean that such a format is unable to support @EncodeDefault(NEVER), which IMO limits its usability with kotlinx.serialization.

Besides, InstantComponentSerializer is not the default. I imagine that for such a format one can easily write InstantComponentWithMandatoryNanosecondsSerializer and use it instead. I highly doubt that we should sacrifice convenience for the sake of some hypothetical ultra-fast binary format.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are quite some cases (not supported by kotlinx.serialization). I have looked at some examples:

  • Java serialization (ObjectOutputStream) - when writing primitives no class is written, only the value
  • Android Parcelable - again just writes the values, not types where not required
  • FlatBuffers - It is aimed at access without parsing. The "Structs" type just groups the members.

It is also common in many binary file formats (e.g. PNG, ZIP) or binary network protocols (IP/TCP/TLS/HTTP2.0/dbus) - but even textual line separated formats (e.g. Advent of code data) would may not be self-describing. The EfficientBinaryFormat was actually taken from the documentation (and it has this property). The key factor is whether the format is self-describing (in other words, can be parsed without access to a schema).

The fix for this case would be where the format can influence behaviour (by default it returns false and constant folding should be able to easily optimize the call away):

if (value.nanosecondsOfSecond != 0 || shouldEncodeElementDefault(descriptor, 1)) {

As to @EncodeDefault(NEVER), I would say this has been implemented incorrectly (and possibly misnamed - AVOID would be better). The implementation would be better if shouldEncodeElementDefault were invoked in all cases, but this being an overload that passes the "never" (or avoid) value. The format could then return true if the storage is invalid when defaults are omitted. This approach is probably preferable over requiring a SerializationStrategy to somehow be able to produce default values on request (and certainly more binary compatible).

import kotlin.reflect.typeOf

@OptIn(ExperimentalTime::class)
class InstantSerializationTest: JsonTestBase() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be worthwhile to also test on another format that is not Json (or JsonLike)

Comment on lines +17 to +25
for ((instant, json) in listOf(
Pair(Instant.fromEpochSeconds(1607505416, 124000),
"\"2020-12-09T09:16:56.000124Z\""),
Pair(Instant.fromEpochSeconds(-1607505416, -124000),
"\"1919-01-23T14:43:03.999876Z\""),
Pair(Instant.fromEpochSeconds(987654321, 123456789),
"\"2001-04-19T04:25:21.123456789Z\""),
Pair(Instant.fromEpochSeconds(987654321, 0),
"\"2001-04-19T04:25:21Z\""),
Copy link
Contributor

Choose a reason for hiding this comment

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

You should include some values with non-utc timezones (especially ones with strange offsets like India). And include the fraction handling with millis, or less than 3 digits in the fraction of seconds.

@dkhalanskyjb dkhalanskyjb force-pushed the dkhalanskyjb/instant-serializers branch from 7d5981e to 7587980 Compare April 15, 2025 12:12
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.

4 participants