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
Function to retrieve KSerializer by KClass and type arguments serializers (#2291)
Implemented obtainment of KSerializer by KClass in SerializersModule
Resolves#2025
The limitations of this API are the inability to implement stable caching, because serialization runtime does not control the equals function of the received parameters serializers, which can cause a memory leak.
Also, a technical limitation is the inability to create an array serializer.
Co-authored-by: Leonid Startsev <sandwwraith@gmail.com>
Copy file name to clipboardExpand all lines: core/api/kotlinx-serialization-core.api
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -124,8 +124,10 @@ public final class kotlinx/serialization/SerializersKt {
124
124
public static final fun noCompiledSerializer (Lkotlinx/serialization/modules/SerializersModule;Lkotlin/reflect/KClass;[Lkotlinx/serialization/KSerializer;)Lkotlinx/serialization/KSerializer;
125
125
public static final fun serializer (Ljava/lang/reflect/Type;)Lkotlinx/serialization/KSerializer;
126
126
public static final fun serializer (Lkotlin/reflect/KClass;)Lkotlinx/serialization/KSerializer;
127
+
public static final fun serializer (Lkotlin/reflect/KClass;Ljava/util/List;Z)Lkotlinx/serialization/KSerializer;
127
128
public static final fun serializer (Lkotlin/reflect/KType;)Lkotlinx/serialization/KSerializer;
128
129
public static final fun serializer (Lkotlinx/serialization/modules/SerializersModule;Ljava/lang/reflect/Type;)Lkotlinx/serialization/KSerializer;
130
+
public static final fun serializer (Lkotlinx/serialization/modules/SerializersModule;Lkotlin/reflect/KClass;Ljava/util/List;Z)Lkotlinx/serialization/KSerializer;
129
131
public static final fun serializer (Lkotlinx/serialization/modules/SerializersModule;Lkotlin/reflect/KType;)Lkotlinx/serialization/KSerializer;
130
132
public static final fun serializerOrNull (Ljava/lang/reflect/Type;)Lkotlinx/serialization/KSerializer;
131
133
public static final fun serializerOrNull (Lkotlin/reflect/KClass;)Lkotlinx/serialization/KSerializer;
* Retrieves serializer for the given [kClass] and,
139
+
* if [kClass] is not serializable, fallbacks to [contextual][SerializersModule.getContextual] lookup.
140
+
* This method uses platform-specific reflection available.
141
+
*
142
+
* If [kClass] is a parametrized type then it is necessary to pass serializers for generic parameters in the [typeArgumentsSerializers].
143
+
* The nullability of returned serializer is specified using the [isNullable].
144
+
*
145
+
* 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.
147
+
* To create array serializer, use overload with [KType] or [ArraySerializer] directly.
148
+
*
149
+
* Caching on JVM platform is disabled for this function, so it may work slower than an overload with [KType].
150
+
*
151
+
* @throws SerializationException if serializer cannot be created (provided [kClass] or its type argument is not serializable and is not registered in [this] module)
152
+
* @throws SerializationException if [kClass] is a `kotlin.Array`
153
+
* @throws SerializationException if size of [typeArgumentsSerializers] does not match the expected generic parameters count
throwSerializationException("It is not possible to retrieve an array serializer using KClass alone, use KType instead or ArraySerializer factory")
233
+
} ?: getContextual(
234
+
rootClass,
235
+
typeArgumentsSerializers
236
+
)
237
+
} catch (e:IndexOutOfBoundsException) {
238
+
throwSerializationException("Unable to retrieve a serializer, the number of passed type serializers differs from the actual number of generic parameters", e)
0 commit comments