serializerOrNull

Creates a serializer for the given type if possible. 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, serializerOrNull<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.

Return

KSerializer for the given type or null if serializer cannot be created (given type or its type argument is not serializable).

Throws

if any of type's arguments contains star projection


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, serializerOrNull<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.

Return

KSerializer for the given type or null if serializer cannot be created (given type or its type argument is not serializable and is not registered in this module).

Throws

if any of type's arguments contains star projection


Retrieves a KSerializer for the given KClass or returns null if none is found. 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 it 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.

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

Constraints

This paragraph explains known (but not all!) constraints of the serializerOrNull() 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

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.

Return

KSerializer for given type or null if serializer cannot be created (given type or its type argument is not serializable).

Throws

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.

Return

KSerializer for given type or null if serializer cannot be created (given type or its type argument is not serializable).

Throws

if an unsupported subclass of Type is provided.