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 package androidx.savedstate.compose.serialization.serializers
18
19 import androidx.compose.runtime.snapshots.SnapshotStateMap
20 import kotlinx.serialization.ExperimentalSerializationApi
21 import kotlinx.serialization.KSerializer
22 import kotlinx.serialization.builtins.MapSerializer
23 import kotlinx.serialization.descriptors.SerialDescriptor
24 import kotlinx.serialization.encoding.Decoder
25 import kotlinx.serialization.encoding.Encoder
26 import kotlinx.serialization.serializer
27
28 /**
29 * Creates a [KSerializer] for a [SnapshotStateMap] containing serializable keys of type [K] and
30 * serializable values of type [V].
31 *
32 * This inline function automatically infers the key type [K] and value type [V], and retrieves the
33 * corresponding [KSerializer] for serializing and deserializing [SnapshotStateMap] instances.
34 *
35 * @param K The type of keys stored in the [SnapshotStateMap].
36 * @param V The type of values stored in the [SnapshotStateMap].
37 * @return A [SnapshotStateMapSerializer] for handling serialization and deserialization of a
38 * [SnapshotStateMap] containing keys of type [K] and values of type [V].
39 */
SnapshotStateMapSerializernull40 public inline fun <reified K, reified V> SnapshotStateMapSerializer():
41 SnapshotStateMapSerializer<K, V> {
42 return SnapshotStateMapSerializer(serializer(), serializer())
43 }
44
45 /**
46 * A [KSerializer] for [SnapshotStateMap].
47 *
48 * This serializer wraps [KSerializer] instances for the key type [K] and value type [V], enabling
49 * serialization and deserialization of [SnapshotStateMap] instances. The serialization of
50 * individual keys and values is delegated to the provided [keySerializer] and [valueSerializer].
51 *
52 * @param K The type of keys stored in the [SnapshotStateMap].
53 * @param V The type of values stored in the [SnapshotStateMap].
54 * @param keySerializer The [KSerializer] used to serialize and deserialize individual keys.
55 * @param valueSerializer The [KSerializer] used to serialize and deserialize individual values.
56 */
57 public class SnapshotStateMapSerializer<K, V>(
58 keySerializer: KSerializer<K>,
59 valueSerializer: KSerializer<V>,
60 ) : KSerializer<SnapshotStateMap<K, V>> {
61
62 private val base = MapSerializer(keySerializer, valueSerializer)
63
64 @OptIn(ExperimentalSerializationApi::class)
65 override val descriptor: SerialDescriptor =
66 SerialDescriptor("androidx.compose.runtime.SnapshotStateMap", base.descriptor)
67
serializenull68 override fun serialize(encoder: Encoder, value: SnapshotStateMap<K, V>) {
69 encoder.encodeSerializableValue(base, value)
70 }
71
deserializenull72 override fun deserialize(decoder: Decoder): SnapshotStateMap<K, V> {
73 val deserialized = decoder.decodeSerializableValue(base)
74 return SnapshotStateMap<K, V>().apply { putAll(deserialized) }
75 }
76 }
77