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
- Bring back its interaction with `coerceInputValues` flag
- Enhance documentation and add more samples to it
- Remove @ExperimentalSerializationApi
FixesKotlin#2636FixesKotlin#2586
Example of using this flag together with [explicitNulls](#explicit-nulls) to coerce invalid enum values:
329
+
330
+
```kotlin
331
+
enumclassColor { BLACK, WHITE }
332
+
333
+
@Serializable
334
+
data classBrush(valforeground:Color = Color.BLACK, valbackground:Color?)
335
+
336
+
val json =Json {
337
+
coerceInputValues =true
338
+
explicitNulls =false
339
+
}
340
+
341
+
funmain() {
342
+
val brush = json.decodeFromString<Brush>("""{"foreground":"pink", "background":"purple"}""")
343
+
println(brush)
344
+
}
345
+
```
346
+
347
+
> You can get the full code [here](../guide/example/example-json-08.kt).
348
+
349
+
Despite that we do not have `Color.pink` and `Color.purple` colors, `decodeFromString` function returns successfully:
350
+
351
+
```text
352
+
Brush(foreground=BLACK, background=null)
353
+
```
354
+
355
+
`foreground` property received its default value, and `background` property received `null` because of `explicitNulls = false` setting.
356
+
357
+
<!--- TEST -->
358
+
320
359
### Allowing structured map keys
321
360
322
361
JSON format does not natively support the concept of a map with structured keys. Keys in JSON objects
@@ -341,7 +380,7 @@ fun main() {
341
380
}
342
381
```
343
382
344
-
> You can get the full code [here](../guide/example/example-json-08.kt).
383
+
> You can get the full code [here](../guide/example/example-json-09.kt).
345
384
346
385
The map with structured keys gets represented as JSON array with the following items: `[key1, value1, key2, value2,...]`.
347
386
@@ -372,7 +411,7 @@ fun main() {
372
411
}
373
412
```
374
413
375
-
> You can get the full code [here](../guide/example/example-json-09.kt).
414
+
> You can get the full code [here](../guide/example/example-json-10.kt).
376
415
377
416
This example produces the following non-stardard JSON output, yet it is a widely used encoding for
378
417
special values in JVM world:
@@ -406,7 +445,7 @@ fun main() {
406
445
}
407
446
```
408
447
409
-
> You can get the full code [here](../guide/example/example-json-10.kt).
448
+
> You can get the full code [here](../guide/example/example-json-11.kt).
410
449
411
450
In combination with an explicitly specified [SerialName] of the class it provides full
412
451
control over the resulting JSON object:
@@ -462,7 +501,7 @@ fun main() {
462
501
}
463
502
```
464
503
465
-
> You can get the full code [here](../guide/example/example-json-11.kt).
504
+
> You can get the full code [here](../guide/example/example-json-12.kt).
466
505
467
506
As you can see, discriminator from the `Base` class is used:
468
507
@@ -498,7 +537,7 @@ fun main() {
498
537
}
499
538
```
500
539
501
-
> You can get the full code [here](../guide/example/example-json-12.kt).
540
+
> You can get the full code [here](../guide/example/example-json-13.kt).
502
541
503
542
Note that it would be impossible to deserialize this output back with kotlinx.serialization.
504
543
@@ -532,7 +571,7 @@ fun main() {
532
571
}
533
572
```
534
573
535
-
> You can get the full code [here](../guide/example/example-json-13.kt).
574
+
> You can get the full code [here](../guide/example/example-json-14.kt).
536
575
537
576
It affects serial names as well as alternative names specified with [JsonNames] annotation, so both values are successfully decoded:
538
577
@@ -564,7 +603,7 @@ fun main() {
564
603
}
565
604
```
566
605
567
-
> You can get the full code [here](../guide/example/example-json-14.kt).
606
+
> You can get the full code [here](../guide/example/example-json-15.kt).
568
607
569
608
As you can see, both serialization and deserialization work as if all serial names are transformed from camel case to snake case:
570
609
@@ -662,7 +701,7 @@ fun main() {
662
701
}
663
702
```
664
703
665
-
> You can get the full code [here](../guide/example/example-json-15.kt)
704
+
> You can get the full code [here](../guide/example/example-json-16.kt)
666
705
667
706
```text
668
707
{"base64Input":"Zm9vIHN0cmluZw=="}
@@ -704,7 +743,7 @@ fun main() {
704
743
}
705
744
```
706
745
707
-
> You can get the full code [here](../guide/example/example-json-16.kt).
746
+
> You can get the full code [here](../guide/example/example-json-17.kt).
708
747
709
748
A `JsonElement` prints itself as a valid JSON:
710
749
@@ -747,7 +786,7 @@ fun main() {
747
786
}
748
787
```
749
788
750
-
> You can get the full code [here](../guide/example/example-json-17.kt).
789
+
> You can get the full code [here](../guide/example/example-json-18.kt).
751
790
752
791
The above example sums `votes` in all objects in the `forks` array, ignoring the objects that have no `votes`:
753
792
@@ -787,7 +826,7 @@ fun main() {
787
826
}
788
827
```
789
828
790
-
> You can get the full code [here](../guide/example/example-json-18.kt).
829
+
> You can get the full code [here](../guide/example/example-json-19.kt).
791
830
792
831
As a result, you get a proper JSON string:
793
832
@@ -816,7 +855,7 @@ fun main() {
816
855
}
817
856
```
818
857
819
-
> You can get the full code [here](../guide/example/example-json-19.kt).
858
+
> You can get the full code [here](../guide/example/example-json-20.kt).
820
859
821
860
The result is exactly what you would expect:
822
861
@@ -862,7 +901,7 @@ fun main() {
862
901
}
863
902
```
864
903
865
-
> You can get the full code [here](../guide/example/example-json-20.kt).
904
+
> You can get the full code [here](../guide/example/example-json-21.kt).
866
905
867
906
Even though `pi` was defined as a number with 30 decimal places, the resulting JSON does not reflect this.
868
907
The [Double] value is truncated to 15 decimal places, and the String is wrapped in quotes - which is not a JSON number.
@@ -902,7 +941,7 @@ fun main() {
902
941
}
903
942
```
904
943
905
-
> You can get the full code [here](../guide/example/example-json-21.kt).
944
+
> You can get the full code [here](../guide/example/example-json-22.kt).
906
945
907
946
`pi_literal` now accurately matches the value defined.
908
947
@@ -942,7 +981,7 @@ fun main() {
942
981
}
943
982
```
944
983
945
-
> You can get the full code [here](../guide/example/example-json-22.kt).
984
+
> You can get the full code [here](../guide/example/example-json-23.kt).
946
985
947
986
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
948
987
@@ -964,7 +1003,7 @@ fun main() {
964
1003
}
965
1004
```
966
1005
967
-
> You can get the full code [here](../guide/example/example-json-23.kt).
1006
+
> You can get the full code [here](../guide/example/example-json-24.kt).
968
1007
969
1008
```text
970
1009
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
@@ -1040,7 +1079,7 @@ fun main() {
1040
1079
}
1041
1080
```
1042
1081
1043
-
> You can get the full code [here](../guide/example/example-json-24.kt).
1082
+
> You can get the full code [here](../guide/example/example-json-25.kt).
1044
1083
1045
1084
The output shows that both cases are correctly deserialized into a Kotlin [List].
1046
1085
@@ -1092,7 +1131,7 @@ fun main() {
1092
1131
}
1093
1132
```
1094
1133
1095
-
> You can get the full code [here](../guide/example/example-json-25.kt).
1134
+
> You can get the full code [here](../guide/example/example-json-26.kt).
1096
1135
1097
1136
You end up with a single JSON object, not an array with one element:
1098
1137
@@ -1137,7 +1176,7 @@ fun main() {
1137
1176
}
1138
1177
```
1139
1178
1140
-
> You can get the full code [here](../guide/example/example-json-26.kt).
1179
+
> You can get the full code [here](../guide/example/example-json-27.kt).
1141
1180
1142
1181
See the effect of the custom serializer:
1143
1182
@@ -1210,7 +1249,7 @@ fun main() {
1210
1249
}
1211
1250
```
1212
1251
1213
-
> You can get the full code [here](../guide/example/example-json-27.kt).
1252
+
> You can get the full code [here](../guide/example/example-json-28.kt).
1214
1253
1215
1254
No class discriminator is added in the JSON output:
1216
1255
@@ -1306,7 +1345,7 @@ fun main() {
1306
1345
}
1307
1346
```
1308
1347
1309
-
> You can get the full code [here](../guide/example/example-json-28.kt).
1348
+
> You can get the full code [here](../guide/example/example-json-29.kt).
1310
1349
1311
1350
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
1312
1351
@@ -1371,7 +1410,7 @@ fun main() {
1371
1410
}
1372
1411
```
1373
1412
1374
-
> You can get the full code [here](../guide/example/example-json-29.kt).
1413
+
> You can get the full code [here](../guide/example/example-json-30.kt).
0 commit comments