public class NavKeySerializer<T extends NavKey> implements KSerializer


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 extends NavKey>

The base NavKey type this serializer can handle.

Summary

Public constructors

<T extends NavKey> NavKeySerializer()

Public methods

@NonNull T

Deserializes a concrete NavKey implementation.

@NonNull SerialDescriptor
void
serialize(@NonNull Encoder encoder, @NonNull T value)

Serializes a concrete NavKey implementation.

Public constructors

public <T extends NavKey> NavKeySerializer()
Parameters
<T extends NavKey>

The base NavKey type this serializer can handle.

Public methods

deserialize

Added in 1.0.0-alpha10
public @NonNulldeserialize(@NonNull Decoder decoder)

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).

getDescriptor

Added in 1.0.0-alpha10
public @NonNull SerialDescriptor getDescriptor()

serialize

Added in 1.0.0-alpha10
public void serialize(@NonNull Encoder encoder, @NonNull T value)

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).