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
Aside from direct conversions between strings and JSON objects, Kotlin serialization offers APIs that allow
@@ -615,7 +704,7 @@ fun main() {
615
704
}
616
705
```
617
706
618
-
> You can get the full code [here](../guide/example/example-json-15.kt).
707
+
> You can get the full code [here](../guide/example/example-json-16.kt).
619
708
620
709
A `JsonElement` prints itself as a valid JSON:
621
710
@@ -658,7 +747,7 @@ fun main() {
658
747
}
659
748
```
660
749
661
-
> You can get the full code [here](../guide/example/example-json-16.kt).
750
+
> You can get the full code [here](../guide/example/example-json-17.kt).
662
751
663
752
The above example sums `votes` in all objects in the `forks` array, ignoring the objects that have no `votes`:
664
753
@@ -698,7 +787,7 @@ fun main() {
698
787
}
699
788
```
700
789
701
-
> You can get the full code [here](../guide/example/example-json-17.kt).
790
+
> You can get the full code [here](../guide/example/example-json-18.kt).
702
791
703
792
As a result, you get a proper JSON string:
704
793
@@ -727,7 +816,7 @@ fun main() {
727
816
}
728
817
```
729
818
730
-
> You can get the full code [here](../guide/example/example-json-18.kt).
819
+
> You can get the full code [here](../guide/example/example-json-19.kt).
731
820
732
821
The result is exactly what you would expect:
733
822
@@ -773,7 +862,7 @@ fun main() {
773
862
}
774
863
```
775
864
776
-
> You can get the full code [here](../guide/example/example-json-19.kt).
865
+
> You can get the full code [here](../guide/example/example-json-20.kt).
777
866
778
867
Even though `pi` was defined as a number with 30 decimal places, the resulting JSON does not reflect this.
779
868
The [Double] value is truncated to 15 decimal places, and the String is wrapped in quotes - which is not a JSON number.
@@ -813,7 +902,7 @@ fun main() {
813
902
}
814
903
```
815
904
816
-
> You can get the full code [here](../guide/example/example-json-20.kt).
905
+
> You can get the full code [here](../guide/example/example-json-21.kt).
817
906
818
907
`pi_literal` now accurately matches the value defined.
819
908
@@ -853,7 +942,7 @@ fun main() {
853
942
}
854
943
```
855
944
856
-
> You can get the full code [here](../guide/example/example-json-21.kt).
945
+
> You can get the full code [here](../guide/example/example-json-22.kt).
857
946
858
947
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
859
948
@@ -875,7 +964,7 @@ fun main() {
875
964
}
876
965
```
877
966
878
-
> You can get the full code [here](../guide/example/example-json-22.kt).
967
+
> You can get the full code [here](../guide/example/example-json-23.kt).
879
968
880
969
```text
881
970
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
@@ -951,7 +1040,7 @@ fun main() {
951
1040
}
952
1041
```
953
1042
954
-
> You can get the full code [here](../guide/example/example-json-23.kt).
1043
+
> You can get the full code [here](../guide/example/example-json-24.kt).
955
1044
956
1045
The output shows that both cases are correctly deserialized into a Kotlin [List].
957
1046
@@ -1003,7 +1092,7 @@ fun main() {
1003
1092
}
1004
1093
```
1005
1094
1006
-
> You can get the full code [here](../guide/example/example-json-24.kt).
1095
+
> You can get the full code [here](../guide/example/example-json-25.kt).
1007
1096
1008
1097
You end up with a single JSON object, not an array with one element:
1009
1098
@@ -1048,7 +1137,7 @@ fun main() {
1048
1137
}
1049
1138
```
1050
1139
1051
-
> You can get the full code [here](../guide/example/example-json-25.kt).
1140
+
> You can get the full code [here](../guide/example/example-json-26.kt).
1052
1141
1053
1142
See the effect of the custom serializer:
1054
1143
@@ -1121,7 +1210,7 @@ fun main() {
1121
1210
}
1122
1211
```
1123
1212
1124
-
> You can get the full code [here](../guide/example/example-json-26.kt).
1213
+
> You can get the full code [here](../guide/example/example-json-27.kt).
1125
1214
1126
1215
No class discriminator is added in the JSON output:
1127
1216
@@ -1217,7 +1306,7 @@ fun main() {
1217
1306
}
1218
1307
```
1219
1308
1220
-
> You can get the full code [here](../guide/example/example-json-27.kt).
1309
+
> You can get the full code [here](../guide/example/example-json-28.kt).
1221
1310
1222
1311
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
1223
1312
@@ -1282,7 +1371,7 @@ fun main() {
1282
1371
}
1283
1372
```
1284
1373
1285
-
> You can get the full code [here](../guide/example/example-json-28.kt).
1374
+
> You can get the full code [here](../guide/example/example-json-29.kt).
* <aname='decoding-enums-in-a-case-insensitive-manner'></a>[Decoding enums in a case-insensitive manner](json.md#decoding-enums-in-a-case-insensitive-manner)
0 commit comments