You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/commonMain/src/kotlinx/serialization/Serializers.kt
+52-24Lines changed: 52 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ import kotlin.reflect.*
23
23
* This overload works with full type information, including type arguments and nullability,
24
24
* and is a recommended way to retrieve a serializer.
25
25
* For example, `serializer<List<String?>>()` returns [KSerializer] that is able
26
-
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
26
+
* to serialize and deserialize a list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
27
27
*
28
28
* Variance of [T]'s type arguments is not used by the serialization and is not taken into account.
29
29
* Star projections in [T]'s type arguments are prohibited.
@@ -42,7 +42,7 @@ public inline fun <reified T> serializer(): KSerializer<T> {
42
42
* This overload works with full type information, including type arguments and nullability,
43
43
* and is a recommended way to retrieve a serializer.
44
44
* For example, `serializer<List<String?>>()` returns [KSerializer] that is able
45
-
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
45
+
* to serialize and deserialize a list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
46
46
*
47
47
* Variance of [T]'s type arguments is not used by the serialization and is not taken into account.
48
48
* Star projections in [T]'s type arguments are prohibited.
@@ -60,12 +60,17 @@ public inline fun <reified T> SerializersModule.serializer(): KSerializer<T> {
60
60
*
61
61
* This overload works with full type information, including type arguments and nullability,
62
62
* and is a recommended way to retrieve a serializer.
63
-
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
64
-
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
63
+
* For example, `serializer(typeOf<List<String?>>())` returns [KSerializer] that is able
64
+
* to serialize and deserialize a list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
65
65
*
66
66
* Variance of [type]'s type arguments is not used by the serialization and is not taken into account.
67
67
* Star projections in [type]'s arguments are prohibited.
68
68
*
69
+
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
70
+
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
71
+
* Consider using the `serializer()` overload accepting a type argument (for example, `serializer<List<String>>()`),
72
+
* which returns the serializer with the correct type.
73
+
*
69
74
* @throws SerializationException if serializer cannot be created (provided [type] or its type argument is not serializable).
70
75
* @throws IllegalArgumentException if any of [type]'s arguments contains star projection
71
76
*/
@@ -80,11 +85,16 @@ public fun serializer(type: KType): KSerializer<Any?> = EmptySerializersModule()
80
85
* The nullability of returned serializer is specified using the [isNullable].
81
86
*
82
87
* Note that it is impossible to create an array serializer with this method,
83
-
* as array serializer needs additional information: type token for an element type.
88
+
* as an array serializer needs additional information: type token for an element type.
84
89
* To create array serializer, use overload with [KType] or [ArraySerializer] directly.
85
90
*
86
91
* Caching on JVM platform is disabled for this function, so it may work slower than an overload with [KType].
87
92
*
93
+
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
94
+
* a value that's not a valid instance of the [KClass], even though the type allows passing such a value.
95
+
* Consider using the `serializer()` overload accepting a type argument (for example, `serializer<List<String>>()`),
96
+
* which returns the serializer with the correct type.
97
+
*
88
98
* @throws SerializationException if serializer cannot be created (provided [kClass] or its type argument is not serializable)
89
99
* @throws SerializationException if [kClass] is a `kotlin.Array`
90
100
* @throws SerializationException if size of [typeArgumentsSerializers] does not match the expected generic parameters count
@@ -102,12 +112,15 @@ public fun serializer(
102
112
*
103
113
* This overload works with full type information, including type arguments and nullability,
104
114
* and is a recommended way to retrieve a serializer.
105
-
* For example, `serializerOrNull<typeOf<List<String?>>>()` returns [KSerializer] that is able
106
-
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
115
+
* For example, `serializerOrNull(typeOf<List<String?>>())` returns [KSerializer] that is able
116
+
* to serialize and deserialize a list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
107
117
*
108
118
* Variance of [type]'s arguments is not used by the serialization and is not taken into account.
109
119
* Star projections in [type]'s arguments are prohibited.
110
120
*
121
+
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
122
+
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
123
+
*
111
124
* @return [KSerializer] for the given [type] or `null` if serializer cannot be created (given [type] or its type argument is not serializable).
112
125
* @throws IllegalArgumentException if any of [type]'s arguments contains star projection
113
126
*/
@@ -120,12 +133,18 @@ public fun serializerOrNull(type: KType): KSerializer<Any?>? = EmptySerializersM
120
133
*
121
134
* This overload works with full type information, including type arguments and nullability,
122
135
* and is a recommended way to retrieve a serializer.
123
-
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
124
-
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
136
+
* For example, `serializer(typeOf<List<String?>>())` returns [KSerializer] that is able
137
+
* to serialize and deserialize a list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
125
138
*
126
139
* Variance of [type]'s arguments is not used by the serialization and is not taken into account.
127
140
* Star projections in [type]'s arguments are prohibited.
128
141
*
142
+
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
143
+
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
144
+
* Consider using the `serializer()` overload accepting a type argument
145
+
* (for example, `module.serializer<List<String>>()`),
146
+
* which returns the serializer with the correct type.
147
+
*
129
148
* @throws SerializationException if serializer cannot be created (provided [type] or its type argument is not serializable and is not registered in [this] module).
130
149
* @throws IllegalArgumentException if any of [type]'s arguments contains star projection
131
150
*/
@@ -143,11 +162,17 @@ public fun SerializersModule.serializer(type: KType): KSerializer<Any?> =
143
162
* The nullability of returned serializer is specified using the [isNullable].
144
163
*
145
164
* Note that it is impossible to create an array serializer with this method,
146
-
* as array serializer needs additional information: type token for an element type.
165
+
* as an array serializer needs additional information: type token for an element type.
147
166
* To create array serializer, use overload with [KType] or [ArraySerializer] directly.
148
167
*
149
168
* Caching on JVM platform is disabled for this function, so it may work slower than an overload with [KType].
150
169
*
170
+
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
171
+
* a value that's not a valid instance of the [KClass], even though the type allows passing such a value.
172
+
* Consider using the `serializer()` overload accepting a type argument
173
+
* (for example, `module.serializer<List<String>>()`),
174
+
* which returns the serializer with the correct type.
175
+
*
151
176
* @throws SerializationException if serializer cannot be created (provided [kClass] or its type argument is not serializable and is not registered in [this] module)
152
177
* @throws SerializationException if [kClass] is a `kotlin.Array`
153
178
* @throws SerializationException if size of [typeArgumentsSerializers] does not match the expected generic parameters count
@@ -168,12 +193,15 @@ public fun SerializersModule.serializer(
168
193
*
169
194
* This overload works with full type information, including type arguments and nullability,
170
195
* and is a recommended way to retrieve a serializer.
171
-
* For example, `serializerOrNull<typeOf<List<String?>>>()` returns [KSerializer] that is able
172
-
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
196
+
* For example, `serializerOrNull(typeOf<List<String?>>())` returns [KSerializer] that is able
197
+
* to serialize and deserialize a list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
173
198
*
174
199
* Variance of [type]'s arguments is not used by the serialization and is not taken into account.
175
200
* Star projections in [type]'s arguments are prohibited.
176
201
*
202
+
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
203
+
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
204
+
*
177
205
* @return [KSerializer] for the given [type] or `null` if serializer cannot be created (given [type] or its type argument is not serializable and is not registered in [this] module).
178
206
* @throws IllegalArgumentException if any of [type]'s arguments contains star projection
179
207
*/
@@ -276,24 +304,24 @@ internal fun SerializersModule.serializersForParameters(
276
304
* The given class must be annotated with [Serializable] or be one of the built-in types.
277
305
*
278
306
* This method uses platform-specific reflection available for the given erased `KClass`
279
-
* and is not recommended to use this method for anything, but last-ditch resort, e.g.
280
-
* when all type info is lost, your application has crashed and it is the final attempt to log or send some serializable data.
307
+
* and is not recommended to use this method for anything, but last-ditch resort, e.g.,
308
+
* when all type info is lost, your application has crashed, and it is the final attempt to log or send some serializable data.
281
309
*
282
310
* The recommended way to retrieve the serializer is inline [serializer] function and [`serializer(KType)`][serializer]
283
311
*
284
312
* This API is not guaranteed to work consistently across different platforms or
285
-
* to work in cases that slightly differ from "plain @Serializable class" and have platform and reflectionspecific limitations.
313
+
* to work in cases that slightly differ from "plain @Serializable class" and have platform- and reflection-specific limitations.
286
314
*
287
315
* ### Constraints
288
316
* This paragraph explains known (but not all!) constraints of the `serializer()` implementation.
289
-
* Please note that they are not bugs, but implementation restrictions that we cannot workaround.
317
+
* Please note that they are not bugs but implementation restrictions that we cannot work around.
290
318
*
291
319
* * This method may behave differently on JVM, JS and Native because of runtime reflection differences
292
320
* * Serializers for classes with generic parameters are ignored by this method
293
-
* * External serializers generated with `Serializer(forClass = )` are not lookuped consistently
294
-
* * Serializers for classes with named companion objects are not lookuped consistently
321
+
* * External serializers generated with `Serializer(forClass = )` are not looked up consistently
322
+
* * Serializers for classes with named companion objects are not looked up consistently
295
323
*
296
-
* @throws SerializationException if serializer can't be found.
324
+
* @throws SerializationException if the serializer can't be found.
0 commit comments