SerializersModule

SerializersModule is a collection of serializers used by ContextualSerializer and PolymorphicSerializer to override or provide serializers at the runtime, whereas at the compile-time they provided by the serialization plugin. It can be considered as a map where serializers can be found using their statically known KClasses.

To enable runtime serializers resolution, one of the special annotations must be used on target types (Polymorphic or Contextual), and a serial module with serializers should be used during construction of SerialFormat.

Serializers module can be built with SerializersModule {} builder function. Empty module can be obtained with EmptySerializersModule() factory function.

See also

Functions

Link copied to clipboard

Copies contents of this module to the given collector.

Link copied to clipboard
abstract fun <T : Any> getContextual(kClass: KClass<T>, typeArgumentsSerializers: List<KSerializer<*>> = emptyList()): KSerializer<T>?

Returns a contextual serializer associated with a given kClass. If given class has generic parameters and module has provider for kClass, typeArgumentsSerializers are used to create serializer. This method is used in context-sensitive operations on a property marked with Contextual by a ContextualSerializer.

Link copied to clipboard

Looks up a descriptor of serializer registered for contextual serialization in this, using SerialDescriptor.capturedKClass as a key.

Link copied to clipboard
abstract fun <T : Any> getPolymorphic(baseClass: KClass<in T>, value: T): SerializationStrategy<T>?

Returns a polymorphic serializer registered for a class of the given value in the scope of baseClass.

abstract fun <T : Any> getPolymorphic(baseClass: KClass<in T>, serializedClassName: String?): DeserializationStrategy<T>?

Returns a polymorphic deserializer registered for a serializedClassName in the scope of baseClass or default value constructed from serializedClassName if a default serializer provider was registered.

Link copied to clipboard

Retrieves a collection of descriptors which serializers are registered for polymorphic serialization in this with base class equal to descriptor's SerialDescriptor.capturedKClass. This method does not retrieve serializers registered with PolymorphicModuleBuilder.defaultDeserializer or PolymorphicModuleBuilder.defaultSerializer.

Link copied to clipboard

Returns a combination of two serial modules

Link copied to clipboard

Returns a combination of two serial modules

Link copied to clipboard

Retrieves default serializer for the given type T and, if T is not serializable, fallbacks to contextual lookup.

Retrieves default serializer for the given type and, if type is not serializable, fallbacks to contextual lookup. type argument is usually obtained with typeOf method.

fun SerializersModule.serializer(kClass: KClass<*>, typeArgumentsSerializers: List<KSerializer<*>>, isNullable: Boolean): KSerializer<Any?>

Retrieves serializer for the given kClass and, if kClass is not serializable, fallbacks to contextual lookup. This method uses platform-specific reflection available.

Retrieves a serializer for the given type using reflective construction and contextual lookup as a fallback for non-serializable types.

Link copied to clipboard

Retrieves default serializer for the given type and, if type is not serializable, fallbacks to contextual lookup. type argument is usually obtained with typeOf method.

Retrieves a serializer for the given type using reflective construction and contextual lookup as a fallback for non-serializable types.