Anyone using a custom `KSerializer` for `Immutable...
# serialization
d
Anyone using a custom
KSerializer
for
ImmutableList
and having a an error when updating to kotlinx.serialization 1.8.0? I get the error
Copy code
This class or interface requires opt-in to be implemented: This class or interface should not be inherited/implemented outside of kotlinx.serialization library. Note it is still permitted to use it directly. Read its documentation about inheritance for details.
on this code
Copy code
@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = ImmutableList::class)
class ImmutableListSerializer<T>(
    private val dataSerializer: KSerializer<T>,
) : KSerializer<ImmutableList<T>> {
    private class PersistentListDescriptor<T> : SerialDescriptor by serialDescriptor<List<T>>() { <-- The error is caused by this line
        override val serialName: String = "kotlinx.collections.immutable.ImmutableList"
    }

    @OptIn(ExperimentalSerializationApi::class)
    override val descriptor: SerialDescriptor = PersistentListDescriptor<T>()

    override fun serialize(
        encoder: Encoder,
        value: ImmutableList<T>,
    ) = ListSerializer(dataSerializer).serialize(encoder, value.toList())

    override fun deserialize(decoder: Decoder): ImmutableList<T> = ListSerializer(dataSerializer).deserialize(decoder).toImmutableList()
}
Its mentioned here in the changelog https://github.com/Kotlin/kotlinx.serialization/releases/tag/v1.8.0 and references the following documentation https://kotlinlang.org/docs/opt-in-requirements.html#opt-in-to-api I assumed I would need to put a annotation on it but that doesn't fix it. How should I fix this?
This seems to fix it
Copy code
@Suppress("EXTERNAL_SERIALIZER_USELESS")
@Serializer(forClass = ImmutableList::class)
class ImmutableListSerializer<T>(
    private val dataSerializer: KSerializer<T>,
) : KSerializer<ImmutableList<T>> {

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

    override fun serialize(
        encoder: Encoder,
        value: ImmutableList<T>,
    ) = ListSerializer(dataSerializer).serialize(encoder, value.toList())

    override fun deserialize(decoder: Decoder): ImmutableList<T> = ListSerializer(dataSerializer).deserialize(decoder).toImmutableList()
}