Skip to content

Commit d344086

Browse files
authored
docs(serializers): Fix grammatical errors (#2779)
- "Let's study the alternatives" is slightly more formal and emphasizes the specific alternatives. - Replace "Serializer has three required pieces" with "A serializer has three required pieces" for better specificity, consistency, and readability. - Clarify explanation of encodeToXxx and decodeFromXxx overloads - Add missing articles to the section header - Change "Specifying serializer globally using typealias" to "Specifying a serializer globally using a typealias" - Improve readability and grammatical correctness - Change "In the below example" to "In the example below" - Enhance readability and professionalism of text - Align with common technical writing conventions - Change "Specifying serializer on a property" to "Specifying a serializer on a property" - Change "Specifying serializer for a particular type" to "Specifying a serializer for a particular type" - Correct the spelling of 'Hand-written' to 'Handwritten,' as it is commonly spelled as one word.
1 parent 98a9502 commit d344086

File tree

4 files changed

+49
-36
lines changed

4 files changed

+49
-36
lines changed

core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ public interface Decoder {
250250

251251
/**
252252
* Decodes the value of type [T] by delegating the decoding process to the given [deserializer].
253-
* For example, `decodeInt` call us equivalent to delegating integer decoding to [Int.serializer][Int.Companion.serializer]:
254-
* `decodeSerializableValue(IntSerializer)`
253+
* For example, `decodeInt` call is equivalent to delegating integer decoding to [Int.serializer][Int.Companion.serializer]:
254+
* `decodeSerializableValue(Int.serializer())`
255255
*/
256256
public fun <T : Any?> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T =
257257
deserializer.deserialize(this)

core/commonMain/src/kotlinx/serialization/encoding/Encoding.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public interface Encoder {
272272

273273
/**
274274
* Encodes the [value] of type [T] by delegating the encoding process to the given [serializer].
275-
* For example, `encodeInt` call us equivalent to delegating integer encoding to [Int.serializer][Int.Companion.serializer]:
275+
* For example, `encodeInt` call is equivalent to delegating integer encoding to [Int.serializer][Int.Companion.serializer]:
276276
* `encodeSerializableValue(Int.serializer())`
277277
*/
278278
public fun <T : Any?> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {

docs/serialization-guide.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ Once the project is set up, we can start serializing some classes.
6666
* <a name='primitive-serializer'></a>[Primitive serializer](serializers.md#primitive-serializer)
6767
* <a name='delegating-serializers'></a>[Delegating serializers](serializers.md#delegating-serializers)
6868
* <a name='composite-serializer-via-surrogate'></a>[Composite serializer via surrogate](serializers.md#composite-serializer-via-surrogate)
69-
* <a name='hand-written-composite-serializer'></a>[Hand-written composite serializer](serializers.md#hand-written-composite-serializer)
69+
* <a name='handwritten-composite-serializer'></a>[Handwritten composite serializer](serializers.md#handwritten-composite-serializer)
7070
* <a name='sequential-decoding-protocol-experimental'></a>[Sequential decoding protocol (experimental)](serializers.md#sequential-decoding-protocol-experimental)
7171
* <a name='serializing-3rd-party-classes'></a>[Serializing 3rd party classes](serializers.md#serializing-3rd-party-classes)
7272
* <a name='passing-a-serializer-manually'></a>[Passing a serializer manually](serializers.md#passing-a-serializer-manually)
73-
* <a name='specifying-serializer-on-a-property'></a>[Specifying serializer on a property](serializers.md#specifying-serializer-on-a-property)
74-
* <a name='specifying-serializer-for-a-particular-type'></a>[Specifying serializer for a particular type](serializers.md#specifying-serializer-for-a-particular-type)
73+
* <a name='specifying-a-serializer-on-a-property'></a>[Specifying a serializer on a property](serializers.md#specifying-a-serializer-on-a-property)
74+
* <a name='specifying-a-serializer-for-a-particular-type'></a>[Specifying a serializer for a particular type](serializers.md#specifying-a-serializer-for-a-particular-type)
7575
* <a name='specifying-serializers-for-a-file'></a>[Specifying serializers for a file](serializers.md#specifying-serializers-for-a-file)
76-
* <a name='specifying-serializer-globally-using-typealias'></a>[Specifying serializer globally using typealias](serializers.md#specifying-serializer-globally-using-typealias)
76+
* <a name='specifying-a-serializer-globally-using-a-typealias'></a>[Specifying a serializer globally using a typealias](serializers.md#specifying-a-serializer-globally-using-a-typealias)
7777
* <a name='custom-serializers-for-a-generic-type'></a>[Custom serializers for a generic type](serializers.md#custom-serializers-for-a-generic-type)
7878
* <a name='format-specific-serializers'></a>[Format-specific serializers](serializers.md#format-specific-serializers)
7979
* <a name='contextual-serialization'></a>[Contextual serialization](serializers.md#contextual-serialization)

docs/serializers.md

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ In this chapter we'll take a look at serializers in more detail, and we'll see h
1919
* [Primitive serializer](#primitive-serializer)
2020
* [Delegating serializers](#delegating-serializers)
2121
* [Composite serializer via surrogate](#composite-serializer-via-surrogate)
22-
* [Hand-written composite serializer](#hand-written-composite-serializer)
22+
* [Handwritten composite serializer](#handwritten-composite-serializer)
2323
* [Sequential decoding protocol (experimental)](#sequential-decoding-protocol-experimental)
2424
* [Serializing 3rd party classes](#serializing-3rd-party-classes)
2525
* [Passing a serializer manually](#passing-a-serializer-manually)
26-
* [Specifying serializer on a property](#specifying-serializer-on-a-property)
27-
* [Specifying serializer for a particular type](#specifying-serializer-for-a-particular-type)
26+
* [Specifying a serializer on a property](#specifying-a-serializer-on-a-property)
27+
* [Specifying a serializer for a particular type](#specifying-a-serializer-for-a-particular-type)
2828
* [Specifying serializers for a file](#specifying-serializers-for-a-file)
29-
* [Specifying serializer globally using typealias](#specifying-serializer-globally-using-typealias)
29+
* [Specifying a serializer globally using a typealias](#specifying-a-serializer-globally-using-a-typealias)
3030
* [Custom serializers for a generic type](#custom-serializers-for-a-generic-type)
3131
* [Format-specific serializers](#format-specific-serializers)
3232
* [Contextual serialization](#contextual-serialization)
@@ -164,9 +164,11 @@ fun main() {
164164

165165
> You can get the full code [here](../guide/example/example-serializer-04.kt).
166166
167-
<!--- TEST
167+
```text
168168
PrimitiveDescriptor(kotlin.Int)
169-
-->
169+
```
170+
171+
<!--- TEST -->
170172

171173
### Constructing collection serializers
172174

@@ -190,9 +192,11 @@ fun main() {
190192

191193
> You can get the full code [here](../guide/example/example-serializer-05.kt).
192194
193-
<!--- TEST
195+
```text
194196
kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
195-
-->
197+
```
198+
199+
<!--- TEST -->
196200

197201
### Using top-level serializer function
198202

@@ -216,14 +220,17 @@ fun main() {
216220

217221
> You can get the full code [here](../guide/example/example-serializer-06.kt).
218222
219-
<!--- TEST
223+
```text
220224
kotlin.collections.LinkedHashMap(PrimitiveDescriptor(kotlin.String), Color(rgb: kotlin.Int))
221-
-->
225+
```
226+
227+
<!--- TEST -->
222228

223229
## Custom serializers
224230

225231
A plugin-generated serializer is convenient, but it may not produce the JSON we want
226-
for such a class as `Color`. Let's study alternatives.
232+
for such a class as `Color`.
233+
Let's study the alternatives.
227234

228235
### Primitive serializer
229236

@@ -253,7 +260,7 @@ object ColorAsStringSerializer : KSerializer<Color> {
253260
}
254261
```
255262

256-
Serializer has three required pieces.
263+
A serializer has three required pieces.
257264

258265
* The [serialize][SerializationStrategy.serialize] function implements [SerializationStrategy].
259266
It receives an instance of [Encoder] and a value to serialize.
@@ -417,10 +424,10 @@ class ColorIntArraySerializer : KSerializer<Color> {
417424
Note that we can't use default `Color.serializer().descriptor` here because formats that rely
418425
on the schema may think that we would call `encodeInt` instead of `encodeSerializableValue`.
419426
Neither we can use `IntArraySerializer().descriptor` directly — otherwise, formats that handle int arrays specially
420-
can't tell if `value` is really a `IntArray` or a `Color`. Don't worry, this optimization would still kick in
421-
when serializing actual underlying int array.
427+
can't tell if `value` is really an `IntArray` or a `Color`.
428+
Don't worry, this optimization would still kick in when serializing the actual underlying int array.
422429

423-
> Example of how format can treat arrays specially is shown in the [formats guide](formats.md#format-specific-types).
430+
> An example of how a format can treat arrays specially is shown in the [formats guide](formats.md#format-specific-types).
424431
425432
Now we can use the serializer:
426433

@@ -516,7 +523,7 @@ fun main() {
516523

517524
<!--- TEST -->
518525

519-
### Hand-written composite serializer
526+
### Handwritten composite serializer
520527

521528
There are some cases where a surrogate solution does not fit. Perhaps we want to avoid the performance
522529
implications of additional allocation, or we want a configurable/dynamic set of properties for the
@@ -615,10 +622,10 @@ As before, we got the `Color` class represented as a JSON object with three keys
615622
### Sequential decoding protocol (experimental)
616623
617624
The implementation of the `deserialize` function from the previous section works with any format. However,
618-
some formats either always store all the complex data in order, or only do so sometimes (JSON always stores
619-
collections in order). With these formats the complex protocol of calling `decodeElementIndex` in the loop is
620-
not needed, and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
621-
The plugin-generated serializers are actually conceptually similar to the below code.
625+
some formats either always store all the complex data in order or only do so sometimes (JSON always stores
626+
collections in order). With these formats the complex protocol of calling `decodeElementIndex` in a loop is
627+
unnecessary, and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
628+
The plugin-generated serializers are actually conceptually similar to the code below.
622629
623630
<!--- INCLUDE
624631
object ColorAsObjectSerializer : KSerializer<Color> {
@@ -712,9 +719,15 @@ We cannot bind the `DateAsLongSerializer` serializer to the `Date` class with th
712719
because we don't control the `Date` source code. There are several ways to work around that.
713720

714721
### Passing a serializer manually
715-
716-
All `encodeToXxx` and `decodeFromXxx` functions have an overload with the first serializer parameter.
717-
When a non-serializable class, like `Date`, is the top-level class being serialized, we can use those.
722+
723+
The `encodeToXxx` and `decodeFromXxx` functions offer overloaded versions
724+
that accept either a [SerializationStrategy] or [DeserializationStrategy] as their first parameter, respectively.
725+
This feature allows you
726+
to provide a custom serializer for types that aren't annotated with [`@Serializable`][Serializable] by default.
727+
728+
This approach is particularly useful
729+
when working with non-serializable classes like `Date` as the top-level object being serialized.
730+
Here's an example:
718731

719732
```kotlin
720733
fun main() {
@@ -731,7 +744,7 @@ fun main() {
731744

732745
<!--- TEST -->
733746

734-
### Specifying serializer on a property
747+
### Specifying a serializer on a property
735748

736749
When a property of a non-serializable class, like `Date`, is serialized as part of a serializable class we must supply
737750
its serializer or the code will not compile. This is accomplished using the [`@Serializable`][Serializable] annotation on the property.
@@ -771,7 +784,7 @@ The `stableReleaseDate` property is serialized with the serialization strategy t
771784

772785
<!--- TEST -->
773786

774-
### Specifying serializer for a particular type
787+
### Specifying a serializer for a particular type
775788

776789
[`@Serializable`][Serializable] annotation can also be applied directly to the types.
777790
This is handy when a class that requires a custom serializer, such as `Date`, happens to be a generic type argument.
@@ -810,7 +823,7 @@ fun main() {
810823

811824
<!--- TEST -->
812825

813-
### Specifying serializers for a file
826+
### Specifying serializers for a file
814827

815828
A serializer for a specific type, like `Date`, can be specified for a whole source code file with the file-level
816829
[UseSerializers] annotation at the beginning of the file.
@@ -851,7 +864,7 @@ fun main() {
851864

852865
<!--- TEST -->
853866

854-
### Specifying serializer globally using typealias
867+
### Specifying a serializer globally using a typealias
855868

856869
kotlinx.serialization tends to be the always-explicit framework when it comes to serialization strategies: normally,
857870
they should be explicitly mentioned in `@Serializable` annotation. Therefore, we do not provide any kind of global serializer
@@ -1045,7 +1058,7 @@ class ProgrammingLanguage(
10451058
To provide a context, we define a [SerializersModule] instance that describes which serializers shall be used
10461059
at run-time to serialize which contextually-serializable classes. This is done using the
10471060
[SerializersModule {}][SerializersModule()] builder function, which provides the [SerializersModuleBuilder] DSL to
1048-
register serializers. In the below example we use the [contextual][_contextual] function with the serializer. The corresponding
1061+
register serializers. In the example below we use the [contextual][_contextual] function with the serializer. The corresponding
10491062
class this serializer is defined for is fetched automatically via the `reified` type parameter.
10501063

10511064
```kotlin
@@ -1142,7 +1155,7 @@ This gets all the `Project` properties serialized:
11421155

11431156
<!--- TEST -->
11441157

1145-
### External serialization uses properties
1158+
### External serialization uses properties
11461159

11471160
As we saw earlier, the regular `@Serializable` annotation creates a serializer so that
11481161
[Backing fields are serialized](basic-serialization.md#backing-fields-are-serialized). _External_ serialization using

0 commit comments

Comments
 (0)