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
Introduce 'decodeEnumsCaseInsensitive' feature to Json. (#2345)
It allows decoding enum values in a case-insensitive manner. It does not affect CLASS kinds or encoding. It is one of the most-voted feature requests.
Also enhance JsonNamingStrategy documentation.
Fixes#209
> You can get the full code [here](../guide/example/example-json-12.kt).
495
+
496
+
It affects serial names as well as alternative names specified with [JsonNames] annotation, so both values are successfully decoded:
497
+
498
+
```text
499
+
CasesList(cases=[VALUE_A, VALUE_B])
500
+
```
501
+
502
+
This property does not affect encoding in any way.
503
+
504
+
<!--- TEST -->
505
+
472
506
### Global naming strategy
473
507
474
508
If properties' names in Json input are different from Kotlin ones, it is recommended to specify the name
@@ -489,7 +523,7 @@ fun main() {
489
523
}
490
524
```
491
525
492
-
> You can get the full code [here](../guide/example/example-json-12.kt).
526
+
> You can get the full code [here](../guide/example/example-json-13.kt).
493
527
494
528
As you can see, both serialization and deserialization work as if all serial names are transformed from camel case to snake case:
495
529
@@ -541,7 +575,7 @@ fun main() {
541
575
}
542
576
```
543
577
544
-
> You can get the full code [here](../guide/example/example-json-13.kt).
578
+
> You can get the full code [here](../guide/example/example-json-14.kt).
545
579
546
580
A `JsonElement` prints itself as a valid JSON:
547
581
@@ -584,7 +618,7 @@ fun main() {
584
618
}
585
619
```
586
620
587
-
> 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).
588
622
589
623
The above example sums `votes` in all objects in the `forks` array, ignoring the objects that have no `votes`:
590
624
@@ -624,7 +658,7 @@ fun main() {
624
658
}
625
659
```
626
660
627
-
> 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).
628
662
629
663
As a result, you get a proper JSON string:
630
664
@@ -653,7 +687,7 @@ fun main() {
653
687
}
654
688
```
655
689
656
-
> You can get the full code [here](../guide/example/example-json-16.kt).
690
+
> You can get the full code [here](../guide/example/example-json-17.kt).
657
691
658
692
The result is exactly what you would expect:
659
693
@@ -699,7 +733,7 @@ fun main() {
699
733
}
700
734
```
701
735
702
-
> You can get the full code [here](../guide/example/example-json-17.kt).
736
+
> You can get the full code [here](../guide/example/example-json-18.kt).
703
737
704
738
Even though `pi` was defined as a number with 30 decimal places, the resulting JSON does not reflect this.
705
739
The [Double] value is truncated to 15 decimal places, and the String is wrapped in quotes - which is not a JSON number.
@@ -739,7 +773,7 @@ fun main() {
739
773
}
740
774
```
741
775
742
-
> 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).
743
777
744
778
`pi_literal` now accurately matches the value defined.
745
779
@@ -779,7 +813,7 @@ fun main() {
779
813
}
780
814
```
781
815
782
-
> 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).
783
817
784
818
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
785
819
@@ -801,7 +835,7 @@ fun main() {
801
835
}
802
836
```
803
837
804
-
> You can get the full code [here](../guide/example/example-json-20.kt).
838
+
> You can get the full code [here](../guide/example/example-json-21.kt).
805
839
806
840
```text
807
841
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
@@ -877,7 +911,7 @@ fun main() {
877
911
}
878
912
```
879
913
880
-
> You can get the full code [here](../guide/example/example-json-21.kt).
914
+
> You can get the full code [here](../guide/example/example-json-22.kt).
881
915
882
916
The output shows that both cases are correctly deserialized into a Kotlin [List].
883
917
@@ -929,7 +963,7 @@ fun main() {
929
963
}
930
964
```
931
965
932
-
> You can get the full code [here](../guide/example/example-json-22.kt).
966
+
> You can get the full code [here](../guide/example/example-json-23.kt).
933
967
934
968
You end up with a single JSON object, not an array with one element:
935
969
@@ -974,7 +1008,7 @@ fun main() {
974
1008
}
975
1009
```
976
1010
977
-
> You can get the full code [here](../guide/example/example-json-23.kt).
1011
+
> You can get the full code [here](../guide/example/example-json-24.kt).
978
1012
979
1013
See the effect of the custom serializer:
980
1014
@@ -1047,7 +1081,7 @@ fun main() {
1047
1081
}
1048
1082
```
1049
1083
1050
-
> You can get the full code [here](../guide/example/example-json-24.kt).
1084
+
> You can get the full code [here](../guide/example/example-json-25.kt).
1051
1085
1052
1086
No class discriminator is added in the JSON output:
1053
1087
@@ -1143,7 +1177,7 @@ fun main() {
1143
1177
}
1144
1178
```
1145
1179
1146
-
> You can get the full code [here](../guide/example/example-json-25.kt).
1180
+
> You can get the full code [here](../guide/example/example-json-26.kt).
1147
1181
1148
1182
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
1149
1183
@@ -1208,7 +1242,7 @@ fun main() {
1208
1242
}
1209
1243
```
1210
1244
1211
-
> You can get the full code [here](../guide/example/example-json-26.kt).
1245
+
> You can get the full code [here](../guide/example/example-json-27.kt).
* <aname='allowing-special-floating-point-values'></a>[Allowing special floating-point values](json.md#allowing-special-floating-point-values)
121
121
* <aname='class-discriminator-for-polymorphism'></a>[Class discriminator for polymorphism](json.md#class-discriminator-for-polymorphism)
122
+
* <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