serializer

inline fun <T> serializer(): KSerializer<T>(source)

Retrieves a serializer for the given type T. This overload is a reified version of serializer(KType).

This overload works with full type information, including type arguments and nullability, and is a recommended way to retrieve a serializer. For example, serializer<List<String?>>() returns KSerializer that is able to serialize and deserialize list of nullable strings — i.e. ListSerializer(String.serializer().nullable).

Variance of T's type arguments is not used by the serialization and is not taken into account. Star projections in T's type arguments are prohibited.

Throws

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

if any of T's type arguments contains star projection


Retrieves default serializer for the given type T and, if T is not serializable, fallbacks to contextual lookup.

This overload works with full type information, including type arguments and nullability, and is a recommended way to retrieve a serializer. For example, serializer<List<String?>>() returns KSerializer that is able to serialize and deserialize list of nullable strings — i.e. ListSerializer(String.serializer().nullable).

Variance of T's type arguments is not used by the serialization and is not taken into account. Star projections in T's type arguments are prohibited.

Throws

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

if any of T's type arguments contains star projection


Creates a serializer for the given type. type argument is usually obtained with typeOf method.

This overload works with full type information, including type arguments and nullability, and is a recommended way to retrieve a serializer. For example, serializer<typeOf<List<String?>>>() returns KSerializer that is able to serialize and deserialize list of nullable strings — i.e. ListSerializer(String.serializer().nullable).

Variance of type's type arguments is not used by the serialization and is not taken into account. Star projections in type's arguments are prohibited.

Throws

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

if any of type's arguments contains star projection


fun serializer(kClass: KClass<*>, typeArgumentsSerializers: List<KSerializer<*>>, isNullable: Boolean): KSerializer<Any?>(source)

Retrieves serializer for the given kClass. This method uses platform-specific reflection available.

If kClass is a parametrized type then it is necessary to pass serializers for generic parameters in the typeArgumentsSerializers. The nullability of returned serializer is specified using the isNullable.

Note that it is impossible to create an array serializer with this method, as array serializer needs additional information: type token for an element type. To create array serializer, use overload with KType or ArraySerializer directly.

Caching on JVM platform is disabled for this function, so it may work slower than an overload with KType.

Throws

if size of typeArgumentsSerializers does not match the expected generic parameters count


Retrieves default serializer for the given type and, if type is not serializable, fallbacks to contextual lookup. type argument is usually obtained with typeOf method.

This overload works with full type information, including type arguments and nullability, and is a recommended way to retrieve a serializer. For example, serializer<typeOf<List<String?>>>() returns KSerializer that is able to serialize and deserialize list of nullable strings — i.e. ListSerializer(String.serializer().nullable).

Variance of type's arguments is not used by the serialization and is not taken into account. Star projections in type's arguments are prohibited.

Throws

if serializer cannot be created (provided type or its type argument is not serializable and is not registered in this module).

if any of type's arguments contains star projection


fun SerializersModule.serializer(kClass: KClass<*>, typeArgumentsSerializers: List<KSerializer<*>>, isNullable: Boolean): KSerializer<Any?>(source)

Retrieves serializer for the given kClass and, if kClass is not serializable, fallbacks to contextual lookup. This method uses platform-specific reflection available.

If kClass is a parametrized type then it is necessary to pass serializers for generic parameters in the typeArgumentsSerializers. The nullability of returned serializer is specified using the isNullable.

Note that it is impossible to create an array serializer with this method, as array serializer needs additional information: type token for an element type. To create array serializer, use overload with KType or ArraySerializer directly.

Caching on JVM platform is disabled for this function, so it may work slower than an overload with KType.

Throws

if size of typeArgumentsSerializers does not match the expected generic parameters count


Retrieves a KSerializer for the given KClass. The given class must be annotated with Serializable or be one of the built-in types.

This method uses platform-specific reflection available for the given erased KClass and is not recommended to use this method for anything, but last-ditch resort, e.g. when all type info is lost, your application has crashed and it is the final attempt to log or send some serializable data.

The recommended way to retrieve the serializer is inline serializer function and serializer(KType)

This API is not guaranteed to work consistently across different platforms or to work in cases that slightly differ from "plain @Serializable class" and have platform and reflection specific limitations.

Constraints

This paragraph explains known (but not all!) constraints of the serializer() implementation. Please note that they are not bugs, but implementation restrictions that we cannot workaround.

  • This method may behave differently on JVM, JS and Native because of runtime reflection differences

  • Serializers for classes with generic parameters are ignored by this method

  • External serializers generated with Serializer(forClass = ) are not lookuped consistently

  • Serializers for classes with named companion objects are not lookuped consistently

Throws

if serializer can't be found.

Reflectively retrieves a serializer for the given type.

This overload is intended to be used as an interoperability layer for JVM-centric libraries, that operate with Java's type tokens and cannot use Kotlin's KType or typeOf. For application-level serialization, it is recommended to use serializer<T>() or serializer(KType) instead as it is aware of Kotlin-specific type information, such as nullability, sealed classes and object singletons.

Note that because Type does not contain any information about nullability, all created serializers work only with non-nullable data.

Not all Type implementations are supported. type must be an instance of Class, GenericArrayType, ParameterizedType or WildcardType.

Throws

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

if an unsupported subclass of Type is provided.


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

This overload is intended to be used as an interoperability layer for JVM-centric libraries, that operate with Java's type tokens and cannot use Kotlin's KType or typeOf. For application-level serialization, it is recommended to use serializer<T>() or serializer(KType) instead as it is aware of Kotlin-specific type information, such as nullability, sealed classes and object singletons.

Note that because Type does not contain any information about nullability, all created serializers work only with non-nullable data.

Not all Type implementations are supported. type must be an instance of Class, GenericArrayType, ParameterizedType or WildcardType.

Throws

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

if an unsupported subclass of Type is provided.