|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.serialization |
| 6 | + |
| 7 | +import kotlinx.serialization.* |
| 8 | +import kotlinx.serialization.json.* |
| 9 | +import kotlinx.serialization.builtins.* |
| 10 | +import kotlin.time.* |
| 11 | +import kotlin.test.* |
| 12 | + |
| 13 | +@OptIn(ExperimentalTime::class) |
| 14 | +class InstantSerializationTest { |
| 15 | + private fun iso8601Serialization(serializer: KSerializer<Instant>) { |
| 16 | + for ((instant, json) in listOf( |
| 17 | + Pair(Instant.fromEpochSeconds(1607505416, 124000), |
| 18 | + "\"2020-12-09T09:16:56.000124Z\""), |
| 19 | + Pair(Instant.fromEpochSeconds(-1607505416, -124000), |
| 20 | + "\"1919-01-23T14:43:03.999876Z\""), |
| 21 | + Pair(Instant.fromEpochSeconds(987654321, 123456789), |
| 22 | + "\"2001-04-19T04:25:21.123456789Z\""), |
| 23 | + Pair(Instant.fromEpochSeconds(987654321, 0), |
| 24 | + "\"2001-04-19T04:25:21Z\""), |
| 25 | + )) { |
| 26 | + assertEquals(json, Json.encodeToString(serializer, instant)) |
| 27 | + assertEquals(instant, Json.decodeFromString(serializer, json)) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private fun componentSerialization(serializer: KSerializer<Instant>) { |
| 32 | + for ((instant, json) in listOf( |
| 33 | + Pair(Instant.fromEpochSeconds(1607505416, 124000), |
| 34 | + "{\"epochSeconds\":1607505416,\"nanosecondsOfSecond\":124000}"), |
| 35 | + Pair(Instant.fromEpochSeconds(-1607505416, -124000), |
| 36 | + "{\"epochSeconds\":-1607505417,\"nanosecondsOfSecond\":999876000}"), |
| 37 | + Pair(Instant.fromEpochSeconds(987654321, 123456789), |
| 38 | + "{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":123456789}"), |
| 39 | + Pair(Instant.fromEpochSeconds(987654321, 0), |
| 40 | + "{\"epochSeconds\":987654321}"), |
| 41 | + )) { |
| 42 | + assertEquals(json, Json.encodeToString(serializer, instant)) |
| 43 | + assertEquals(instant, Json.decodeFromString(serializer, json)) |
| 44 | + } |
| 45 | + // check that having a `"nanosecondsOfSecond": 0` field doesn't break deserialization |
| 46 | + assertEquals(Instant.fromEpochSeconds(987654321, 0), |
| 47 | + Json.decodeFromString(serializer, |
| 48 | + "{\"epochSeconds\":987654321,\"nanosecondsOfSecond\":0}")) |
| 49 | + // "epochSeconds" should always be present |
| 50 | + assertFailsWith<SerializationException> { Json.decodeFromString(serializer, "{}") } |
| 51 | + assertFailsWith<SerializationException> { Json.decodeFromString(serializer, "{\"nanosecondsOfSecond\":3}") } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun testIso8601Serialization() { |
| 56 | + iso8601Serialization(Instant.serializer()) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun testComponentSerialization() { |
| 61 | + componentSerialization(InstantComponentSerializer) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testDefaultSerializers() { |
| 66 | + // should be the same as the ISO 8601 |
| 67 | + iso8601Serialization(Json.serializersModule.serializer()) |
| 68 | + } |
| 69 | +} |
0 commit comments