You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
for more fine-grained control over JsonBuilder.ignoreUnknownKeys setting.
Fixes#1420
Also, improve error message and path handling when an 'Unknown key' exception is thrown.
Fixes#2869Fixes#2637
Copy file name to clipboardExpand all lines: docs/basic-serialization.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -411,8 +411,8 @@ Attempts to explicitly specify its value in the serial format, even if the speci
411
411
value is equal to the default one, produces the following exception.
412
412
413
413
```text
414
-
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 42: Encountered an unknown key 'language' at path: $.name
415
-
Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
414
+
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Encountered an unknown key 'language' at offset 42 at path: $
415
+
Use 'ignoreUnknownKeys = true' in 'Json {}' builder or '@JsonIgnoreUnknownKeys' annotation to ignore unknown keys.
Sometimes, for cleaner and safer API, it is desirable to ignore unknown properties only for specific classes.
171
+
In that case, you can use [JsonIgnoreUnknownKeys] annotation on such classes while leaving global [ignoreUnknownKeys][JsonBuilder.ignoreUnknownKeys] setting
172
+
turned off:
173
+
174
+
```kotlin
175
+
@OptIn(ExperimentalSerializationApi::class) // JsonIgnoreUnknownKeys is an experimental annotation for now
> You can get the full code [here](../guide/example/example-json-04.kt).
193
+
194
+
Line (1) decodes successfully despite "unknownKey" in `Outer`, because annotation is present on the class.
195
+
However, line (2) throws `SerializationException` because there is no "unknownKey" property in `Inner`:
196
+
197
+
```text
198
+
Outer(a=1, inner=Inner(x=value))
199
+
200
+
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Encountered an unknown key 'unknownKey' at offset 29 at path: $.inner
201
+
Use 'ignoreUnknownKeys = true' in 'Json {}' builder or '@JsonIgnoreUnknownKeys' annotation to ignore unknown keys.
202
+
```
203
+
204
+
<!--- TEST LINES_START-->
205
+
167
206
### Alternative Json names
168
207
169
208
It's not a rare case when JSON fields are renamed due to a schema version change.
@@ -184,7 +223,7 @@ fun main() {
184
223
}
185
224
```
186
225
187
-
> You can get the full code [here](../guide/example/example-json-04.kt).
226
+
> You can get the full code [here](../guide/example/example-json-05.kt).
188
227
189
228
As you can see, both `name` and `title` Json fields correspond to `name` property:
190
229
@@ -222,7 +261,7 @@ fun main() {
222
261
}
223
262
```
224
263
225
-
> You can get the full code [here](../guide/example/example-json-05.kt).
264
+
> You can get the full code [here](../guide/example/example-json-06.kt).
226
265
227
266
It produces the following output which encodes all the property values including the default ones:
228
267
@@ -261,7 +300,7 @@ fun main() {
261
300
}
262
301
```
263
302
264
-
> You can get the full code [here](../guide/example/example-json-06.kt).
303
+
> You can get the full code [here](../guide/example/example-json-07.kt).
265
304
266
305
As you can see, `version`, `website` and `description` fields are not present in output JSON on the first line.
267
306
After decoding, the missing nullable property `website` without a default values has received a `null` value,
@@ -319,7 +358,7 @@ fun main() {
319
358
}
320
359
```
321
360
322
-
> You can get the full code [here](../guide/example/example-json-07.kt).
361
+
> You can get the full code [here](../guide/example/example-json-08.kt).
323
362
324
363
The invalid `null` value for the `language` property was coerced into the default value:
325
364
@@ -348,7 +387,7 @@ fun main() {
348
387
}
349
388
```
350
389
351
-
> You can get the full code [here](../guide/example/example-json-08.kt).
390
+
> You can get the full code [here](../guide/example/example-json-09.kt).
352
391
353
392
Despite that we do not have `Color.pink` and `Color.purple` colors, `decodeFromString` function returns successfully:
354
393
@@ -384,7 +423,7 @@ fun main() {
384
423
}
385
424
```
386
425
387
-
> You can get the full code [here](../guide/example/example-json-09.kt).
426
+
> You can get the full code [here](../guide/example/example-json-10.kt).
388
427
389
428
The map with structured keys gets represented as JSON array with the following items: `[key1, value1, key2, value2,...]`.
390
429
@@ -415,7 +454,7 @@ fun main() {
415
454
}
416
455
```
417
456
418
-
> You can get the full code [here](../guide/example/example-json-10.kt).
457
+
> You can get the full code [here](../guide/example/example-json-11.kt).
419
458
420
459
This example produces the following non-stardard JSON output, yet it is a widely used encoding for
421
460
special values in JVM world:
@@ -449,7 +488,7 @@ fun main() {
449
488
}
450
489
```
451
490
452
-
> You can get the full code [here](../guide/example/example-json-11.kt).
491
+
> You can get the full code [here](../guide/example/example-json-12.kt).
453
492
454
493
In combination with an explicitly specified [SerialName] of the class it provides full
455
494
control over the resulting JSON object:
@@ -506,7 +545,7 @@ fun main() {
506
545
}
507
546
```
508
547
509
-
> You can get the full code [here](../guide/example/example-json-12.kt).
548
+
> You can get the full code [here](../guide/example/example-json-13.kt).
510
549
511
550
As you can see, discriminator from the `Base` class is used:
512
551
@@ -543,7 +582,7 @@ fun main() {
543
582
}
544
583
```
545
584
546
-
> You can get the full code [here](../guide/example/example-json-13.kt).
585
+
> You can get the full code [here](../guide/example/example-json-14.kt).
547
586
548
587
Note that it would be impossible to deserialize this output back with kotlinx.serialization.
549
588
@@ -579,7 +618,7 @@ fun main() {
579
618
}
580
619
```
581
620
582
-
> You can get the full code [here](../guide/example/example-json-14.kt).
621
+
> You can get the full code [here](../guide/example/example-json-15.kt).
583
622
584
623
It affects serial names as well as alternative names specified with [JsonNames] annotation, so both values are successfully decoded:
585
624
@@ -612,7 +651,7 @@ fun main() {
612
651
}
613
652
```
614
653
615
-
> You can get the full code [here](../guide/example/example-json-15.kt).
654
+
> You can get the full code [here](../guide/example/example-json-16.kt).
616
655
617
656
As you can see, both serialization and deserialization work as if all serial names are transformed from camel case to snake case:
618
657
@@ -710,7 +749,7 @@ fun main() {
710
749
}
711
750
```
712
751
713
-
> You can get the full code [here](../guide/example/example-json-16.kt)
752
+
> You can get the full code [here](../guide/example/example-json-17.kt)
714
753
715
754
```text
716
755
{"base64Input":"Zm9vIHN0cmluZw=="}
@@ -752,7 +791,7 @@ fun main() {
752
791
}
753
792
```
754
793
755
-
> You can get the full code [here](../guide/example/example-json-17.kt).
794
+
> You can get the full code [here](../guide/example/example-json-18.kt).
756
795
757
796
A `JsonElement` prints itself as a valid JSON:
758
797
@@ -795,7 +834,7 @@ fun main() {
795
834
}
796
835
```
797
836
798
-
> You can get the full code [here](../guide/example/example-json-18.kt).
837
+
> You can get the full code [here](../guide/example/example-json-19.kt).
799
838
800
839
The above example sums `votes` in all objects in the `forks` array, ignoring the objects that have no `votes`:
801
840
@@ -835,7 +874,7 @@ fun main() {
835
874
}
836
875
```
837
876
838
-
> You can get the full code [here](../guide/example/example-json-19.kt).
877
+
> You can get the full code [here](../guide/example/example-json-20.kt).
839
878
840
879
As a result, you get a proper JSON string:
841
880
@@ -864,7 +903,7 @@ fun main() {
864
903
}
865
904
```
866
905
867
-
> You can get the full code [here](../guide/example/example-json-20.kt).
906
+
> You can get the full code [here](../guide/example/example-json-21.kt).
868
907
869
908
The result is exactly what you would expect:
870
909
@@ -910,7 +949,7 @@ fun main() {
910
949
}
911
950
```
912
951
913
-
> You can get the full code [here](../guide/example/example-json-21.kt).
952
+
> You can get the full code [here](../guide/example/example-json-22.kt).
914
953
915
954
Even though `pi` was defined as a number with 30 decimal places, the resulting JSON does not reflect this.
916
955
The [Double] value is truncated to 15 decimal places, and the String is wrapped in quotes - which is not a JSON number.
@@ -951,7 +990,7 @@ fun main() {
951
990
}
952
991
```
953
992
954
-
> You can get the full code [here](../guide/example/example-json-22.kt).
993
+
> You can get the full code [here](../guide/example/example-json-23.kt).
955
994
956
995
`pi_literal` now accurately matches the value defined.
957
996
@@ -991,7 +1030,7 @@ fun main() {
991
1030
}
992
1031
```
993
1032
994
-
> You can get the full code [here](../guide/example/example-json-23.kt).
1033
+
> You can get the full code [here](../guide/example/example-json-24.kt).
995
1034
996
1035
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
997
1036
@@ -1014,7 +1053,7 @@ fun main() {
1014
1053
}
1015
1054
```
1016
1055
1017
-
> You can get the full code [here](../guide/example/example-json-24.kt).
1056
+
> You can get the full code [here](../guide/example/example-json-25.kt).
1018
1057
1019
1058
```text
1020
1059
Exception in thread "main" kotlinx.serialization.json.internal.JsonEncodingException: Creating a literal unquoted value of 'null' is forbidden. If you want to create JSON null literal, use JsonNull object, otherwise, use JsonPrimitive
@@ -1090,7 +1129,7 @@ fun main() {
1090
1129
}
1091
1130
```
1092
1131
1093
-
> You can get the full code [here](../guide/example/example-json-25.kt).
1132
+
> You can get the full code [here](../guide/example/example-json-26.kt).
1094
1133
1095
1134
The output shows that both cases are correctly deserialized into a Kotlin [List].
1096
1135
@@ -1142,7 +1181,7 @@ fun main() {
1142
1181
}
1143
1182
```
1144
1183
1145
-
> You can get the full code [here](../guide/example/example-json-26.kt).
1184
+
> You can get the full code [here](../guide/example/example-json-27.kt).
1146
1185
1147
1186
You end up with a single JSON object, not an array with one element:
1148
1187
@@ -1187,7 +1226,7 @@ fun main() {
1187
1226
}
1188
1227
```
1189
1228
1190
-
> You can get the full code [here](../guide/example/example-json-27.kt).
1229
+
> You can get the full code [here](../guide/example/example-json-28.kt).
1191
1230
1192
1231
See the effect of the custom serializer:
1193
1232
@@ -1260,7 +1299,7 @@ fun main() {
1260
1299
}
1261
1300
```
1262
1301
1263
-
> You can get the full code [here](../guide/example/example-json-28.kt).
1302
+
> You can get the full code [here](../guide/example/example-json-29.kt).
1264
1303
1265
1304
No class discriminator is added in the JSON output:
1266
1305
@@ -1312,7 +1351,7 @@ fun main() {
1312
1351
}
1313
1352
```
1314
1353
1315
-
> You can get the full code [here](../guide/example/example-json-29.kt).
1354
+
> You can get the full code [here](../guide/example/example-json-30.kt).
1316
1355
1317
1356
`BasicProject` will be printed to the output:
1318
1357
@@ -1406,7 +1445,7 @@ fun main() {
1406
1445
}
1407
1446
```
1408
1447
1409
-
> You can get the full code [here](../guide/example/example-json-30.kt).
1448
+
> You can get the full code [here](../guide/example/example-json-31.kt).
1410
1449
1411
1450
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
1412
1451
@@ -1471,7 +1510,7 @@ fun main() {
1471
1510
}
1472
1511
```
1473
1512
1474
-
> You can get the full code [here](../guide/example/example-json-31.kt).
1513
+
> You can get the full code [here](../guide/example/example-json-32.kt).
Copy file name to clipboardExpand all lines: formats/json/api/kotlinx-serialization-json.api
+7
Original file line number
Diff line number
Diff line change
@@ -262,6 +262,13 @@ public final class kotlinx/serialization/json/JsonEncoder$DefaultImpls {
262
262
public static fun shouldEncodeElementDefault (Lkotlinx/serialization/json/JsonEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;I)Z
263
263
}
264
264
265
+
public abstract interface annotation class kotlinx/serialization/json/JsonIgnoreUnknownKeys : java/lang/annotation/Annotation {
266
+
}
267
+
268
+
public synthetic class kotlinx/serialization/json/JsonIgnoreUnknownKeys$Impl : kotlinx/serialization/json/JsonIgnoreUnknownKeys {
269
+
public fun <init> ()V
270
+
}
271
+
265
272
public final class kotlinx/serialization/json/JsonKt {
266
273
public static final fun Json (Lkotlinx/serialization/json/Json;Lkotlin/jvm/functions/Function1;)Lkotlinx/serialization/json/Json;
267
274
public static synthetic fun Json$default (Lkotlinx/serialization/json/Json;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/serialization/json/Json;
0 commit comments