1 /*
2 * Copyright 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 @file:OptIn(InternalSerializationApi::class, ExperimentalTypeInference::class)
18
19 package androidx.savedstate.compose.serialization.serializers
20
21 import androidx.compose.runtime.MutableState
22 import androidx.compose.runtime.mutableStateOf
23 import kotlin.experimental.ExperimentalTypeInference
24 import kotlinx.serialization.ExperimentalSerializationApi
25 import kotlinx.serialization.InternalSerializationApi
26 import kotlinx.serialization.KSerializer
27 import kotlinx.serialization.descriptors.PrimitiveKind
28 import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
29 import kotlinx.serialization.descriptors.SerialDescriptor
30 import kotlinx.serialization.encoding.Decoder
31 import kotlinx.serialization.encoding.Encoder
32 import kotlinx.serialization.serializer
33
34 /**
35 * Creates a [KSerializer] for a [MutableState] containing a [Serializable] value of type [T].
36 *
37 * This inline function infers the state type [T] automatically and retrieves the appropriate
38 * [KSerializer] for serialization and deserialization of [MutableState].
39 *
40 * @param T The type of the value stored in the [MutableState].
41 * @return A [MutableStateSerializer] for handling [MutableState] containing a [Serializable] type
42 * [T].
43 */
MutableStateSerializernull44 public inline fun <reified T> MutableStateSerializer(): MutableStateSerializer<T> {
45 return MutableStateSerializer(serializer())
46 }
47
48 /**
49 * A [KSerializer] for [MutableState].
50 *
51 * This class wraps a [KSerializer] for the inner value type [T], enabling serialization and
52 * deserialization of [MutableState] instances. The inner value serialization is delegated to the
53 * provided [valueSerializer].
54 *
55 * @param T The type of the value stored in the [MutableState].
56 * @param valueSerializer The [KSerializer] used to serialize and deserialize the inner value.
57 */
58 public class MutableStateSerializer<T>(
59 private val valueSerializer: KSerializer<T>,
60 ) : KSerializer<MutableState<T>> {
61
62 @OptIn(ExperimentalSerializationApi::class)
<lambda>null63 override val descriptor: SerialDescriptor = run {
64 val serialName = "androidx.compose.runtime.MutableState"
65 val kind = valueSerializer.descriptor.kind
66 if (kind is PrimitiveKind) {
67 PrimitiveSerialDescriptor(serialName, kind)
68 } else {
69 SerialDescriptor(serialName, valueSerializer.descriptor)
70 }
71 }
72
serializenull73 override fun serialize(encoder: Encoder, value: MutableState<T>) {
74 encoder.encodeSerializableValue(valueSerializer, value.value)
75 }
76
deserializenull77 override fun deserialize(decoder: Decoder): MutableState<T> {
78 return mutableStateOf(decoder.decodeSerializableValue(valueSerializer))
79 }
80 }
81