SavedStateEncoderKt

Added in 1.3.0

public final class SavedStateEncoderKt


Summary

Public methods

static final @NonNull SavedState
<T extends Object> encodeToSavedState(
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Serializes the value of type T into an equivalent SavedState using KSerializer retrieved from the reified type parameter.

static final @NonNull SavedState
<T extends Object> encodeToSavedState(
    @NonNull <Error class: unknown class><@NonNull T> serializer,
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Serializes and encodes the given value to SavedState using the given serializer.

Public methods

encodeToSavedState

public static final @NonNull SavedState <T extends Object> encodeToSavedState(
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Serializes the value of type T into an equivalent SavedState using KSerializer retrieved from the reified type parameter.

Format not stable: The internal structure of the returned SavedState is subject to change in future releases for optimization. While it is guaranteed to be compatible with decodeFromSavedState, direct manipulation of its encoded format using keys is not recommended.

import androidx.savedstate.serialization.encodeToSavedState

@Serializable data class User(val id: Int, val name: String)
val user = User(123, "foo")
val savedState = encodeToSavedState(user)
Parameters
@NonNull T value

The serializable object to encode.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use. Defaults to SavedStateConfiguration.DEFAULT.

Returns
@NonNull SavedState

The encoded SavedState.

Throws
SerializationException

in case of any encoding-specific error.

encodeToSavedState

public static final @NonNull SavedState <T extends Object> encodeToSavedState(
    @NonNull <Error class: unknown class><@NonNull T> serializer,
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Serializes and encodes the given value to SavedState using the given serializer.

Format not stable: The internal structure of the returned SavedState is subject to change in future releases for optimization. While it is guaranteed to be compatible with decodeFromSavedState, direct manipulation of its encoded format using keys is not recommended.

import androidx.savedstate.serialization.SavedStateConfiguration
import androidx.savedstate.serialization.encodeToSavedState

val config = SavedStateConfiguration {
    serializersModule = SerializersModule {
        polymorphic(Any::class) { subclass(String::class) }
    }
}
val value = "foo"
val encoded =
    encodeToSavedState(
        serializer = PolymorphicSerializer(Any::class),
        value = value,
        configuration = config,
    )
Parameters
@NonNull <Error class: unknown class><@NonNull T> serializer

The serializer to use.

@NonNull T value

The serializable object to encode.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use. Defaults to SavedStateConfiguration.DEFAULT.

Returns
@NonNull SavedState

The encoded SavedState.

Throws
SerializationException

in case of any encoding-specific error.