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
This PR provides a new function for encoding raw JSON content, without quoting it as a string. This allows for encoding JSON numbers of any size or precision, so BigDecimal and BigInteger can be supported.
Fixes#1051Fixes#1405
The implementation is similar to how unsigned numbers are handled.
JsonUnquotedLiteral() is a new function that allows creating literal JSON content.
Added val coerceToInlineType to JsonLiteral, so that JsonUnquotedLiteral could use encodeInline()
Defined val jsonUnquotedLiteralDescriptor as a 'marker', for use with encodeInline()
ComposerForUnquotedLiterals (based on ComposerForUnsignedNumbers) will 'override' the encoder when a JsonLiteral has the jsonUnquotedLiteralDescriptor marker, and will encode the content as a string without surrounding quotes.
Copy file name to clipboardExpand all lines: core/api/kotlinx-serialization-core.api
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -774,6 +774,10 @@ public final class kotlinx/serialization/internal/InlineClassDescriptor : kotlin
774
774
public fun isInline ()Z
775
775
}
776
776
777
+
public final class kotlinx/serialization/internal/InlineClassDescriptorKt {
778
+
public static final fun InlinePrimitiveDescriptor (Ljava/lang/String;Lkotlinx/serialization/KSerializer;)Lkotlinx/serialization/descriptors/SerialDescriptor;
779
+
}
780
+
777
781
public final class kotlinx/serialization/internal/IntArrayBuilder : kotlinx/serialization/internal/PrimitiveArrayBuilder {
778
782
public synthetic fun build$kotlinx_serialization_core ()Ljava/lang/Object;
*[Serializing large decimal numbers](#serializing-large-decimal-numbers)
30
+
*[Using `JsonUnquotedLiteral` to create a literal unquoted value of `null` is forbidden](#using-jsonunquotedliteral-to-create-a-literal-unquoted-value-of-null-is-forbidden)
val piObject:JsonObject=Json.decodeFromString(piObjectJson)
725
+
726
+
val piJsonLiteral = piObject["pi_literal"]!!.jsonPrimitive.content
727
+
728
+
val pi =BigDecimal(piJsonLiteral)
729
+
730
+
println(pi)
731
+
}
732
+
```
733
+
734
+
> You can get the full code [here](../guide/example/example-json-18.kt).
735
+
736
+
The exact value of `pi` is decoded, with all 30 decimal places of precision that were in the source JSON.
737
+
738
+
```text
739
+
3.141592653589793238462643383279
740
+
```
741
+
742
+
<!--- TEST -->
743
+
744
+
#### Using `JsonUnquotedLiteral` to create a literal unquoted value of `null` is forbidden
745
+
746
+
To avoid creating an inconsistent state, encoding a String equal to `"null"` is forbidden.
747
+
Use [JsonNull] or [JsonPrimitive] instead.
748
+
749
+
```kotlin
750
+
funmain() {
751
+
// caution: creating null with JsonUnquotedLiteral will cause an exception!
752
+
JsonUnquotedLiteral("null")
753
+
}
754
+
```
755
+
756
+
> You can get the full code [here](../guide/example/example-json-19.kt).
757
+
758
+
```text
759
+
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
760
+
```
761
+
762
+
<!--- TEST LINES_START -->
763
+
764
+
615
765
## Json transformations
616
766
617
767
To affect the shape and contents of JSON output after serialization, or adapt input to deserialization,
@@ -679,7 +829,7 @@ fun main() {
679
829
}
680
830
```
681
831
682
-
> You can get the full code [here](../guide/example/example-json-16.kt).
832
+
> You can get the full code [here](../guide/example/example-json-20.kt).
683
833
684
834
The output shows that both cases are correctly deserialized into a Kotlin [List].
685
835
@@ -731,7 +881,7 @@ fun main() {
731
881
}
732
882
```
733
883
734
-
> You can get the full code [here](../guide/example/example-json-17.kt).
884
+
> You can get the full code [here](../guide/example/example-json-21.kt).
735
885
736
886
You end up with a single JSON object, not an array with one element:
737
887
@@ -776,7 +926,7 @@ fun main() {
776
926
}
777
927
```
778
928
779
-
> You can get the full code [here](../guide/example/example-json-18.kt).
929
+
> You can get the full code [here](../guide/example/example-json-22.kt).
780
930
781
931
See the effect of the custom serializer:
782
932
@@ -849,7 +999,7 @@ fun main() {
849
999
}
850
1000
```
851
1001
852
-
> You can get the full code [here](../guide/example/example-json-19.kt).
1002
+
> You can get the full code [here](../guide/example/example-json-23.kt).
853
1003
854
1004
No class discriminator is added in the JSON output:
855
1005
@@ -945,7 +1095,7 @@ fun main() {
945
1095
}
946
1096
```
947
1097
948
-
> You can get the full code [here](../guide/example/example-json-20.kt).
1098
+
> You can get the full code [here](../guide/example/example-json-24.kt).
949
1099
950
1100
This gives you fine-grained control on the representation of the `Response` class in the JSON output:
951
1101
@@ -1010,7 +1160,7 @@ fun main() {
1010
1160
}
1011
1161
```
1012
1162
1013
-
> You can get the full code [here](../guide/example/example-json-21.kt).
1163
+
> You can get the full code [here](../guide/example/example-json-25.kt).
* <aname='serializing-large-decimal-numbers'></a>[Serializing large decimal numbers](json.md#serializing-large-decimal-numbers)
128
+
* <aname='using-jsonunquotedliteral-to-create-a-literal-unquoted-value-of-null-is-forbidden'></a>[Using `JsonUnquotedLiteral` to create a literal unquoted value of `null` is forbidden](json.md#using-jsonunquotedliteral-to-create-a-literal-unquoted-value-of-null-is-forbidden)
0 commit comments