serializer

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

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


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

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


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

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 SerializersModule.serializer(type: KType): KSerializer<Any?>

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 <T : Any> KClass<T>.serializer(): KSerializer<T>

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

fun serializer(type: Type): KSerializer<Any>

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.


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

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.