@@ -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* [ Simultaneous use of plugin-generated and custom serializers] ( #simultaneous-use-of-plugin-generated-and-custom-serializers )
@@ -165,9 +165,11 @@ fun main() {
165165
166166> You can get the full code [ here] ( ../guide/example/example-serializer-04.kt ) .
167167
168- <!-- - TEST
168+ ``` text
169169PrimitiveDescriptor(kotlin.Int)
170- -->
170+ ```
171+
172+ <!-- - TEST -->
171173
172174### Constructing collection serializers
173175
@@ -191,9 +193,11 @@ fun main() {
191193
192194> You can get the full code [ here] ( ../guide/example/example-serializer-05.kt ) .
193195
194- <!-- - TEST
196+ ``` text
195197kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
196- -->
198+ ```
199+
200+ <!-- - TEST -->
197201
198202### Using top-level serializer function
199203
@@ -217,14 +221,17 @@ fun main() {
217221
218222> You can get the full code [ here] ( ../guide/example/example-serializer-06.kt ) .
219223
220- <!-- - TEST
224+ ``` text
221225kotlin.collections.LinkedHashMap(PrimitiveDescriptor(kotlin.String), Color(rgb: kotlin.Int))
222- -->
226+ ```
227+
228+ <!-- - TEST -->
223229
224230## Custom serializers
225231
226232A plugin-generated serializer is convenient, but it may not produce the JSON we want
227- for such a class as ` Color ` . Let's study alternatives.
233+ for such a class as ` Color ` .
234+ Let's study the alternatives.
228235
229236### Primitive serializer
230237
@@ -254,7 +261,7 @@ object ColorAsStringSerializer : KSerializer<Color> {
254261}
255262```
256263
257- Serializer has three required pieces.
264+ A serializer has three required pieces.
258265
259266* The [ serialize] [ SerializationStrategy.serialize ] function implements [ SerializationStrategy] .
260267 It receives an instance of [ Encoder] and a value to serialize.
@@ -419,10 +426,10 @@ class ColorIntArraySerializer : KSerializer<Color> {
419426Note that we can't use default ` Color.serializer().descriptor ` here because formats that rely
420427on the schema may think that we would call ` encodeInt ` instead of ` encodeSerializableValue ` .
421428Neither we can use ` IntArraySerializer().descriptor ` directly — otherwise, formats that handle int arrays specially
422- can't tell if ` value ` is really a ` IntArray ` or a ` Color ` . Don't worry, this optimization would still kick in
423- when serializing actual underlying int array.
429+ can't tell if ` value ` is really an ` IntArray ` or a ` Color ` .
430+ Don't worry, this optimization would still kick in when serializing the actual underlying int array.
424431
425- > Example of how format can treat arrays specially is shown in the [ formats guide] ( formats.md#format-specific-types ) .
432+ > An example of how a format can treat arrays specially is shown in the [ formats guide] ( formats.md#format-specific-types ) .
426433
427434Now we can use the serializer:
428435
@@ -518,7 +525,7 @@ fun main() {
518525
519526<!-- - TEST -->
520527
521- ### Hand-written composite serializer
528+ ### Handwritten composite serializer
522529
523530There are some cases where a surrogate solution does not fit. Perhaps we want to avoid the performance
524531implications of additional allocation, or we want a configurable/dynamic set of properties for the
@@ -617,10 +624,10 @@ As before, we got the `Color` class represented as a JSON object with three keys
617624### Sequential decoding protocol (experimental)
618625
619626The implementation of the `deserialize` function from the previous section works with any format. However,
620- some formats either always store all the complex data in order, or only do so sometimes (JSON always stores
621- collections in order). With these formats the complex protocol of calling `decodeElementIndex` in the loop is
622- not needed , and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
623- The plugin-generated serializers are actually conceptually similar to the below code.
627+ some formats either always store all the complex data in order or only do so sometimes (JSON always stores
628+ collections in order). With these formats the complex protocol of calling `decodeElementIndex` in a loop is
629+ unnecessary , and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
630+ The plugin-generated serializers are actually conceptually similar to the code below .
624631
625632<!--- INCLUDE
626633object ColorAsObjectSerializer : KSerializer<Color> {
@@ -715,9 +722,15 @@ We cannot bind the `DateAsLongSerializer` serializer to the `Date` class with th
715722because we don' t control the `Date ` source code. There are several ways to work around that.
716723
717724### Passing a serializer manually
718-
719- All `encodeToXxx` and `decodeFromXxx` functions have an overload with the first serializer parameter.
720- When a non- serializable class , like `Date `, is the top- level class being serialized, we can use those.
725+
726+ The `encodeToXxx` and `decodeFromXxx` functions offer overloaded versions
727+ that accept either a [SerializationStrategy ] or [DeserializationStrategy ] as their first parameter, respectively.
728+ This feature allows you
729+ to provide a custom serializer for types that aren' t annotated with [`@Serializable`][Serializable] by default.
730+
731+ This approach is particularly useful
732+ when working with non-serializable classes like `Date` as the top-level object being serialized.
733+ Here' s an example:
721734
722735```kotlin
723736fun main () {
@@ -734,7 +747,7 @@ fun main() {
734747
735748< ! -- - TEST -- >
736749
737- ### Specifying serializer on a property
750+ ### Specifying a serializer on a property
738751
739752When a property of a non- serializable class , like `Date `, is serialized as part of a serializable class we must supply
740753its serializer or the code will not compile. This is accomplished using the [`@Serializable`][Serializable ] annotation on the property.
@@ -774,7 +787,7 @@ The `stableReleaseDate` property is serialized with the serialization strategy t
774787
775788< ! -- - TEST -- >
776789
777- ### Specifying serializer for a particular type
790+ ### Specifying a serializer for a particular type
778791
779792[`@Serializable`][Serializable ] annotation can also be applied directly to the types.
780793This is handy when a class that requires a custom serializer, such as `Date `, happens to be a generic type argument.
@@ -854,7 +867,7 @@ fun main() {
854867
855868< ! -- - TEST -- >
856869
857- ### Specifying serializer globally using typealias
870+ ### Specifying a serializer globally using a typealias
858871
859872kotlinx.serialization tends to be the always- explicit framework when it comes to serialization strategies: normally,
860873they should be explicitly mentioned in `@Serializable` annotation. Therefore , we do not provide any kind of global serializer
@@ -1103,7 +1116,7 @@ class ProgrammingLanguage(
11031116To provide a context, we define a [SerializersModule] instance that describes which serializers shall be used
11041117at run-time to serialize which contextually-serializable classes. This is done using the
11051118[SerializersModule {}][SerializersModule()] builder function, which provides the [SerializersModuleBuilder] DSL to
1106- register serializers. In the below example we use the [contextual][_contextual] function with the serializer. The corresponding
1119+ register serializers. In the example below we use the [contextual][_contextual] function with the serializer. The corresponding
11071120class this serializer is defined for is fetched automatically via the `reified` type parameter.
11081121
11091122```kotlin
@@ -1201,7 +1214,7 @@ This gets all the `Project` properties serialized:
12011214
12021215< ! -- - TEST -- >
12031216
1204- ### External serialization uses properties
1217+ ### External serialization uses properties
12051218
12061219As we saw earlier, the regular `@Serializable` annotation creates a serializer so that
12071220[Backing fields are serialized](basic- serialization.md#backing- fields- are- serialized). _External_ serialization using
0 commit comments