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.

sealed class SerializersModule

See also

Contextual
Link copied to clipboard

Polymorphic
Link copied to clipboard

Functions

dumpTo
Link copied to clipboard
common

Copies contents of this module to the given collector.

abstract fun dumpTo(collector: SerializersModuleCollector)
equals
Link copied to clipboard
common
open operator override fun equals(other: Any?): Boolean
getContextual
Link copied to clipboard
common

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.

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

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>, value: T): SerializationStrategy<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.

abstract fun <T : Any> getPolymorphic(baseClass: KClass<in T>, serializedClassName: String?): DeserializationStrategy<out T>?
hashCode
Link copied to clipboard
common
open override fun hashCode(): Int
toString
Link copied to clipboard
common
open override fun toString(): String

Extensions

getContextualDescriptor
Link copied to clipboard
common

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

fun SerializersModule.getContextualDescriptor(descriptor: SerialDescriptor): SerialDescriptor?
getPolymorphicDescriptors
Link copied to clipboard
common

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

fun SerializersModule.getPolymorphicDescriptors(descriptor: SerialDescriptor): List<SerialDescriptor>
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
serializer
Link copied to clipboard

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

common
inline fun <T> SerializersModule.serializer(): KSerializer<T>

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.

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

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

serializer is intended to be used as an interoperability layer for libraries like GSON and Retrofit, that operate with reflective Java Type and cannot use typeOf.

For application-level serialization, it is recommended to use serializer<T>() instead as it is aware of Kotlin-specific type information, such as nullability, sealed classes and object singletons.

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

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

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

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

serializer is intended to be used as an interoperability layer for libraries like GSON and Retrofit, that operate with reflective Java Type and cannot use typeOf.

For application-level serialization, it is recommended to use serializer<T>() instead as it is aware of Kotlin-specific type information, such as nullability, sealed classes and object singletons.

Returns null if serializer cannot be created (provided type or its type argument is not serializable).

fun SerializersModule.serializerOrNull(type: Type): KSerializer<Any>?