MutableStateFlowSerializer


public final class MutableStateFlowSerializer<T extends Object>


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

Public constructors

MutableStateFlowSerializer

public <T extends Object> MutableStateFlowSerializer(
    @NonNull <Error class: unknown class><@NonNull T> valueSerializer
)
Parameters
<T extends Object>

The type of the value stored in the MutableStateFlow.

@NonNull <Error class: unknown class><@NonNull T> valueSerializer

The KSerializer used to serialize and deserialize the inner value.

Public methods

getDescriptor

Added in 1.3.0
public @NonNull <Error class: unknown class> getDescriptor()

serialize

public void serialize(
    @NonNull <Error class: unknown class> encoder,
    @NonNull MutableStateFlow<@NonNull T> value
)