serializerOrNull

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

Creates a serializer for the given type. 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).


fun SerializersModule.serializerOrNull(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. Returns null 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>.serializerOrNull(): KSerializer<T>?

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

fun serializerOrNull(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.

Returns null 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.

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