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
val data:Project=OwnedProject("kotlinx.coroutines", "kotlin")
496
+
println(format.encodeToString(data))
497
+
}
498
+
```
499
+
500
+
> You can get the full code [here](../guide/example/example-json-12.kt).
501
+
502
+
Note that it would be impossible to deserialize this output back with kotlinx.serialization.
503
+
504
+
```text
505
+
{"name":"kotlinx.coroutines","owner":"kotlin"}
506
+
```
507
+
508
+
Two other available values are [ClassDiscriminatorMode.POLYMORPHIC] (default behavior) and [ClassDiscriminatorMode.ALL_JSON_OBJECTS] (adds discriminator whenever possible).
> You can get the full code [here](../guide/example/example-json-12.kt).
534
+
> You can get the full code [here](../guide/example/example-json-13.kt).
495
535
496
536
It affects serial names as well as alternative names specified with [JsonNames] annotation, so both values are successfully decoded:
497
537
@@ -523,7 +563,7 @@ fun main() {
523
563
}
524
564
```
525
565
526
-
> You can get the full code [here](../guide/example/example-json-13.kt).
566
+
> You can get the full code [here](../guide/example/example-json-14.kt).
527
567
528
568
As you can see, both serialization and deserialization work as if all serial names are transformed from camel case to snake case:
529
569
@@ -575,7 +615,7 @@ fun main() {
575
615
}
576
616
```
577
617
578
-
> You can get the full code [here](../guide/example/example-json-14.kt).
618
+
> You can get the full code [here](../guide/example/example-json-15.kt).
579
619
580
620
A `JsonElement` prints itself as a valid JSON:
581
621
@@ -618,7 +658,7 @@ fun main() {
618
658
}
619
659
```
620
660
621
-
> You can get the full code [here](../guide/example/example-json-15.kt).
661
+
> You can get the full code [here](../guide/example/example-json-16.kt).
622
662
623
663
The above example sums `votes` in all objects in the `forks` array, ignoring the objects that have no `votes`:
624
664
@@ -658,7 +698,7 @@ fun main() {
658
698
}
659
699
```
660
700
661
-
> You can get the full code [here](../guide/example/example-json-16.kt).
701
+
> You can get the full code [here](../guide/example/example-json-17.kt).
662
702
663
703
As a result, you get a proper JSON string:
664
704
@@ -687,7 +727,7 @@ fun main() {
687
727
}
688
728
```
689
729
690
-
> You can get the full code [here](../guide/example/example-json-17.kt).
730
+
> You can get the full code [here](../guide/example/example-json-18.kt).
691
731
692
732
The result is exactly what you would expect:
693
733
@@ -733,7 +773,7 @@ fun main() {
733
773
}
734
774
```
735
775
736
-
> You can get the full code [here](../guide/example/example-json-18.kt).
776
+
> You can get the full code [here](../guide/example/example-json-19.kt).
737
777
738
778
Even though `pi` was defined as a number with 30 decimal places, the resulting JSON does not reflect this.
739
779
The [Double] value is truncated to 15 decimal places, and the String is wrapped in quotes - which is not a JSON number.
@@ -773,7 +813,7 @@ fun main() {
773
813
}
774
814
```
775
815
776
-
> You can get the full code [here](../guide/example/example-json-19.kt).
816
+
> You can get the full code [here](../guide/example/example-json-20.kt).
777
817
778
818
`pi_literal` now accurately matches the value defined.
779
819
@@ -813,7 +853,7 @@ fun main() {
813
853
}
814
854
```
815
855
816
-
> You can get the full code [here](../guide/example/example-json-20.kt).
856
+
> You can get the full code [here](../guide/example/example-json-21.kt).
817
857
818
858
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
819
859
@@ -835,7 +875,7 @@ fun main() {
835
875
}
836
876
```
837
877
838
-
> You can get the full code [here](../guide/example/example-json-21.kt).
878
+
> You can get the full code [here](../guide/example/example-json-22.kt).
839
879
840
880
```text
841
881
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
@@ -911,7 +951,7 @@ fun main() {
911
951
}
912
952
```
913
953
914
-
> You can get the full code [here](../guide/example/example-json-22.kt).
954
+
> You can get the full code [here](../guide/example/example-json-23.kt).
915
955
916
956
The output shows that both cases are correctly deserialized into a Kotlin [List].
917
957
@@ -963,7 +1003,7 @@ fun main() {
963
1003
}
964
1004
```
965
1005
966
-
> 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).
967
1007
968
1008
You end up with a single JSON object, not an array with one element:
969
1009
@@ -1008,7 +1048,7 @@ fun main() {
1008
1048
}
1009
1049
```
1010
1050
1011
-
> You can get the full code [here](../guide/example/example-json-24.kt).
1051
+
> You can get the full code [here](../guide/example/example-json-25.kt).
1012
1052
1013
1053
See the effect of the custom serializer:
1014
1054
@@ -1081,7 +1121,7 @@ fun main() {
1081
1121
}
1082
1122
```
1083
1123
1084
-
> You can get the full code [here](../guide/example/example-json-25.kt).
1124
+
> You can get the full code [here](../guide/example/example-json-26.kt).
1085
1125
1086
1126
No class discriminator is added in the JSON output:
1087
1127
@@ -1177,7 +1217,7 @@ fun main() {
1177
1217
}
1178
1218
```
1179
1219
1180
-
> You can get the full code [here](../guide/example/example-json-26.kt).
1220
+
> You can get the full code [here](../guide/example/example-json-27.kt).
1181
1221
1182
1222
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
1183
1223
@@ -1242,7 +1282,7 @@ fun main() {
1242
1282
}
1243
1283
```
1244
1284
1245
-
> You can get the full code [here](../guide/example/example-json-27.kt).
1285
+
> You can get the full code [here](../guide/example/example-json-28.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