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.SnapshotStateList 20 import kotlinx.serialization.ExperimentalSerializationApi 21 import kotlinx.serialization.KSerializer 22 import kotlinx.serialization.builtins.ListSerializer 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 [SnapshotStateList] containing serializable elements of type [T]. 30 * 31 * This inline function automatically infers the element type [T] and retrieves the corresponding 32 * [KSerializer] for serializing and deserializing [SnapshotStateList] instances. 33 * 34 * @param T The type of elements stored in the [SnapshotStateList]. 35 * @return A [SnapshotStateListSerializer] for handling serialization and deserialization of a 36 * [SnapshotStateList] containing elements of type [T]. 37 */ SnapshotStateListSerializernull38public inline fun <reified T> SnapshotStateListSerializer(): SnapshotStateListSerializer<T> { 39 return SnapshotStateListSerializer(serializer()) 40 } 41 42 /** 43 * A [KSerializer] for [SnapshotStateList]. 44 * 45 * This serializer wraps a [KSerializer] for the element type [T], enabling serialization and 46 * deserialization of [SnapshotStateList] instances. The serialization of individual elements is 47 * delegated to the provided [elementSerializer]. 48 * 49 * @param T The type of elements stored in the [SnapshotStateList]. 50 * @param elementSerializer The [KSerializer] used to serialize and deserialize individual elements. 51 */ 52 public class SnapshotStateListSerializer<T>( 53 private val elementSerializer: KSerializer<T>, 54 ) : KSerializer<SnapshotStateList<T>> { 55 56 private val base = ListSerializer(elementSerializer) 57 58 @OptIn(ExperimentalSerializationApi::class) 59 override val descriptor: SerialDescriptor = 60 SerialDescriptor("androidx.compose.runtime.SnapshotStateList", base.descriptor) 61 serializenull62 override fun serialize(encoder: Encoder, value: SnapshotStateList<T>) { 63 encoder.encodeSerializableValue(base, value) 64 } 65 deserializenull66 override fun deserialize(decoder: Decoder): SnapshotStateList<T> { 67 val deserialized = decoder.decodeSerializableValue(base) 68 return SnapshotStateList<T>().apply { addAll(deserialized.toList()) } 69 } 70 } 71