serializer

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

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


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

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


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

Creates a serializer for the given type. type argument can be obtained with experimental typeOf method.

Throws

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


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

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.

Throws

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


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 constructs a serializer for the given reflective Java type. 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.

Throws

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


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.

Throws

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