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
*[Parsing to Json element](#parsing-to-json-element)
25
26
*[Types of Json elements](#types-of-json-elements)
@@ -468,6 +469,53 @@ As you can see, discriminator from the `Base` class is used:
468
469
469
470
<!--- TEST -->
470
471
472
+
### Global naming strategy
473
+
474
+
If properties' names in Json input are different from Kotlin ones, it is recommended to specify the name
475
+
for each property explicitly using [`@SerialName` annotation](basic-serialization.md#serial-field-names).
476
+
However, there are certain situations where transformation should be applied to every serial name — such as migration
477
+
from other frameworks or legacy codebase. For that cases, it is possible to specify a [namingStrategy][JsonBuilder.namingStrategy]
478
+
for a [Json] instance. `kotlinx.serialization` provides one strategy implementation out of the box, the [JsonNamingStrategy.SnakeCase](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-naming-strategy/-builtins/-snake-case.html):
479
+
480
+
```kotlin
481
+
@Serializable
482
+
data classProject(valprojectName:String, valprojectOwner:String)
483
+
484
+
val format =Json { namingStrategy =JsonNamingStrategy.SnakeCase }
485
+
486
+
funmain() {
487
+
val project = format.decodeFromString<Project>("""{"project_name":"kotlinx.coroutines", "project_owner":"Kotlin"}""")
There are some caveats one should remember while dealing with a [JsonNamingStrategy]:
501
+
502
+
* Due to the nature of the `kotlinx.serialization` framework, naming strategy transformation is applied to all properties regardless
503
+
of whether their serial name was taken from the property name or provided by [SerialName] annotation.
504
+
Effectively, it means one cannot avoid transformation by explicitly specifying the serial name. To be able to deserialize
505
+
non-transformed names, [JsonNames] annotation can be used instead.
506
+
507
+
* Collision of the transformed name with any other (transformed) properties serial names or any alternative names
508
+
specified with [JsonNames] will lead to a deserialization exception.
509
+
510
+
* Global naming strategies are very implicit: by looking only at the definition of the class,
511
+
it is impossible to determine which names it will have in the serialized form.
512
+
As a consequence, naming strategies are not friendly to actions like Find Usages/Rename in IDE, full-text search by grep, etc.
513
+
For them, the original name and the transformed are two different things;
514
+
changing one without the other may introduce bugs in many unexpected ways and lead to greater maintenance efforts for code with global naming strategies.
515
+
516
+
Therefore, one should carefully weigh the pros and cons before considering adding global naming strategies to an application.
517
+
518
+
<!--- TEST -->
471
519
472
520
## Json elements
473
521
@@ -493,7 +541,7 @@ fun main() {
493
541
}
494
542
```
495
543
496
-
> You can get the full code [here](../guide/example/example-json-12.kt).
544
+
> You can get the full code [here](../guide/example/example-json-13.kt).
497
545
498
546
A `JsonElement` prints itself as a valid JSON:
499
547
@@ -536,7 +584,7 @@ fun main() {
536
584
}
537
585
```
538
586
539
-
> You can get the full code [here](../guide/example/example-json-13.kt).
587
+
> You can get the full code [here](../guide/example/example-json-14.kt).
540
588
541
589
The above example sums `votes` in all objects in the `forks` array, ignoring the objects that have no `votes`:
542
590
@@ -576,7 +624,7 @@ fun main() {
576
624
}
577
625
```
578
626
579
-
> You can get the full code [here](../guide/example/example-json-14.kt).
627
+
> You can get the full code [here](../guide/example/example-json-15.kt).
580
628
581
629
As a result, you get a proper JSON string:
582
630
@@ -605,7 +653,7 @@ fun main() {
605
653
}
606
654
```
607
655
608
-
> You can get the full code [here](../guide/example/example-json-15.kt).
656
+
> You can get the full code [here](../guide/example/example-json-16.kt).
609
657
610
658
The result is exactly what you would expect:
611
659
@@ -651,7 +699,7 @@ fun main() {
651
699
}
652
700
```
653
701
654
-
> You can get the full code [here](../guide/example/example-json-16.kt).
702
+
> You can get the full code [here](../guide/example/example-json-17.kt).
655
703
656
704
Even though `pi` was defined as a number with 30 decimal places, the resulting JSON does not reflect this.
657
705
The [Double] value is truncated to 15 decimal places, and the String is wrapped in quotes - which is not a JSON number.
@@ -691,7 +739,7 @@ fun main() {
691
739
}
692
740
```
693
741
694
-
> You can get the full code [here](../guide/example/example-json-17.kt).
742
+
> You can get the full code [here](../guide/example/example-json-18.kt).
695
743
696
744
`pi_literal` now accurately matches the value defined.
697
745
@@ -731,7 +779,7 @@ fun main() {
731
779
}
732
780
```
733
781
734
-
> You can get the full code [here](../guide/example/example-json-18.kt).
782
+
> You can get the full code [here](../guide/example/example-json-19.kt).
735
783
736
784
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
737
785
@@ -753,7 +801,7 @@ fun main() {
753
801
}
754
802
```
755
803
756
-
> You can get the full code [here](../guide/example/example-json-19.kt).
804
+
> You can get the full code [here](../guide/example/example-json-20.kt).
757
805
758
806
```text
759
807
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
@@ -829,7 +877,7 @@ fun main() {
829
877
}
830
878
```
831
879
832
-
> You can get the full code [here](../guide/example/example-json-20.kt).
880
+
> You can get the full code [here](../guide/example/example-json-21.kt).
833
881
834
882
The output shows that both cases are correctly deserialized into a Kotlin [List].
835
883
@@ -881,7 +929,7 @@ fun main() {
881
929
}
882
930
```
883
931
884
-
> You can get the full code [here](../guide/example/example-json-21.kt).
932
+
> You can get the full code [here](../guide/example/example-json-22.kt).
885
933
886
934
You end up with a single JSON object, not an array with one element:
887
935
@@ -926,7 +974,7 @@ fun main() {
926
974
}
927
975
```
928
976
929
-
> You can get the full code [here](../guide/example/example-json-22.kt).
977
+
> You can get the full code [here](../guide/example/example-json-23.kt).
930
978
931
979
See the effect of the custom serializer:
932
980
@@ -999,7 +1047,7 @@ fun main() {
999
1047
}
1000
1048
```
1001
1049
1002
-
> You can get the full code [here](../guide/example/example-json-23.kt).
1050
+
> You can get the full code [here](../guide/example/example-json-24.kt).
1003
1051
1004
1052
No class discriminator is added in the JSON output:
1005
1053
@@ -1095,7 +1143,7 @@ fun main() {
1095
1143
}
1096
1144
```
1097
1145
1098
-
> You can get the full code [here](../guide/example/example-json-24.kt).
1146
+
> You can get the full code [here](../guide/example/example-json-25.kt).
1099
1147
1100
1148
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
1101
1149
@@ -1160,7 +1208,7 @@ fun main() {
1160
1208
}
1161
1209
```
1162
1210
1163
-
> You can get the full code [here](../guide/example/example-json-25.kt).
1211
+
> You can get the full code [here](../guide/example/example-json-26.kt).
0 commit comments