@@ -19,14 +19,14 @@ In this chapter we'll take a look at serializers in more detail, and we'll see h
19
19
* [ Primitive serializer] ( #primitive-serializer )
20
20
* [ Delegating serializers] ( #delegating-serializers )
21
21
* [ Composite serializer via surrogate] ( #composite-serializer-via-surrogate )
22
- * [ Hand-written composite serializer] ( #hand-written -composite-serializer )
22
+ * [ Handwritten composite serializer] ( #handwritten -composite-serializer )
23
23
* [ Sequential decoding protocol (experimental)] ( #sequential-decoding-protocol-experimental )
24
24
* [ Serializing 3rd party classes] ( #serializing-3rd-party-classes )
25
25
* [ 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 )
28
28
* [ 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 )
30
30
* [ Custom serializers for a generic type] ( #custom-serializers-for-a-generic-type )
31
31
* [ Format-specific serializers] ( #format-specific-serializers )
32
32
* [ Simultaneous use of plugin-generated and custom serializers] ( #simultaneous-use-of-plugin-generated-and-custom-serializers )
@@ -165,9 +165,11 @@ fun main() {
165
165
166
166
> You can get the full code [ here] ( ../guide/example/example-serializer-04.kt ) .
167
167
168
- <!-- - TEST
168
+ ``` text
169
169
PrimitiveDescriptor(kotlin.Int)
170
- -->
170
+ ```
171
+
172
+ <!-- - TEST -->
171
173
172
174
### Constructing collection serializers
173
175
@@ -191,9 +193,11 @@ fun main() {
191
193
192
194
> You can get the full code [ here] ( ../guide/example/example-serializer-05.kt ) .
193
195
194
- <!-- - TEST
196
+ ``` text
195
197
kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
196
- -->
198
+ ```
199
+
200
+ <!-- - TEST -->
197
201
198
202
### Using top-level serializer function
199
203
@@ -217,14 +221,17 @@ fun main() {
217
221
218
222
> You can get the full code [ here] ( ../guide/example/example-serializer-06.kt ) .
219
223
220
- <!-- - TEST
224
+ ``` text
221
225
kotlin.collections.LinkedHashMap(PrimitiveDescriptor(kotlin.String), Color(rgb: kotlin.Int))
222
- -->
226
+ ```
227
+
228
+ <!-- - TEST -->
223
229
224
230
## Custom serializers
225
231
226
232
A 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.
228
235
229
236
### Primitive serializer
230
237
@@ -254,7 +261,7 @@ object ColorAsStringSerializer : KSerializer<Color> {
254
261
}
255
262
```
256
263
257
- Serializer has three required pieces.
264
+ A serializer has three required pieces.
258
265
259
266
* The [ serialize] [ SerializationStrategy.serialize ] function implements [ SerializationStrategy] .
260
267
It receives an instance of [ Encoder] and a value to serialize.
@@ -419,10 +426,10 @@ class ColorIntArraySerializer : KSerializer<Color> {
419
426
Note that we can't use default ` Color.serializer().descriptor ` here because formats that rely
420
427
on the schema may think that we would call ` encodeInt ` instead of ` encodeSerializableValue ` .
421
428
Neither 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.
424
431
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 ) .
426
433
427
434
Now we can use the serializer:
428
435
@@ -518,7 +525,7 @@ fun main() {
518
525
519
526
<!-- - TEST -->
520
527
521
- ### Hand-written composite serializer
528
+ ### Handwritten composite serializer
522
529
523
530
There are some cases where a surrogate solution does not fit. Perhaps we want to avoid the performance
524
531
implications 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
617
624
### Sequential decoding protocol (experimental)
618
625
619
626
The 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 .
624
631
625
632
<!--- INCLUDE
626
633
object ColorAsObjectSerializer : KSerializer<Color> {
@@ -715,9 +722,15 @@ We cannot bind the `DateAsLongSerializer` serializer to the `Date` class with th
715
722
because we don' t control the `Date ` source code. There are several ways to work around that.
716
723
717
724
### 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:
721
734
722
735
```kotlin
723
736
fun main () {
@@ -734,7 +747,7 @@ fun main() {
734
747
735
748
< ! -- - TEST -- >
736
749
737
- ### Specifying serializer on a property
750
+ ### Specifying a serializer on a property
738
751
739
752
When a property of a non- serializable class , like `Date `, is serialized as part of a serializable class we must supply
740
753
its 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
774
787
775
788
< ! -- - TEST -- >
776
789
777
- ### Specifying serializer for a particular type
790
+ ### Specifying a serializer for a particular type
778
791
779
792
[`@Serializable`][Serializable ] annotation can also be applied directly to the types.
780
793
This 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() {
854
867
855
868
< ! -- - TEST -- >
856
869
857
- ### Specifying serializer globally using typealias
870
+ ### Specifying a serializer globally using a typealias
858
871
859
872
kotlinx.serialization tends to be the always- explicit framework when it comes to serialization strategies: normally,
860
873
they should be explicitly mentioned in `@Serializable` annotation. Therefore , we do not provide any kind of global serializer
@@ -1103,7 +1116,7 @@ class ProgrammingLanguage(
1103
1116
To provide a context, we define a [SerializersModule] instance that describes which serializers shall be used
1104
1117
at run-time to serialize which contextually-serializable classes. This is done using the
1105
1118
[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
1107
1120
class this serializer is defined for is fetched automatically via the `reified` type parameter.
1108
1121
1109
1122
```kotlin
@@ -1201,7 +1214,7 @@ This gets all the `Project` properties serialized:
1201
1214
1202
1215
< ! -- - TEST -- >
1203
1216
1204
- ### External serialization uses properties
1217
+ ### External serialization uses properties
1205
1218
1206
1219
As we saw earlier, the regular `@Serializable` annotation creates a serializer so that
1207
1220
[Backing fields are serialized](basic- serialization.md#backing- fields- are- serialized). _External_ serialization using
0 commit comments