kotlinx-serialization / kotlinx.serialization.modules

Package kotlinx.serialization.modules

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

Types

(common, js, jvm, native)

EmptyModule

A SerialModule which is empty and always returns null.

object EmptyModule : SerialModule
(common, js, jvm, native)

PolymorphicModuleBuilder

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

class PolymorphicModuleBuilder<Base : Any>
(common, js, jvm, native)

SerializersModuleBuilder

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

class SerializersModuleBuilder : SerialModuleCollector
(common, js, jvm, native)

SerialModule

SerialModule is a collection of serializers used by ContextSerializer 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.

sealed class SerialModule
(common, js, jvm, native)

SerialModuleCollector

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

interface SerialModuleCollector

Exceptions

SerializerAlreadyRegisteredException

Thrown on an attempt to register serializer (for contextual or polymorphic serialization) for some class twice.

(common, js, native) class SerializerAlreadyRegisteredException : IllegalArgumentException
(jvm) class SerializerAlreadyRegisteredException : IllegalArgumentException

Functions

(common, js, jvm, native)

contextual

Version of SerialModuleCollector.contextual with reified argument.

fun <T : Any> SerialModuleCollector.contextual(serializer: KSerializer<T>): Unit
(common, js, jvm, native)

getContextual

Returns a dependent serializer associated with a given reified type.

fun <T : Any> SerialModule.getContextual(): KSerializer<T>?

Returns a serializer associated with KClass of the given value.

fun <T : Any> SerialModule.getContextual(value: T): KSerializer<T>?
(common, js, jvm, native)

getContextualDescriptor

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

fun SerialModule.getContextualDescriptor(descriptor: SerialDescriptor): SerialDescriptor?
(common, js, jvm, native)

getContextualOrDefault

Returns a serializer associated with klass, or a default one.

fun <T : Any> SerialModule.getContextualOrDefault(klass: KClass<T>): KSerializer<T>

Returns a serializer associated with KClass of the given value, or a default one.

fun <T : Any> SerialModule.getContextualOrDefault(value: T): KSerializer<T>
(common, js, jvm, native)

getPolymorphicDescriptors

Retrieves a collection of descriptors which serializers are registered for polymorphic serialization in this with base class equal to descriptor's SerialDescriptor.capturedKClass.

fun SerialModule.getPolymorphicDescriptors(descriptor: SerialDescriptor): List<SerialDescriptor>
(common, js, jvm, native)

overwriteWith

Returns a combination of two serial modules

infix fun SerialModule.overwriteWith(other: SerialModule): SerialModule
(common, js, jvm, native)

plus

Returns a combination of two serial modules

operator fun SerialModule.plus(other: SerialModule): SerialModule
(common, js, jvm, native)

serializersModule

Shortcut for serializersModuleOf function with type parameter.

fun <T : Any> serializersModule(serializer: KSerializer<T>): SerialModule
(common, js, jvm, native)

SerializersModule

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

fun SerializersModule(buildAction: SerializersModuleBuilder.() -> Unit): SerialModule
(common, js, jvm, native)

serializersModuleOf

Returns a SerialModule which has one class with one serializer for ContextSerializer.

fun <T : Any> serializersModuleOf(kClass: KClass<T>, serializer: KSerializer<T>): SerialModule

Returns a SerialModule which has multiple classes with its serializers for ContextSerializer.

fun serializersModuleOf(map: Map<KClass<*>, KSerializer<*>>): SerialModule