What is wrong w/ this CBOR code (1.0.0-RC, kotlin ...
# serialization
t
What is wrong w/ this CBOR code (1.0.0-RC, kotlin ver : 1.4.10) Throws the exception on the decode:
kotlinx.serialization.cbor.internal.CborDecodingException: Expected start of array, but found BF
Copy code
@Serializable
sealed class BaseClass()

@Serializable
data class TypeOne(val name:String) : BaseClass()

@Serializable
data class TypeTwo(val num:Int) : BaseClass()


@ExperimentalSerializationApi
fun main(args: Array<String>) {
    
    val bb = Cbor.encodeToByteArray(TypeOne("my name is mud"))

    val msg = Cbor.decodeFromByteArray<BaseClass>( bb )
    
    println(msg)

}
actually, this doesn't work for json either.
Copy code
fun main(args: Array<String>) {

    val bb = Json.encodeToString(TypeOne("my name is mud"))

    println(bb)

    val msg = Json.decodeFromString<BaseClass>( bb )

    println(msg)

}
throws
kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
do sealed classes, or something just not work at all? is there a workaround?
nope... changed it to from sealed to open ... failure.
it's not even trying to encode the type. do hierarchies just not work at all?
getting major "amateur hour" vibes from this thing. Is this just a toy?
v
No, it works fine. But if you need polymorphism you have to do explicitly afair. Did you read the docs about polymorphism?
t
yes. the code looks pretty much identical. except for the use of data classes
what's different?
I just to this code :
And altered one line `
Copy code
val list = listOf(TextResponse("OK"))
and all the type information is gone.
it's just [{"Test":"OK}}
WTF....
for polymorphic you must serialize no less than two in order for it to work?
I can't use a
SerializerModule
because BaseClass or Response (from the docs) class is sealed / abstract.
so.... not sure what you mean by "it works fine" except that it doesn't work fine.
figured it out. You have to cast it to the base class.
v
instead of cast, you also can use a (opionated) slightly more readable
Cbor.encodeToByteArray<BaseClass>(TypeOne("my name is mud"))
👌 1