Skip to content

Commit 077b514

Browse files
committed
Make ISO serializers for LocalTime/LocalDateTime more predictable
Now, they will always include seconds and will not add trailing zeros for prettiness to the fractional part. Fixes #351
1 parent 9d59199 commit 077b514

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

core/common/src/serializers/LocalDateTimeSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public object LocalDateTimeIso8601Serializer: KSerializer<LocalDateTime> {
2727
LocalDateTime.parse(decoder.decodeString())
2828

2929
override fun serialize(encoder: Encoder, value: LocalDateTime) {
30-
encoder.encodeString(value.toString())
30+
encoder.encodeString(LocalDateTime.Formats.ISO.format(value))
3131
}
3232

3333
}

core/common/src/serializers/LocalTimeSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public object LocalTimeIso8601Serializer : KSerializer<LocalTime> {
2727
LocalTime.parse(decoder.decodeString())
2828

2929
override fun serialize(encoder: Encoder, value: LocalTime) {
30-
encoder.encodeString(value.toString())
30+
encoder.encodeString(LocalTime.Formats.ISO.format(value))
3131
}
3232
}
3333

serialization/common/test/LocalDateTimeSerializationTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import kotlin.test.*
1515
class LocalDateTimeSerializationTest {
1616
private fun iso8601Serialization(serializer: KSerializer<LocalDateTime>) {
1717
for ((localDateTime, json) in listOf(
18-
Pair(LocalDateTime(2008, 7, 5, 2, 1), "\"2008-07-05T02:01\""),
18+
Pair(LocalDateTime(2008, 7, 5, 2, 1), "\"2008-07-05T02:01:00\""),
1919
Pair(LocalDateTime(2007, 12, 31, 23, 59, 1), "\"2007-12-31T23:59:01\""),
20-
Pair(LocalDateTime(999, 12, 31, 23, 59, 59, 990000000), "\"0999-12-31T23:59:59.990\""),
21-
Pair(LocalDateTime(-1, 1, 2, 23, 59, 59, 999990000), "\"-0001-01-02T23:59:59.999990\""),
22-
Pair(LocalDateTime(-2008, 1, 2, 23, 59, 59, 999999990), "\"-2008-01-02T23:59:59.999999990\""),
20+
Pair(LocalDateTime(999, 12, 31, 23, 59, 59, 990000000), "\"0999-12-31T23:59:59.99\""),
21+
Pair(LocalDateTime(-1, 1, 2, 23, 59, 59, 999990000), "\"-0001-01-02T23:59:59.99999\""),
22+
Pair(LocalDateTime(-2008, 1, 2, 23, 59, 59, 999999990), "\"-2008-01-02T23:59:59.99999999\""),
2323
)) {
2424
assertEquals(json, Json.encodeToString(serializer, localDateTime))
2525
assertEquals(localDateTime, Json.decodeFromString(serializer, json))

serialization/common/test/LocalTimeSerializationTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import kotlin.test.*
1515
class LocalTimeSerializationTest {
1616
private fun iso8601Serialization(serializer: KSerializer<LocalTime>) {
1717
for ((localTime, json) in listOf(
18-
Pair(LocalTime(2, 1), "\"02:01\""),
18+
Pair(LocalTime(2, 1), "\"02:01:00\""),
1919
Pair(LocalTime(23, 59, 1), "\"23:59:01\""),
20-
Pair(LocalTime(23, 59, 59, 990000000), "\"23:59:59.990\""),
21-
Pair(LocalTime(23, 59, 59, 999990000), "\"23:59:59.999990\""),
22-
Pair(LocalTime(23, 59, 59, 999999990), "\"23:59:59.999999990\""),
20+
Pair(LocalTime(23, 59, 59, 990000000), "\"23:59:59.99\""),
21+
Pair(LocalTime(23, 59, 59, 999990000), "\"23:59:59.99999\""),
22+
Pair(LocalTime(23, 59, 59, 999999990), "\"23:59:59.99999999\""),
2323
)) {
2424
assertEquals(json, Json.encodeToString(serializer, localTime))
2525
assertEquals(localTime, Json.decodeFromString(serializer, json))

0 commit comments

Comments
 (0)