@@ -45,14 +45,21 @@ class InstantSerializationTest: JsonTestBase() {
4545 Pair (Instant .fromEpochSeconds(987654321 , 123456789 ),
4646 " {\" epochSeconds\" :987654321,\" nanosecondsOfSecond\" :123456789}" ),
4747 Pair (Instant .fromEpochSeconds(987654321 , 0 ),
48- " {\" epochSeconds\" :987654321}" ),
48+ " {\" epochSeconds\" :987654321, \" nanosecondsOfSecond \" :0 }" ),
4949 )) {
5050 assertJsonFormAndRestored(serializer, instant, json)
5151 }
52- // check that having a `"nanosecondsOfSecond": 0` field doesn't break deserialization
52+ // by default, `nanosecondsOfSecond` is optional
53+ assertJsonFormAndRestored(serializer, Instant .fromEpochSeconds(987654321 , 0 ),
54+ " {\" epochSeconds\" :987654321}" , Json { })
55+ // having a `"nanosecondsOfSecond": 0` field doesn't break deserialization
5356 assertEquals(Instant .fromEpochSeconds(987654321 , 0 ),
5457 Json .decodeFromString(serializer,
5558 " {\" epochSeconds\" :987654321,\" nanosecondsOfSecond\" :0}" ))
59+ // as does not having a `"nanosecondsOfSecond"` field if `encodeDefaults` is true
60+ assertEquals(Instant .fromEpochSeconds(987654321 , 0 ),
61+ default.decodeFromString(serializer,
62+ " {\" epochSeconds\" :987654321}" ))
5663 // "epochSeconds" should always be present
5764 assertFailsWith<SerializationException > { Json .decodeFromString(serializer, " {}" ) }
5865 assertFailsWith<SerializationException > { Json .decodeFromString(serializer, " {\" nanosecondsOfSecond\" :3}" ) }
0 commit comments