-
Notifications
You must be signed in to change notification settings - Fork 642
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
base: dev
Are you sure you want to change the base?
Conversation
For |
For now, you can use |
Something like this? JetBrains/kotlin#5412 |
Note that even after merging Kotlin PR, changes in this one are required, because compiler is not updated instantly |
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
Sure thing! Are there any other changes I should make in addition to that? For example, I copied the test from |
…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]>
core/commonMain/src/kotlinx/serialization/builtins/InstantComponentSerializer.kt
Show resolved
Hide resolved
@@ -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). |
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.
* 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 |
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.
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)) |
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.
You can use assertJsonFormAndRestored
test helper function as in e.g. here:
kotlinx.serialization/formats/json-tests/commonTest/src/kotlinx/serialization/features/UuidTest.kt
Line 18 in fa797bc
assertJsonFormAndRestored(Uuid.serializer(), uuid, "\"$uuid\"") |
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.
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") |
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.
Generally, having Json string tests is enough, but if you think these are necessary as well, you can keep them.
c28031f
to
89a6f95
Compare
89a6f95
to
7d5981e
Compare
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.
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). |
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.
* and described in ISO-8601-1:2019, 5.4.2.1b). | |
* and described in ISO-8601-1:2019, 5.4.2.1b. |
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.
The parenthesis is not a typo, it's there because the format is called b)
in that part of the ISO document.
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.
I've added some suggestions on augmentation/correctness.
else -> throw SerializationException("Unexpected index: $index") | ||
} | ||
} | ||
if (epochSeconds == null) throw MissingFieldException( |
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.
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
)
if (value.nanosecondsOfSecond != 0) { | ||
encodeIntElement(descriptor, 1, value.nanosecondsOfSecond) | ||
} |
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.
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).
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.
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
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.
@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).
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.
@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).
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.
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
.
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.
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 null
s can be placed there automatically.
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.
@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.
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.
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
.
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.
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.
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.
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() { |
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.
It may be worthwhile to also test on another format that is not Json
(or JsonLike
)
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\""), |
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.
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.
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
7d5981e
to
7587980
Compare
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