SerializersModule

sealed class 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
abstract fun dumpTo(collector: SerializersModuleCollector)

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
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<out 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.

Extensions

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

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
infix fun SerializersModule.overwriteWith(other: SerializersModule): SerializersModule

Returns a combination of two serial modules

Link copied to clipboard
operator fun SerializersModule.plus(other: SerializersModule): SerializersModule

Returns a combination of two serial modules

Link copied to clipboard
inline fun <T> SerializersModule.serializer(): KSerializer<T>

Retrieves serializer for the given type T from the current SerializersModule and, if not found, fallbacks to plain serializer method.

fun SerializersModule.serializer(type: KType): KSerializer<Any?>

Attempts to create a serializer for the given type and fallbacks to contextual lookup for non-serializable types. type argument can be obtained with experimental typeOf method.

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

Link copied to clipboard
fun SerializersModule.serializerOrNull(type: KType): KSerializer<Any?>?

Attempts to create a serializer for the given type and fallbacks to contextual lookup for non-serializable types. type argument can be obtained with experimental typeOf method. Returns null if serializer cannot be created (provided type or its type argument is not serializable and is not registered in this module).

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