Package kotlinx.serialization.modules

Classes that provides runtime mechanisms for resolving serializers, typically used during polymorphic serialization.

Types

PolymorphicModuleBuilder
Link copied to clipboard
common

A builder which registers all its content for polymorphic serialization in the scope of the base class. If baseSerializer is present, registers it as a serializer for baseClass (which will be used if base class is serializable). Subclasses and its serializers can be added with subclass builder function.

To obtain an instance of this builder, use SerializersModuleBuilder.polymorphic DSL function.

class PolymorphicModuleBuilder<in Base : Any>
PolymorphicProvider
Link copied to clipboard
common
internal typealias PolymorphicProvider<Base> = (className: String?) -> DeserializationStrategy<out Base>?
SerializersModule
Link copied to clipboard
common

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.

sealed class SerializersModule
SerializersModuleBuilder
Link copied to clipboard
common

A builder class for SerializersModule DSL. To create an instance of builder, use SerializersModule factory function.

class SerializersModuleBuilder : SerializersModuleCollector
SerializersModuleCollector
Link copied to clipboard
common

SerializersModuleCollector can introspect and accumulate content of any SerializersModule via SerializersModule.dumpTo, using a visitor-like pattern: contextual and polymorphic functions are invoked for each registered serializer.

Not stable for inheritance

SerializersModuleCollector interface is not stable for inheritance in 3rd party libraries, as new methods might be added to this interface or contracts of the existing methods can be changed.

interface SerializersModuleCollector

Functions

contextual
Link copied to clipboard
common

Adds serializer associated with given type T for contextual serialization. Throws SerializationException if a module already has serializer associated with the given type. To overwrite an already registered serializer, SerializersModule.overwriteWith can be used.

inline fun <T : Any> SerializersModuleBuilder.contextual(serializer: KSerializer<T>)
overwriteWith
Link copied to clipboard
common

Returns a combination of two serial modules

If serializer for some class presents in both modules, result module will contain serializer from other module.

infix fun SerializersModule.overwriteWith(other: SerializersModule): SerializersModule
plus
Link copied to clipboard
common

Returns a combination of two serial modules

If serializer for some class presents in both modules, a SerializerAlreadyRegisteredException is thrown. To overwrite serializers, use SerializersModule.overwriteWith function.

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

Creates a builder to register subclasses of a given baseClass for polymorphic serialization. If baseSerializer is not null, registers it as a serializer for baseClass, which is useful if the base class is serializable itself. To register subclasses, PolymorphicModuleBuilder.subclass builder function can be used.

If a serializer already registered for the given KClass in the given scope, an IllegalArgumentException is thrown. To override registered serializers, combine built module with another using SerializersModule.overwriteWith.

inline fun <Base : Any> SerializersModuleBuilder.polymorphic(baseClass: KClass<Base>, baseSerializer: KSerializer<Base>? = null, builderAction: PolymorphicModuleBuilder<Base>.() -> Unit = {})
SerializersModule
Link copied to clipboard
common

A builder function for creating a SerializersModule. Serializers can be added via SerializersModuleBuilder.contextual or SerializersModuleBuilder.polymorphic. Since SerializersModuleBuilder also implements SerialModuleCollector, it is possible to copy whole another module to this builder with SerializersModule.dumpTo

inline fun SerializersModule(builderAction: SerializersModuleBuilder.() -> Unit): SerializersModule
serializersModuleOf
Link copied to clipboard
common

Returns a SerializersModule which has one class with one serializer for ContextualSerializer.

inline fun <T : Any> serializersModuleOf(serializer: KSerializer<T>): SerializersModule
fun <T : Any> serializersModuleOf(kClass: KClass<T>, serializer: KSerializer<T>): SerializersModule
subclass
Link copied to clipboard
common

Registers a serializer for class T in the resulting module under the base class.

inline fun <Base : Any, T : Base> PolymorphicModuleBuilder<Base>.subclass(clazz: KClass<T>)

Registers a subclass in the resulting module under the base class.

inline fun <Base : Any, T : Base> PolymorphicModuleBuilder<Base>.subclass(serializer: KSerializer<T>)

Properties

EmptySerializersModule
Link copied to clipboard
common

A SerializersModule which is empty and always returns null.

val EmptySerializersModule: SerializersModule