Skip to content

Commit 728e220

Browse files
authored
Fixed some typos in the basics chapter. (#1214)
1 parent 196d06c commit 728e220

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

docs/basic-serialization.md

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ This chapter shows the basic use of Kotlin Serialization and explains its core c
3333
## Basics
3434

3535
To convert an object tree to a string or to a sequence of bytes, it must come
36-
through two mutually intertwined processes. In the first step, an object is _serialized_ —
37-
it is converted into a serial sequence of its constituting primitive values. This process is common for all
36+
through two mutually intertwined processes. In the first step, an object is _serialized_—it
37+
is converted into a serial sequence of its constituting primitive values. This process is common for all
3838
data formats and its result depends on the object being serialized. A _serializer_ controls this process.
39-
The second step is called _encoding_ — it is the conversion of the corresponding sequence of primitives into
39+
The second step is called _encoding_—it is the conversion of the corresponding sequence of primitives into
4040
the output format representation. An _encoder_ controls this process. Whenever the distinction is not important,
4141
both the terms of encoding and serialization are used interchangeably.
4242

@@ -58,10 +58,9 @@ import kotlinx.serialization.json.*
5858

5959
### JSON encoding
6060

61-
The whole process of converting data into a specific format is called _encoding_. For JSON we
62-
encode data using the [Json.encodeToString][kotlinx.serialization.encodeToString] extension function.
63-
It serializes the object
64-
that is passed as its parameter under the hood and encodes it to a JSON string.
61+
The whole process of converting data into a specific format is called _encoding_. For JSON we encode data
62+
using the [Json.encodeToString][kotlinx.serialization.encodeToString] extension function. It serializes
63+
the object that is passed as its parameter under the hood and encodes it to a JSON string.
6564

6665
Let's start with a class describing a project and try to get its JSON representation.
6766

@@ -85,7 +84,7 @@ Mark the class as @Serializable or provide the serializer explicitly.
8584

8685
<!--- TEST LINES_START -->
8786

88-
Serializable classes have to be explicitly marked. Kotlin serialization does not use reflection,
87+
Serializable classes have to be explicitly marked. Kotlin Serialization does not use reflection,
8988
so you cannot accidentally deserialize a class which was not supposed to be serializable. We fix it by
9089
adding the [`@Serializable`][Serializable] annotation.
9190

@@ -101,7 +100,7 @@ fun main() {
101100

102101
> You can get the full code [here](../guide/example/example-basic-02.kt).
103102
104-
The `@Serializable` annotation instructs Kotlin Serialization plugin to automatically generate and hook
103+
The `@Serializable` annotation instructs the Kotlin Serialization plugin to automatically generate and hook
105104
up a _serializer_ for this class. Now the output of the example is the corresponding JSON.
106105

107106
```text
@@ -111,17 +110,16 @@ up a _serializer_ for this class. Now the output of the example is the correspon
111110
<!--- TEST -->
112111

113112
> There is a whole chapter about the [Serializers](serializers.md). For now, it is enough to know
114-
> that they are automatically generated by the Kotlin serialization plugin.
113+
> that they are automatically generated by the Kotlin Serialization plugin.
115114
116115
### JSON decoding
117116

118117
The reverse process is called _decoding_. To decode a JSON string into an object, we'll
119118
use the [Json.decodeFromString][kotlinx.serialization.decodeFromString] extension function.
120-
To specify which type we want to get as a result,
121-
we provide a type parameter to this function.
119+
To specify which type we want to get as a result, we provide a type parameter to this function.
122120

123121
As we'll see later, serialization works with different kinds of classes.
124-
Here we are marking our `Project` class as `data class` not because it is required, but because
122+
Here we are marking our `Project` class as a `data class`, not because it is required, but because
125123
we want to print its contents to verify how it decodes.
126124

127125
```kotlin
@@ -157,7 +155,7 @@ import kotlinx.serialization.json.*
157155

158156
### Backing fields are serialized
159157

160-
Only class's properties with backing fields are serialized, so properties with getter/setter that don't
158+
Only a class's properties with backing fields are serialized, so properties with a getter/setter that don't
161159
have a backing field and delegated properties are not serialized, as the following example shows.
162160

163161
```kotlin
@@ -182,7 +180,7 @@ fun main() {
182180

183181
> You can get the full code [here](../guide/example/example-classes-01.kt).
184182
185-
We can clearly see that only `name` and `stars` properties are present in the JSON output.
183+
We can clearly see that only the `name` and `stars` properties are present in the JSON output.
186184

187185
```text
188186
{"name":"kotlinx.serialization","stars":9000}
@@ -192,8 +190,8 @@ We can clearly see that only `name` and `stars` properties are present in the JS
192190

193191
### Constructor properties requirement
194192

195-
If we want to define the `Project` class so that it takes a path string and then
196-
deconstructs them into the corresponding properties, then we might be tempted to write something like the code below.
193+
If we want to define the `Project` class so that it takes a path string, and then
194+
deconstructs it into the corresponding properties, we might be tempted to write something like the code below.
197195

198196
```kotlin
199197
@Serializable
@@ -205,9 +203,9 @@ class Project(path: String) {
205203

206204
<!--- CLEAR -->
207205

208-
This class does not compile, because `@Serializable` annotation requires that all parameters of the class primary
209-
constructor are properties. A simple workaround is to define a private primary constructor with class's
210-
properties and turn the constructor we wanted into the secondary one.
206+
This class does not compile because the `@Serializable` annotation requires that all parameters of the class's primary
207+
constructor be properties. A simple workaround is to define a private primary constructor with the class's
208+
properties, and turn the constructor we wanted into the secondary one.
211209

212210
```kotlin
213211
@Serializable
@@ -222,7 +220,7 @@ class Project private constructor(val owner: String, val name: String) {
222220
}
223221
```
224222

225-
Serialization works with private primary constructor and still serializes only backing fields.
223+
Serialization works with a private primary constructor, and still serializes only backing fields.
226224

227225
```kotlin
228226
fun main() {
@@ -244,7 +242,7 @@ This example produces the expected output.
244242

245243
Another case where you might want to introduce a primary constructor parameter without a property is when you
246244
want to validate its value before storing it to a property. To make it serializable you shall replace it
247-
with a property in the primary constructor and move validation to an `init { ... }` block.
245+
with a property in the primary constructor, and move the validation to an `init { ... }` block.
248246

249247
```kotlin
250248
@Serializable
@@ -269,7 +267,7 @@ fun main() {
269267

270268
> You can get the full code [here](../guide/example/example-classes-03.kt).
271269
272-
Running this code produces the exception.
270+
Running this code produces the exception:
273271

274272
```text
275273
Exception in thread "main" java.lang.IllegalArgumentException: name cannot be empty
@@ -296,7 +294,7 @@ fun main() {
296294

297295
> You can get the full code [here](../guide/example/example-classes-04.kt).
298296
299-
It produces the exception.
297+
It produces the exception:
300298

301299
```text
302300
Exception in thread "main" kotlinx.serialization.MissingFieldException: Field 'language' is required, but it was missing
@@ -305,7 +303,7 @@ Exception in thread "main" kotlinx.serialization.MissingFieldException: Field 'l
305303
<!--- TEST LINES_START -->
306304

307305
This problem can be fixed by adding a default value to the property, which automatically makes it optional
308-
for serialization:
306+
for serialization.
309307

310308
```kotlin
311309
@Serializable
@@ -354,7 +352,7 @@ fun main() {
354352

355353
> You can get the full code [here](../guide/example/example-classes-06.kt).
356354
357-
Since `language` property was specified in the input, we don't see "Computing" string printed
355+
Since the `language` property was specified in the input, we don't see the "Computing" string printed
358356
in the output.
359357

360358
```text
@@ -366,7 +364,7 @@ Project(name=kotlinx.serialization, language=Kotlin)
366364
### Required properties
367365

368366
A property with a default value can be required in a serial format with the [`@Required`][Required] annotation.
369-
Let us change the previous example by marking `language` property as `@Required`.
367+
Let us change the previous example by marking the `language` property as `@Required`.
370368

371369
```kotlin
372370
@Serializable
@@ -419,12 +417,12 @@ Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
419417

420418
<!--- TEST LINES_START -->
421419

422-
> 'ignoreUnknownKeys' feature is explained in the [Ignoring Unknown Keys section](json.md#ignoring-unknown-keys) section.
420+
> The 'ignoreUnknownKeys' feature is explained in the [Ignoring Unknown Keys section](json.md#ignoring-unknown-keys) section.
423421
424422
### Defaults are not encoded
425423

426-
Default values are not encoded by default in JSON. This behavior is motivated by the fact that in most real-life scenarios,
427-
such configuration reduces visual clutter and saves amount of data being serialized.
424+
Default values are not encoded by default in JSON. This behavior is motivated by the fact that in most real-life scenarios
425+
such configuration reduces visual clutter, and saves the amount of data being serialized.
428426

429427
```kotlin
430428
@Serializable
@@ -438,7 +436,7 @@ fun main() {
438436

439437
> You can get the full code [here](../guide/example/example-classes-09.kt).
440438
441-
It produces the following output, which does not have `language` property, because its value is equal to the default one.
439+
It produces the following output, which does not have the `language` property because its value is equal to the default one.
442440

443441
```text
444442
{"name":"kotlinx.serialization"}
@@ -450,7 +448,7 @@ It produces the following output, which does not have `language` property, becau
450448

451449
### Nullable properties
452450

453-
Nullable properties are natively supported by Kotlin serialization.
451+
Nullable properties are natively supported by Kotlin Serialization.
454452

455453
```kotlin
456454
@Serializable
@@ -474,7 +472,7 @@ This example does not encode `null` in JSON because [Defaults are not encoded](#
474472

475473
### Type safety is enforced
476474

477-
Kotlin serialization strongly enforces type safety of the Kotlin programming language.
475+
Kotlin Serialization strongly enforces the type safety of the Kotlin programming language.
478476
In particular, let us try to decode a `null` value from a JSON object into a non-nullable Kotlin property `language`.
479477

480478
```kotlin
@@ -531,14 +529,14 @@ When encoded to JSON it results in a nested JSON object.
531529
{"name":"kotlinx.serialization","owner":{"name":"kotlin"}}
532530
```
533531

534-
> References to non-serializable classes can be marked as [Transient properties](#transient-properties) or a
532+
> References to non-serializable classes can be marked as [Transient properties](#transient-properties), or a
535533
> custom serializer can be provided for them as shown in the [Serializers](serializers.md) chapter.
536534
537535
<!--- TEST -->
538536

539537
### No compression of repeated references
540538

541-
Kotlin serialization is designed for encoding and decoding of plain data. It does not support reconstruction
539+
Kotlin Serialization is designed for encoding and decoding of plain data. It does not support reconstruction
542540
of arbitrary object graphs with repeated object references. For example, let us try to serialize an object
543541
that references the same `owner` instance twice.
544542

@@ -564,22 +562,22 @@ We simply get the `owner` value encoded twice.
564562
{"name":"kotlinx.serialization","owner":{"name":"kotlin"},"maintainer":{"name":"kotlin"}}
565563
```
566564

567-
> Attempt to serialize a circular structure will end up with stack overflow.
565+
> Attempt to serialize a circular structure will result in stack overflow.
568566
> You can use the [Transient properties](#transient-properties) to exclude some references from serialization.
569567
570568
<!--- TEST -->
571569

572570
### Generic classes
573571

574-
Generic classes in Kotlin provide type-polymorphic behavior, which is enforced by Kotlin serialization at
572+
Generic classes in Kotlin provide type-polymorphic behavior, which is enforced by Kotlin Serialization at
575573
compile-time. For example, consider a generic serializable class `Box<T>`.
576574

577575
```kotlin
578576
@Serializable
579577
class Box<T>(val contents: T)
580578
```
581579

582-
The `Box<T>` class can be used with builtin types like `Int` as well as with user-defined types like `Project`.
580+
The `Box<T>` class can be used with builtin types like `Int`, as well as with user-defined types like `Project`.
583581

584582
<!--- INCLUDE
585583
@@ -610,14 +608,14 @@ The actual type that we get in JSON depends on the actual compile-time type para
610608

611609
<!--- TEST -->
612610

613-
If the actual generic type is not serializable, compile-time error will be produced.
611+
If the actual generic type is not serializable a compile-time error will be produced.
614612

615613
### Serial field names
616614

617615
The names of the properties used in encoded representation, JSON in our examples, are the same as
618-
their names in the source code by default. The name that is used for serialization is called a _serial name_ and
619-
can be changed using the [`@SerialName`][SerialName] annotation. For example, we can have a `language` property in the source,
620-
with an abbreviated serial name.
616+
their names in the source code by default. The name that is used for serialization is called a _serial name_, and
617+
can be changed using the [`@SerialName`][SerialName] annotation. For example, we can have a `language` property in
618+
the source with an abbreviated serial name.
621619

622620
```kotlin
623621
@Serializable
@@ -657,4 +655,3 @@ The next chapter covers [Builtin classes](builtin-classes.md).
657655
<!--- MODULE /kotlinx-serialization-json -->
658656
<!--- INDEX kotlinx-serialization-json/kotlinx.serialization.json -->
659657
<!--- END -->
660-

0 commit comments

Comments
 (0)