A KSerializer for handling polymorphic NavKey hierarchies on Android.

This serializer enables kotlinx.serialization to save and restore different disparate implementations of NavKey without requiring developers to manually register them in a SerializersModule.

How It Works

Instead of serializing the NavKey directly, this class serializes two pieces of information:

  1. The fully qualified class name of the concrete NavKey (e.g., "com.example.Home").

  2. The actual NavKey object, using its specific runtime serializer.

During deserialization, it reads the class name first, uses reflection (Class.forName) to find the corresponding KSerializer for that class, and then uses that serializer to deserialize the object data.

Platform Limitation

This serializer is for Android only. It relies on JVM reflection (Class.forName) which is not available on other platforms in a multiplatform context.

Parameters
<T : NavKey>

The base NavKey type this serializer can handle.

Summary

Public constructors

android

Public functions

open T
deserialize(decoder: Decoder)

Deserializes a concrete NavKey implementation.

android
open Unit
serialize(encoder: Encoder, value: T)

Serializes a concrete NavKey implementation.

android

Public properties

open SerialDescriptor
android

Public constructors

<T : NavKey> NavKeySerializer()
Parameters
<T : NavKey>

The base NavKey type this serializer can handle.

Public functions

deserialize

open fun deserialize(decoder: Decoder): T

Deserializes a concrete NavKey implementation.

It first reads the class name (type), then uses that name to find the corresponding KSerializer for the class using reflection. Finally, it uses that specific serializer to read the actual object data (value).

serialize

open fun serialize(encoder: Encoder, value: T): Unit

Serializes a concrete NavKey implementation.

It writes the object's fully qualified class name (type) first, then uses the object's runtime serializer to write the object's data (value).

Public properties

descriptor

open val descriptorSerialDescriptor