Skip to content

Commit 7587980

Browse files
committed
Address the review
1 parent 973a6e8 commit 7587980

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

core/commonMain/src/kotlinx/serialization/builtins/InstantComponentSerializer.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,28 @@ public object InstantComponentSerializer : KSerializer<Instant> {
2727
@OptIn(ExperimentalSerializationApi::class)
2828
override fun deserialize(decoder: Decoder): Instant =
2929
decoder.decodeStructure(descriptor) {
30-
var epochSeconds: Long? = null
30+
var epochSecondsNotSeen = true
31+
var epochSeconds: Long = 0
3132
var nanosecondsOfSecond = 0
3233
while (true) {
3334
when (val index = decodeElementIndex(descriptor)) {
34-
0 -> epochSeconds = decodeLongElement(descriptor, 0)
35+
0 -> {
36+
epochSecondsNotSeen = false
37+
epochSeconds = decodeLongElement(descriptor, 0)
38+
}
3539
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1)
3640
CompositeDecoder.DECODE_DONE -> break
3741
else -> throw SerializationException("Unexpected index: $index")
3842
}
3943
}
40-
if (epochSeconds == null) throw MissingFieldException(
44+
if (epochSecondsNotSeen) throw MissingFieldException(
4145
missingField = "epochSeconds",
4246
serialName = descriptor.serialName
4347
)
4448
Instant.fromEpochSeconds(epochSeconds, nanosecondsOfSecond)
4549
}
4650

51+
@OptIn(ExperimentalSerializationApi::class)
4752
override fun serialize(encoder: Encoder, value: Instant) {
4853
encoder.encodeStructure(descriptor) {
4954
encodeLongElement(descriptor, 0, value.epochSeconds)

formats/json-tests/commonTest/src/kotlinx/serialization/InstantSerializationTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class InstantSerializationTest: JsonTestBase() {
2626
)) {
2727
assertJsonFormAndRestored(serializer, instant, json)
2828
}
29+
for ((instant, json) in listOf(
30+
Pair(Instant.fromEpochSeconds(987654321, 123456789),
31+
"\"2001-04-19T07:55:21.123456789+03:30\""),
32+
Pair(Instant.fromEpochSeconds(987654321, 123456789),
33+
"\"2001-04-19T00:55:21.123456789-03:30\""),
34+
)) {
35+
assertRestoredFromJsonForm(serializer, json, instant)
36+
}
2937
}
3038

3139
private fun componentSerialization(serializer: KSerializer<Instant>) {

formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonTestBase.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,15 @@ abstract class JsonTestBase {
190190
assertTrue("Failed with streaming = $jsonTestingMode\n\tsource value =$data\n\tdeserialized value=$deserialized") { check(data, deserialized) }
191191
}
192192
}
193+
194+
internal fun <T> assertRestoredFromJsonForm(
195+
serializer: KSerializer<T>,
196+
jsonForm: String,
197+
expected: T,
198+
) {
199+
parametrizedTest { jsonTestingMode ->
200+
val deserialized: T = Json.decodeFromString(serializer, jsonForm, jsonTestingMode)
201+
assertEquals(expected, deserialized, "Failed with streaming = $jsonTestingMode")
202+
}
203+
}
193204
}

0 commit comments

Comments
 (0)