Skip to content

docs(serializers): Fix grammatical errors #2779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a2b80ea
docs(serializers): Add Missing outputs for certain examples
jamhour1g Aug 12, 2024
749d1ba
docs(serializers): Improve wording in the Custom serializers section
jamhour1g Aug 12, 2024
a8deff1
docs(serializers): Improve sentence clarity and consistency
jamhour1g Aug 12, 2024
614406d
docs(Encoding.kt): Fix a typo in encodeSerializableValue docs
jamhour1g Aug 12, 2024
76e07e4
docs(Decoding.kt): Fix typos in decodeSerializableValue docs
jamhour1g Aug 12, 2024
7ccc2bf
docs(serializers): Fix typos
jamhour1g Aug 12, 2024
9edc238
docs(serializers): simplify the phrasing for better understanding
jamhour1g Aug 12, 2024
bf5a080
docs(serializers): Improve docs for Passing a serializer manually
jamhour1g Aug 13, 2024
d9f0e9e
docs(serializers): Fix grammar in typealias serializer documentation
jamhour1g Aug 13, 2024
b937893
docs(serializers): Improve phrasing in the Serializers module section
jamhour1g Aug 13, 2024
02198ce
Merge branch 'refs/heads/add-missing-outputs' into serializers-changes
jamhour1g Aug 15, 2024
822e511
Merge branch 'refs/heads/better-explain-passing-a-serializer-manually…
jamhour1g Aug 15, 2024
0d938a6
Merge branch 'refs/heads/fix-typo-in-delegating-serializers' into ser…
jamhour1g Aug 15, 2024
f6238c9
Merge branch 'refs/heads/fix-typo-in-serialization-api-docs' into ser…
jamhour1g Aug 15, 2024
5b62e3f
Merge branch 'refs/heads/improve-serializers-module' into serializers…
jamhour1g Aug 15, 2024
7727b7e
Merge branch 'refs/heads/improve-wording' into serializers-changes
jamhour1g Aug 15, 2024
bbd0fe9
Merge branch 'refs/heads/improve-wording-for-primitive-serializer-sec…
jamhour1g Aug 15, 2024
4bd411e
Merge branch 'refs/heads/simplify-sequential-decoding-protocol-phrasi…
jamhour1g Aug 15, 2024
de71312
docs(serialization-guide): Regenerate Table of Contents and Fix Tests
jamhour1g Aug 15, 2024
d230a2a
docs(serialization-guide, serializers): Add articles
jamhour1g Aug 16, 2024
0475d04
docs(serialization-guide, serializers): Change 'Hand-written' to 'Han…
jamhour1g Aug 16, 2024
6213f69
docs(serializers): Fix broken anchor links in the table of contents.
jamhour1g Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ public interface Decoder {

/**
* Decodes the value of type [T] by delegating the decoding process to the given [deserializer].
* For example, `decodeInt` call us equivalent to delegating integer decoding to [Int.serializer][Int.Companion.serializer]:
* `decodeSerializableValue(IntSerializer)`
* For example, `decodeInt` call is equivalent to delegating integer decoding to [Int.serializer][Int.Companion.serializer]:
* `decodeSerializableValue(Int.serializer())`
*/
public fun <T : Any?> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T =
deserializer.deserialize(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public interface Encoder {

/**
* Encodes the [value] of type [T] by delegating the encoding process to the given [serializer].
* For example, `encodeInt` call us equivalent to delegating integer encoding to [Int.serializer][Int.Companion.serializer]:
* For example, `encodeInt` call is equivalent to delegating integer encoding to [Int.serializer][Int.Companion.serializer]:
* `encodeSerializableValue(Int.serializer())`
*/
public fun <T : Any?> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {
Expand Down
2 changes: 1 addition & 1 deletion docs/serialization-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Once the project is set up, we can start serializing some classes.
* <a name='specifying-serializer-on-a-property'></a>[Specifying serializer on a property](serializers.md#specifying-serializer-on-a-property)
* <a name='specifying-serializer-for-a-particular-type'></a>[Specifying serializer for a particular type](serializers.md#specifying-serializer-for-a-particular-type)
* <a name='specifying-serializers-for-a-file'></a>[Specifying serializers for a file](serializers.md#specifying-serializers-for-a-file)
* <a name='specifying-serializer-globally-using-typealias'></a>[Specifying serializer globally using typealias](serializers.md#specifying-serializer-globally-using-typealias)
* <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)
* <a name='custom-serializers-for-a-generic-type'></a>[Custom serializers for a generic type](serializers.md#custom-serializers-for-a-generic-type)
* <a name='format-specific-serializers'></a>[Format-specific serializers](serializers.md#format-specific-serializers)
* <a name='contextual-serialization'></a>[Contextual serialization](serializers.md#contextual-serialization)
Expand Down
55 changes: 34 additions & 21 deletions docs/serializers.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ In this chapter we'll take a look at serializers in more detail, and we'll see h
* [Specifying serializer on a property](#specifying-serializer-on-a-property)
* [Specifying serializer for a particular type](#specifying-serializer-for-a-particular-type)
* [Specifying serializers for a file](#specifying-serializers-for-a-file)
* [Specifying serializer globally using typealias](#specifying-serializer-globally-using-typealias)
* [Specifying a serializer globally using a typealias](#specifying-a-serializer-globally-using-a-typealias)
* [Custom serializers for a generic type](#custom-serializers-for-a-generic-type)
* [Format-specific serializers](#format-specific-serializers)
* [Contextual serialization](#contextual-serialization)
Expand Down Expand Up @@ -164,9 +164,11 @@ fun main() {

> You can get the full code [here](../guide/example/example-serializer-04.kt).

<!--- TEST
```text
PrimitiveDescriptor(kotlin.Int)
-->
```

<!--- TEST -->

### Constructing collection serializers

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

> You can get the full code [here](../guide/example/example-serializer-05.kt).

<!--- TEST
```text
kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
-->
```

<!--- TEST -->

### Using top-level serializer function

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

> You can get the full code [here](../guide/example/example-serializer-06.kt).

<!--- TEST
```text
kotlin.collections.LinkedHashMap(PrimitiveDescriptor(kotlin.String), Color(rgb: kotlin.Int))
-->
```

<!--- TEST -->

## Custom serializers

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

### Primitive serializer

Expand Down Expand Up @@ -253,7 +260,7 @@ object ColorAsStringSerializer : KSerializer<Color> {
}
```

Serializer has three required pieces.
A serializer has three required pieces.

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

> Example of how format can treat arrays specially is shown in the [formats guide](formats.md#format-specific-types).
> An example of how a format can treat arrays specially is shown in the [formats guide](formats.md#format-specific-types).

Now we can use the serializer:

Expand Down Expand Up @@ -615,10 +622,10 @@ As before, we got the `Color` class represented as a JSON object with three keys
### Sequential decoding protocol (experimental)

The implementation of the `deserialize` function from the previous section works with any format. However,
some formats either always store all the complex data in order, or only do so sometimes (JSON always stores
collections in order). With these formats the complex protocol of calling `decodeElementIndex` in the loop is
not needed, and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
The plugin-generated serializers are actually conceptually similar to the below code.
some formats either always store all the complex data in order or only do so sometimes (JSON always stores
collections in order). With these formats the complex protocol of calling `decodeElementIndex` in a loop is
unnecessary, and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
The plugin-generated serializers are actually conceptually similar to the code below.

<!--- INCLUDE
object ColorAsObjectSerializer : KSerializer<Color> {
Expand Down Expand Up @@ -712,9 +719,15 @@ We cannot bind the `DateAsLongSerializer` serializer to the `Date` class with th
because we don't control the `Date` source code. There are several ways to work around that.

### Passing a serializer manually

All `encodeToXxx` and `decodeFromXxx` functions have an overload with the first serializer parameter.
When a non-serializable class, like `Date`, is the top-level class being serialized, we can use those.

The `encodeToXxx` and `decodeFromXxx` functions offer overloaded versions
that accept either a [SerializationStrategy] or [DeserializationStrategy] as their first parameter, respectively.
This feature allows you
to provide a custom serializer for types that aren't annotated with [`@Serializable`][Serializable] by default.

This approach is particularly useful
when working with non-serializable classes like `Date` as the top-level object being serialized.
Here's an example:

```kotlin
fun main() {
Expand Down Expand Up @@ -851,7 +864,7 @@ fun main() {

<!--- TEST -->

### Specifying serializer globally using typealias
### Specifying a serializer globally using a typealias

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

```kotlin
Expand Down