4
4
5
5
This is the second chapter of the [ Kotlin Serialization Guide] ( serialization-guide.md ) .
6
6
In addition to all the primitive types and strings, serialization for some classes from the Kotlin standard library,
7
- including the standard collections, is built into the Kotlin Serialization. This chapter explains the details.
7
+ including the standard collections, is built into Kotlin Serialization. This chapter explains the details.
8
8
9
9
** Table of contents**
10
10
@@ -33,9 +33,9 @@ import kotlinx.serialization.json.*
33
33
34
34
## Primitives
35
35
36
- Kotlin serialization has the following ten primitives:
36
+ Kotlin Serialization has the following ten primitives:
37
37
` Boolean ` , ` Byte ` , ` Short ` , ` Int ` , ` Long ` , ` Float ` , ` Double ` , ` Char ` , ` String ` , and enums.
38
- The other types in Kotlin serialization are _ composite_ &mdash ; composed of those primitive values.
38
+ The other types in Kotlin Serialization are _ composite_ &mdash ; composed of those primitive values.
39
39
40
40
### Numbers
41
41
@@ -68,7 +68,7 @@ Their natural representation in JSON is used.
68
68
69
69
<!-- - TEST -->
70
70
71
- > Experimental unsigned numbers as well as other experimental inline classes are not supported by Kotlin serialization yet.
71
+ > Experimental unsigned numbers as well as other experimental inline classes are not supported by Kotlin Serialization yet.
72
72
73
73
74
74
### Long numbers
@@ -87,7 +87,7 @@ fun main() {
87
87
88
88
> You can get the full code [ here] ( ../guide/example/example-builtin-02.kt ) .
89
89
90
- By default, they are serialized to JSON as numbers.
90
+ By default they are serialized to JSON as numbers.
91
91
92
92
``` text
93
93
{"signature":2067120338512882656}
@@ -97,17 +97,17 @@ By default, they are serialized to JSON as numbers.
97
97
98
98
### Long numbers as strings
99
99
100
- The JSON output from the previous example will get decoded normally by Kotlin serialization running on Kotlin/JS.
100
+ The JSON output from the previous example will get decoded normally by Kotlin Serialization running on Kotlin/JS.
101
101
However, if we try to parse this JSON by native JavaScript methods, we get this truncated result.
102
102
103
103
```
104
104
JSON.parse("{\"signature\":2067120338512882656}")
105
105
▶ {signature: 2067120338512882700}
106
106
```
107
107
108
- The full range of Kotlin Long does not fit in the JavaScript number, so its precision gets lost in JavaScript.
109
- A common workaround is to represent long numbers with full precision using JSON string type.
110
- This approach is optionally supported by Kotlin serialization with [ LongAsStringSerializer] that
108
+ The full range of a Kotlin Long does not fit in the JavaScript number, so its precision gets lost in JavaScript.
109
+ A common workaround is to represent long numbers with full precision using the JSON string type.
110
+ This approach is optionally supported by Kotlin Serialization with [ LongAsStringSerializer] , which
111
111
can be specified for a given Long property using the [ ` @Serializable ` ] [ Serializable ] annotation:
112
112
113
113
<!-- - INCLUDE
@@ -160,7 +160,7 @@ fun main() {
160
160
161
161
> You can get the full code [ here] ( ../guide/example/example-builtin-04.kt ) .
162
162
163
- In JSON enum gets encoded as a string.
163
+ In JSON an enum gets encoded as a string.
164
164
165
165
``` text
166
166
{"name":"kotlinx.serialization","status":"SUPPORTED"}
@@ -170,7 +170,7 @@ In JSON enum gets encoded as a string.
170
170
171
171
### Serial names of enum entries
172
172
173
- Serial names of enum entries can be customized with [ SerialName] annotation just like
173
+ Serial names of enum entries can be customized with the [ SerialName] annotation just like
174
174
it was shown for properties in the [ Serial field names] ( basic-serialization.md#serial-field-names ) section.
175
175
However, in this case, the whole enum class must be marked with the [ ` @Serializable ` ] [ Serializable ] annotation.
176
176
@@ -199,11 +199,11 @@ We see that the specified serial name is now used in the resulting JSON.
199
199
200
200
## Composites
201
201
202
- A number of composite types from the standard library are supported by Kotlin serialization .
202
+ A number of composite types from the standard library are supported by Kotlin Serialization .
203
203
204
204
### Pair and triple
205
205
206
- Simple data classes [ Pair] and [ Triple] from the Kotlin standard library are serializable.
206
+ The simple data classes [ Pair] and [ Triple] from the Kotlin standard library are serializable.
207
207
208
208
``` kotlin
209
209
@Serializable
@@ -223,7 +223,7 @@ fun main() {
223
223
224
224
<!-- - TEST -->
225
225
226
- > Not all classes from the Kotlin standard library are serializable. In particular, ranges and [ Regex] class
226
+ > Not all classes from the Kotlin standard library are serializable. In particular, ranges and the [ Regex] class
227
227
> are not serializable at the moment. Support for their serialization may be added in the future.
228
228
229
229
### Lists
@@ -255,7 +255,7 @@ The result is represented as a list in JSON.
255
255
256
256
### Sets and other collections
257
257
258
- Other collections, like a [ Set] , are also serializable.
258
+ Other collections, like [ Set] , are also serializable.
259
259
260
260
``` kotlin
261
261
@Serializable
@@ -272,7 +272,7 @@ fun main() {
272
272
273
273
> You can get the full code [ here] ( ../guide/example/example-builtin-08.kt ) .
274
274
275
- The [ Set] is also represented as a list in JSON, like all other collections.
275
+ [ Set] is also represented as a list in JSON, like all other collections.
276
276
277
277
``` text
278
278
[{"name":"kotlinx.serialization"},{"name":"kotlinx.coroutines"}]
@@ -283,7 +283,7 @@ The [Set] is also represented as a list in JSON, like all other collections.
283
283
### Deserializing collections
284
284
285
285
During deserialization, the type of the resulting object is determined by the static type that was specified
286
- in the source code &mdash ; either as the type of the property or as the type parameter of the decoding function.
286
+ in the source code&mdash ; either as the type of the property or as the type parameter of the decoding function.
287
287
The following example shows how the same JSON list of integers is deserialized into two properties of
288
288
different Kotlin types.
289
289
@@ -307,7 +307,7 @@ fun main() {
307
307
308
308
> You can get the full code [ here] ( ../guide/example/example-builtin-09.kt ) .
309
309
310
- Because ` data.b ` property is a [ Set] , the duplicate values from it had disappeared.
310
+ Because the ` data.b ` property is a [ Set] , the duplicate values from it disappeared.
311
311
312
312
``` text
313
313
Data(a=[42, 42], b=[42])
@@ -317,7 +317,7 @@ Data(a=[42, 42], b=[42])
317
317
318
318
### Maps
319
319
320
- A [ Map] with a primitive or enum keys and an arbitrary serializable values can be serialized.
320
+ A [ Map] with primitive or enum keys and arbitrary serializable values can be serialized.
321
321
322
322
``` kotlin
323
323
@Serializable
@@ -344,16 +344,16 @@ even if they are numbers in Kotlin, as we can see below.
344
344
<!-- - TEST -->
345
345
346
346
> It is a JSON-specific limitation that keys cannot be composite.
347
- > It can be lifted as shown in [ Allowing structured map keys] ( json.md#allowing-structured-map-keys ) section.
347
+ > It can be lifted as shown in the [ Allowing structured map keys] ( json.md#allowing-structured-map-keys ) section.
348
348
349
349
350
350
### Unit and singleton objects
351
351
352
- Kotlin builtin ` Unit ` type is also serializable.
353
- ` Unit ` is a Kotlin [ singleton object] ( https://kotlinlang.org/docs/tutorials/kotlin-for-py/objects-and-companion-objects.html )
352
+ The Kotlin builtin ` Unit ` type is also serializable.
353
+ ` Unit ` is a Kotlin [ singleton object] ( https://kotlinlang.org/docs/tutorials/kotlin-for-py/objects-and-companion-objects.html ) ,
354
354
and is handled equally with other Kotlin objects.
355
355
356
- Conceptually, singleton is a class with the only instance, meaning that state does not define the object,
356
+ Conceptually, a singleton is a class with only one instance, meaning that state does not define the object,
357
357
but the object defines its state. In JSON, objects are serialized as empty structures.
358
358
359
359
``` kotlin
@@ -370,8 +370,8 @@ fun main() {
370
370
371
371
> You can get the full code [ here] ( ../guide/example/example-builtin-11.kt ) .
372
372
373
- While it may seem useless at the first glance, this comes handy for sealed class serialization,
374
- which is explained in [ Polymorphism. Objects] ( polymorphism.md#objects ) section.
373
+ While it may seem useless at first glance, this comes in handy for sealed class serialization,
374
+ which is explained in the [ Polymorphism. Objects] ( polymorphism.md#objects ) section.
375
375
376
376
``` text
377
377
{}
0 commit comments