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 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public object InstantComponentSerializer : KSerializer<Instant> {
override fun serialize(encoder: Encoder, value: Instant) {
encoder.encodeStructure(descriptor) {
encodeLongElement(descriptor, 0, value.epochSeconds)
if (value.nanosecondsOfSecond != 0) {
if (value.nanosecondsOfSecond != 0 || shouldEncodeElementDefault(descriptor, 1)) {
encodeIntElement(descriptor, 1, value.nanosecondsOfSecond)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ class InstantSerializationTest: JsonTestBase() {
Pair(Instant.fromEpochSeconds(987654321, 123456789),
"{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":123456789}"),
Pair(Instant.fromEpochSeconds(987654321, 0),
"{\"epochSeconds\":987654321}"),
"{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":0}"),
)) {
assertJsonFormAndRestored(serializer, instant, json)
}
// check that having a `"nanosecondsOfSecond": 0` field doesn't break deserialization
// by default, `nanosecondsOfSecond` is optional
assertJsonFormAndRestored(serializer, Instant.fromEpochSeconds(987654321, 0),
"{\"epochSeconds\":987654321}", Json { })
Comment on lines +52 to +54
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment might clarify that the default test json instance has encoding defaults enabled. the nanosecondsOfSecond element is always optional, but the format decides whether to serialize the default value. You might replace Json {} with Json(default) { encodeDefaults = false } to both take in the remainder of the default test configuration and communicate that it set to false here.

// having a `"nanosecondsOfSecond": 0` field doesn't break deserialization
assertEquals(Instant.fromEpochSeconds(987654321, 0),
Json.decodeFromString(serializer,
"{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":0}"))
// as does not having a `"nanosecondsOfSecond"` field if `encodeDefaults` is true
assertEquals(Instant.fromEpochSeconds(987654321, 0),
default.decodeFromString(serializer,
"{\"epochSeconds\":987654321}"))
// "epochSeconds" should always be present
assertFailsWith<SerializationException> { Json.decodeFromString(serializer, "{}") }
assertFailsWith<SerializationException> { Json.decodeFromString(serializer, "{\"nanosecondsOfSecond\":3}") }
Expand Down