@@ -45,14 +45,21 @@ class InstantSerializationTest: JsonTestBase() {
45
45
Pair (Instant .fromEpochSeconds(987654321 , 123456789 ),
46
46
" {\" epochSeconds\" :987654321,\" nanosecondsOfSecond\" :123456789}" ),
47
47
Pair (Instant .fromEpochSeconds(987654321 , 0 ),
48
- " {\" epochSeconds\" :987654321}" ),
48
+ " {\" epochSeconds\" :987654321, \" nanosecondsOfSecond \" :0 }" ),
49
49
)) {
50
50
assertJsonFormAndRestored(serializer, instant, json)
51
51
}
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
53
56
assertEquals(Instant .fromEpochSeconds(987654321 , 0 ),
54
57
Json .decodeFromString(serializer,
55
58
" {\" 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}" ))
56
63
// "epochSeconds" should always be present
57
64
assertFailsWith<SerializationException > { Json .decodeFromString(serializer, " {}" ) }
58
65
assertFailsWith<SerializationException > { Json .decodeFromString(serializer, " {\" nanosecondsOfSecond\" :3}" ) }
0 commit comments