Hi there! Facing troubles with Serialization… Could someone explain me what’s wrong and what I need ...
r
Hi there! Facing troubles with Serialization… Could someone explain me what’s wrong and what I need to do? Using serialization 1.6.10…
Copy code
sealed interface Convertible {

    fun convertUserInput(value : String): String
}


@Serializable
@SerialName("CustomConvertible")
class CustomConvertible(): Convertible {


    override fun convertUserInput(value : String): String {
       return ""
    }
}


@Serializable
class DTOAttribute(val convertibles         : List<Convertible>   = emptyList())
Later on, I’d like to encode the
DTOAttribute
with
val string = Json.encodeToString(dtoAttr)
Calling this, gives me the following exception:
Copy code
kotlinx.serialization.SerializationException: Class 'CustomConvertible' is not registered for polymorphic serialization in the scope of 'Convertible'.
Mark the base class as 'sealed' or register the serializer explicitly.
What am I doing wrong?
e
use
sealed class
or upgrade to Kotlin 1.6.20 https://github.com/Kotlin/kotlinx.serialization/issues/1576
also the base type needs to be
@Serializable
r
Thanks, that actually helped 🙂