Skip to content

Commit

Permalink
Make ISO serializers for LocalTime/LocalDateTime more predictable
Browse files Browse the repository at this point in the history
Now, they will always include seconds and will not add trailing
zeros for prettiness to the fractional part.

Fixes #351
  • Loading branch information
dkhalanskyjb committed Apr 17, 2024
1 parent 9d59199 commit 077b514
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/common/src/serializers/LocalDateTimeSerializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public object LocalDateTimeIso8601Serializer: KSerializer<LocalDateTime> {
LocalDateTime.parse(decoder.decodeString())

override fun serialize(encoder: Encoder, value: LocalDateTime) {
encoder.encodeString(value.toString())
encoder.encodeString(LocalDateTime.Formats.ISO.format(value))
}

}
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/serializers/LocalTimeSerializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public object LocalTimeIso8601Serializer : KSerializer<LocalTime> {
LocalTime.parse(decoder.decodeString())

override fun serialize(encoder: Encoder, value: LocalTime) {
encoder.encodeString(value.toString())
encoder.encodeString(LocalTime.Formats.ISO.format(value))
}
}

Expand Down
8 changes: 4 additions & 4 deletions serialization/common/test/LocalDateTimeSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import kotlin.test.*
class LocalDateTimeSerializationTest {
private fun iso8601Serialization(serializer: KSerializer<LocalDateTime>) {
for ((localDateTime, json) in listOf(
Pair(LocalDateTime(2008, 7, 5, 2, 1), "\"2008-07-05T02:01\""),
Pair(LocalDateTime(2008, 7, 5, 2, 1), "\"2008-07-05T02:01:00\""),
Pair(LocalDateTime(2007, 12, 31, 23, 59, 1), "\"2007-12-31T23:59:01\""),
Pair(LocalDateTime(999, 12, 31, 23, 59, 59, 990000000), "\"0999-12-31T23:59:59.990\""),
Pair(LocalDateTime(-1, 1, 2, 23, 59, 59, 999990000), "\"-0001-01-02T23:59:59.999990\""),
Pair(LocalDateTime(-2008, 1, 2, 23, 59, 59, 999999990), "\"-2008-01-02T23:59:59.999999990\""),
Pair(LocalDateTime(999, 12, 31, 23, 59, 59, 990000000), "\"0999-12-31T23:59:59.99\""),
Pair(LocalDateTime(-1, 1, 2, 23, 59, 59, 999990000), "\"-0001-01-02T23:59:59.99999\""),
Pair(LocalDateTime(-2008, 1, 2, 23, 59, 59, 999999990), "\"-2008-01-02T23:59:59.99999999\""),
)) {
assertEquals(json, Json.encodeToString(serializer, localDateTime))
assertEquals(localDateTime, Json.decodeFromString(serializer, json))
Expand Down
8 changes: 4 additions & 4 deletions serialization/common/test/LocalTimeSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import kotlin.test.*
class LocalTimeSerializationTest {
private fun iso8601Serialization(serializer: KSerializer<LocalTime>) {
for ((localTime, json) in listOf(
Pair(LocalTime(2, 1), "\"02:01\""),
Pair(LocalTime(2, 1), "\"02:01:00\""),
Pair(LocalTime(23, 59, 1), "\"23:59:01\""),
Pair(LocalTime(23, 59, 59, 990000000), "\"23:59:59.990\""),
Pair(LocalTime(23, 59, 59, 999990000), "\"23:59:59.999990\""),
Pair(LocalTime(23, 59, 59, 999999990), "\"23:59:59.999999990\""),
Pair(LocalTime(23, 59, 59, 990000000), "\"23:59:59.99\""),
Pair(LocalTime(23, 59, 59, 999990000), "\"23:59:59.99999\""),
Pair(LocalTime(23, 59, 59, 999999990), "\"23:59:59.99999999\""),
)) {
assertEquals(json, Json.encodeToString(serializer, localTime))
assertEquals(localTime, Json.decodeFromString(serializer, json))
Expand Down

0 comments on commit 077b514

Please sign in to comment.