MutableStateFlowSerializer


public final class MutableStateFlowSerializer<T extends Object> implements KSerializer


A KSerializer for MutableStateFlow.

This class wraps a KSerializer for the inner value type T, enabling serialization and deserialization of MutableStateFlow instances. The inner value serialization is delegated to the provided valueSerializer.

Note that the SavedState format uses this serializer automatically for a MutableStateFlow at root and there is no need to explicitly specify it. For example:

val msf = MutableStateFlow(123)
// No need to do `encodeToSavedState(MutableStateFlowSerializer(Int.serializer), msf)`
encodeToSavedState(msf)

However, when the MutableStateFlow is a property of a serializable class there is no automatic fallback and a Serializable annotation is still needed if the property is intended to be serialized with this serializer. For example:

@Serializable
data class MyData(
@Serializable(with = MutableStateFlowSerializer::class)
val flow: MutableStateFlow<Int>
)
Parameters
<T extends Object>

The type of the value stored in the MutableStateFlow.

Summary

Public constructors

<T extends Object> MutableStateFlowSerializer(
    @NonNull KSerializer<@NonNull T> valueSerializer
)

Public constructors

MutableStateFlowSerializer

public <T extends Object> MutableStateFlowSerializer(
    @NonNull KSerializer<@NonNull T> valueSerializer
)
Parameters
<T extends Object>

The type of the value stored in the MutableStateFlow.

@NonNull KSerializer<@NonNull T> valueSerializer

The KSerializer used to serialize and deserialize the inner value.

Public methods

deserialize

Added in 1.3.0
public @NonNull MutableStateFlow<@NonNull T> deserialize(@NonNull Decoder decoder)

getDescriptor

Added in 1.3.0
public @NonNull SerialDescriptor getDescriptor()

serialize

Added in 1.3.0
public void serialize(
    @NonNull Encoder encoder,
    @NonNull MutableStateFlow<@NonNull T> value
)