Package kotlinx.serialization.modules

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

Types

Link copied to clipboard
class PolymorphicModuleBuilder<in Base : Any>

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.

Link copied to clipboard
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.

Link copied to clipboard
class SerializersModuleBuilder : SerializersModuleCollector

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

Link copied to clipboard
interface SerializersModuleCollector

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.

Functions

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

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.

Link copied to clipboard
fun EmptySerializersModule(): SerializersModule

A SerializersModule which is empty and returns null from each method.

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 <Base : Any> SerializersModuleBuilder.polymorphic(    baseClass: KClass<Base>,     baseSerializer: KSerializer<Base>? = null,     builderAction: PolymorphicModuleBuilder<Base>.() -> Unit = {})

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.

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

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

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

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

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

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

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

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