-
Notifications
You must be signed in to change notification settings - Fork 641
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?
Changes from all commits
288f73c
4a0530d
dd9909f
eea8b48
9030611
22a5509
973a6e8
7587980
5efb20f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2025-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.builtins | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlin.time.ExperimentalTime | ||
import kotlin.time.Instant | ||
|
||
/** | ||
* Serializer that encodes and decodes [Instant] as its second and nanosecond components of the Unix time. | ||
* | ||
* JSON example: `{"epochSeconds":1607505416,"nanosecondsOfSecond":124000}`. | ||
*/ | ||
@ExperimentalTime | ||
public object InstantComponentSerializer : KSerializer<Instant> { | ||
sandwwraith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override val descriptor: SerialDescriptor = | ||
buildClassSerialDescriptor("kotlinx.serialization.InstantComponentSerializer") { | ||
element<Long>("epochSeconds") | ||
element<Long>("nanosecondsOfSecond", isOptional = true) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
override fun deserialize(decoder: Decoder): Instant = | ||
decoder.decodeStructure(descriptor) { | ||
var epochSecondsNotSeen = true | ||
var epochSeconds: Long = 0 | ||
var nanosecondsOfSecond = 0 | ||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> { | ||
epochSecondsNotSeen = false | ||
epochSeconds = decodeLongElement(descriptor, 0) | ||
} | ||
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1) | ||
CompositeDecoder.DECODE_DONE -> break | ||
else -> throw SerializationException("Unexpected index: $index") | ||
} | ||
} | ||
if (epochSecondsNotSeen) throw MissingFieldException( | ||
missingField = "epochSeconds", | ||
serialName = descriptor.serialName | ||
) | ||
Instant.fromEpochSeconds(epochSeconds, nanosecondsOfSecond) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
override fun serialize(encoder: Encoder, value: Instant) { | ||
encoder.encodeStructure(descriptor) { | ||
encodeLongElement(descriptor, 0, value.epochSeconds) | ||
if (value.nanosecondsOfSecond != 0 || shouldEncodeElementDefault(descriptor, 1)) { | ||
encodeIntElement(descriptor, 1, value.nanosecondsOfSecond) | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2025-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.builtins.* | ||
import kotlin.time.* | ||
import kotlin.test.* | ||
import kotlin.reflect.typeOf | ||
|
||
@OptIn(ExperimentalTime::class) | ||
class InstantSerializationTest: JsonTestBase() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
private fun iso8601Serialization(serializer: KSerializer<Instant>) { | ||
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\""), | ||
Comment on lines
+17
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
)) { | ||
assertJsonFormAndRestored(serializer, instant, json) | ||
} | ||
for ((instant, json) in listOf( | ||
Pair(Instant.fromEpochSeconds(987654321, 123456789), | ||
"\"2001-04-19T07:55:21.123456789+03:30\""), | ||
Pair(Instant.fromEpochSeconds(987654321, 123456789), | ||
"\"2001-04-19T00:55:21.123456789-03:30\""), | ||
)) { | ||
assertRestoredFromJsonForm(serializer, json, instant) | ||
} | ||
} | ||
|
||
private fun componentSerialization(serializer: KSerializer<Instant>) { | ||
for ((instant, json) in listOf( | ||
Pair(Instant.fromEpochSeconds(1607505416, 124000), | ||
"{\"epochSeconds\":1607505416,\"nanosecondsOfSecond\":124000}"), | ||
Pair(Instant.fromEpochSeconds(-1607505416, -124000), | ||
"{\"epochSeconds\":-1607505417,\"nanosecondsOfSecond\":999876000}"), | ||
Pair(Instant.fromEpochSeconds(987654321, 123456789), | ||
"{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":123456789}"), | ||
Pair(Instant.fromEpochSeconds(987654321, 0), | ||
"{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":0}"), | ||
)) { | ||
assertJsonFormAndRestored(serializer, instant, json) | ||
} | ||
// by default, `nanosecondsOfSecond` is optional | ||
assertJsonFormAndRestored(serializer, Instant.fromEpochSeconds(987654321, 0), | ||
"{\"epochSeconds\":987654321}", Json { }) | ||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// 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}") } | ||
} | ||
|
||
@Test | ||
fun testIso8601Serialization() { | ||
iso8601Serialization(Instant.serializer()) | ||
} | ||
|
||
@Test | ||
fun testComponentSerialization() { | ||
componentSerialization(InstantComponentSerializer) | ||
} | ||
|
||
@Test | ||
fun testDefaultSerializers() { | ||
// should be the same as the ISO 8601 | ||
@Suppress("UNCHECKED_CAST") | ||
iso8601Serialization(serializer(typeOf<Instant>()) as KSerializer<Instant>) | ||
// iso8601Serialization(serializer()) TODO: uncomment when the compiler adds KT-75759 | ||
} | ||
} |
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 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.