-
Notifications
You must be signed in to change notification settings - Fork 635
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
dkhalanskyjb
wants to merge
6
commits into
dev
Choose a base branch
from
dkhalanskyjb/instant-serializers
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−7
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
88b69d5
Add kotlin.time.Instant serializers
dkhalanskyjb 5b3515e
Move the tests for the Instant serializers
dkhalanskyjb 5c196df
Improve the documentation
dkhalanskyjb 05a1b33
Remove an old workaround for the JS compiler bug
dkhalanskyjb b741692
Remove an excessive test
dkhalanskyjb 89a6f95
Revork the Instant serializers tests
dkhalanskyjb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -890,6 +890,14 @@ sealed class kotlinx.serialization.modules/SerializersModule { // kotlinx.serial | |
final fun <#A1: kotlin/Any> getContextual(kotlin.reflect/KClass<#A1>): kotlinx.serialization/KSerializer<#A1>? // kotlinx.serialization.modules/SerializersModule.getContextual|getContextual(kotlin.reflect.KClass<0:0>){0§<kotlin.Any>}[0] | ||
} | ||
|
||
final object kotlinx.serialization.builtins/InstantComponentSerializer : kotlinx.serialization/KSerializer<kotlin.time/Instant> { // kotlinx.serialization.builtins/InstantComponentSerializer|null[0] | ||
final val descriptor // kotlinx.serialization.builtins/InstantComponentSerializer.descriptor|{}descriptor[0] | ||
final fun <get-descriptor>(): kotlinx.serialization.descriptors/SerialDescriptor // kotlinx.serialization.builtins/InstantComponentSerializer.descriptor.<get-descriptor>|<get-descriptor>(){}[0] | ||
|
||
final fun deserialize(kotlinx.serialization.encoding/Decoder): kotlin.time/Instant // kotlinx.serialization.builtins/InstantComponentSerializer.deserialize|deserialize(kotlinx.serialization.encoding.Decoder){}[0] | ||
final fun serialize(kotlinx.serialization.encoding/Encoder, kotlin.time/Instant) // kotlinx.serialization.builtins/InstantComponentSerializer.serialize|serialize(kotlinx.serialization.encoding.Encoder;kotlin.time.Instant){}[0] | ||
} | ||
|
||
final object kotlinx.serialization.builtins/LongAsStringSerializer : kotlinx.serialization/KSerializer<kotlin/Long> { // kotlinx.serialization.builtins/LongAsStringSerializer|null[0] | ||
final val descriptor // kotlinx.serialization.builtins/LongAsStringSerializer.descriptor|{}descriptor[0] | ||
final fun <get-descriptor>(): kotlinx.serialization.descriptors/SerialDescriptor // kotlinx.serialization.builtins/LongAsStringSerializer.descriptor.<get-descriptor>|<get-descriptor>(){}[0] | ||
|
@@ -956,6 +964,14 @@ final object kotlinx.serialization.internal/FloatSerializer : kotlinx.serializat | |
final fun serialize(kotlinx.serialization.encoding/Encoder, kotlin/Float) // kotlinx.serialization.internal/FloatSerializer.serialize|serialize(kotlinx.serialization.encoding.Encoder;kotlin.Float){}[0] | ||
} | ||
|
||
final object kotlinx.serialization.internal/InstantSerializer : kotlinx.serialization/KSerializer<kotlin.time/Instant> { // kotlinx.serialization.internal/InstantSerializer|null[0] | ||
final val descriptor // kotlinx.serialization.internal/InstantSerializer.descriptor|{}descriptor[0] | ||
final fun <get-descriptor>(): kotlinx.serialization.descriptors/SerialDescriptor // kotlinx.serialization.internal/InstantSerializer.descriptor.<get-descriptor>|<get-descriptor>(){}[0] | ||
|
||
final fun deserialize(kotlinx.serialization.encoding/Decoder): kotlin.time/Instant // kotlinx.serialization.internal/InstantSerializer.deserialize|deserialize(kotlinx.serialization.encoding.Decoder){}[0] | ||
final fun serialize(kotlinx.serialization.encoding/Encoder, kotlin.time/Instant) // kotlinx.serialization.internal/InstantSerializer.serialize|serialize(kotlinx.serialization.encoding.Encoder;kotlin.time.Instant){}[0] | ||
} | ||
|
||
final object kotlinx.serialization.internal/IntArraySerializer : kotlinx.serialization.internal/PrimitiveArraySerializer<kotlin/Int, kotlin/IntArray, kotlinx.serialization.internal/IntArrayBuilder>, kotlinx.serialization/KSerializer<kotlin/IntArray> // kotlinx.serialization.internal/IntArraySerializer|null[0] | ||
|
||
final object kotlinx.serialization.internal/IntSerializer : kotlinx.serialization/KSerializer<kotlin/Int> { // kotlinx.serialization.internal/IntSerializer|null[0] | ||
|
@@ -1074,6 +1090,7 @@ final val kotlinx.serialization.modules/EmptySerializersModule // kotlinx.serial | |
final fun <get-EmptySerializersModule>(): kotlinx.serialization.modules/SerializersModule // kotlinx.serialization.modules/EmptySerializersModule.<get-EmptySerializersModule>|<get-EmptySerializersModule>(){}[0] | ||
|
||
final fun (kotlin.time/Duration.Companion).kotlinx.serialization.builtins/serializer(): kotlinx.serialization/KSerializer<kotlin.time/Duration> // kotlinx.serialization.builtins/serializer|[email protected](){}[0] | ||
final fun (kotlin.time/Instant.Companion).kotlinx.serialization.builtins/serializer(): kotlinx.serialization/KSerializer<kotlin.time/Instant> // kotlinx.serialization.builtins/serializer|[email protected](){}[0] | ||
final fun (kotlin.uuid/Uuid.Companion).kotlinx.serialization.builtins/serializer(): kotlinx.serialization/KSerializer<kotlin.uuid/Uuid> // kotlinx.serialization.builtins/serializer|[email protected](){}[0] | ||
final fun (kotlin/Boolean.Companion).kotlinx.serialization.builtins/serializer(): kotlinx.serialization/KSerializer<kotlin/Boolean> // kotlinx.serialization.builtins/serializer|[email protected](){}[0] | ||
final fun (kotlin/Byte.Companion).kotlinx.serialization.builtins/serializer(): kotlinx.serialization/KSerializer<kotlin/Byte> // kotlinx.serialization.builtins/serializer|[email protected](){}[0] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/commonMain/src/kotlinx/serialization/builtins/InstantComponentSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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> { | ||
|
||
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 epochSeconds: Long? = null | ||
var nanosecondsOfSecond = 0 | ||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> epochSeconds = decodeLongElement(descriptor, 0) | ||
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1) | ||
CompositeDecoder.DECODE_DONE -> break | ||
else -> throw SerializationException("Unexpected index: $index") | ||
} | ||
} | ||
if (epochSeconds == null) throw MissingFieldException( | ||
missingField = "epochSeconds", | ||
serialName = descriptor.serialName | ||
) | ||
Instant.fromEpochSeconds(epochSeconds, nanosecondsOfSecond) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: Instant) { | ||
encoder.encodeStructure(descriptor) { | ||
encodeLongElement(descriptor, 0, value.epochSeconds) | ||
if (value.nanosecondsOfSecond != 0) { | ||
encodeIntElement(descriptor, 1, value.nanosecondsOfSecond) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
formats/json-tests/commonTest/src/kotlinx/serialization/InstantSerializationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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() { | ||
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\""), | ||
)) { | ||
assertJsonFormAndRestored(serializer, instant, json) | ||
} | ||
} | ||
|
||
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}"), | ||
)) { | ||
assertJsonFormAndRestored(serializer, instant, json) | ||
} | ||
// check that having a `"nanosecondsOfSecond": 0` field doesn't break deserialization | ||
assertEquals(Instant.fromEpochSeconds(987654321, 0), | ||
Json.decodeFromString(serializer, | ||
"{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":0}")) | ||
// "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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[versions] | ||
kotlin = "2.1.0" | ||
kotlin = "2.1.20-RC" | ||
kover = "0.8.2" | ||
dokka = "2.0.0-Beta" | ||
knit = "0.5.0" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Documentation is missing