Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I set up a serializer for SnapshotStateList, but it doesn't work #2603

Open
little-creeper opened this issue Mar 17, 2024 · 1 comment
Open
Labels

Comments

@little-creeper
Copy link

little-creeper commented Mar 17, 2024

image

package models

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable
data class Server(
    var id:Int,
    var name:MutableState<String>,
    @Serializable(with=SnapshotListSerializer::class) var urls: SnapshotStateList<Address>
)

class SnapshotListSerializer<T>(private val dataSerializer: KSerializer<T>) :
    KSerializer<SnapshotStateList<T>> {

    override val descriptor: SerialDescriptor = ListSerializer(dataSerializer).descriptor

    override fun serialize(encoder: Encoder, value: SnapshotStateList<T>) {
        encoder.encodeSerializableValue(ListSerializer(dataSerializer), value as List<T>)
    }

    override fun deserialize(decoder: Decoder): SnapshotStateList<T> {
        val list = mutableStateListOf<T>()
        val items = decoder.decodeSerializableValue(ListSerializer(dataSerializer))
        list.addAll(items)
        return list
    }
}

I was expecting to serialize/deserialize the SnapshotStateList, and a serializer was set, but I got an error

image

How do I fix this?

Exception in thread "AWT-EventQueue-0" kotlinx.serialization.SerializationException: Serializer for subclass 'SnapshotMutableStateImpl' is not found in the polymorphic scope of 'MutableState'.
Check if class with serial name 'SnapshotMutableStateImpl' exists and serializer is registered in a corresponding SerializersModule.
To be registered automatically, class 'SnapshotMutableStateImpl' has to be '@Serializable', and the base class 'MutableState' has to be sealed and '@Serializable'.
	at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:102)
	at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:114)
	at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:242)
	at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableElement(AbstractEncoder.kt:80)
	at models.Server.write$Self$TsTeA(Server.kt:16)
	at models.Server$$serializer.serialize(Server.kt:16)
	at models.Server$$serializer.serialize(Server.kt:16)
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:249)
	at kotlinx.serialization.encoding.AbstractEncoder.encodeSerializableElement(AbstractEncoder.kt:80)
	at kotlinx.serialization.internal.CollectionLikeSerializer.serialize(CollectionSerializers.kt:69)
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:249)
	at kotlinx.serialization.json.internal.JsonStreamsKt.encodeByWriter(JsonStreams.kt:31)
	at kotlinx.serialization.json.Json.encodeToString(Json.kt:81)
	at pages.mainpage.MainPageKt$MainPage$1$1$2$1$3.invoke(MainPage.kt:98)
	at pages.mainpage.MainPageKt$MainPage$1$1$2$1$3.invoke(MainPage.kt:49)
......

@sandwwraith
Copy link
Member

You also need to provide a serializer for MutableState or its subclasses, because MutableState is an interface. See here: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants