I was trying to use one of the Kotlin's serialization package with the following custom example class. I will extend this class matching the real use case by adding a List
of the real class (which also need to be serialized), but right now the problem simply lies in de-serialization.
I've managed to make the serialization works (in the second snippet of code there's both the object to be serialized and the resulting String
) and seems that the de-serialization process requires to be done in the same way (i.e., by using an instance of NullableSerializer
) (in fact, I haven't found a simple and correct way to de-serialize an encoded object right now.
Here's the class, simple but wannabe-complex.
@Serializable(with = EhEH.Companion::class)
data class EhEH(
val i: Int
, val txt: String
//, val list: Array<String> // next feature, also with complex data, not only "simple" Strings
) {
@Serializer(forClass = EhEH::class)
companion object : KSerializer<EhEH> {
public var deserialEhEH: DeserializationStrategy<EhEH> = EhEH
override val descriptor: SerialDescriptor =
SerialDescriptor("EhEHSerializer", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, obj: EhEH) =
encoder.encodeString(obj::class.qualifiedName!!)
override fun deserialize(decoder: Decoder): EhEH {
val dec = decoder.beginStructure(descriptor)
var txt: String? = null
var x: Int? = null
var i = -1
var notFinished = true
do {
i = dec.decodeElementIndex(descriptor)
when (i) {
CompositeDecoder.READ_DONE -> notFinished = false
0 -> x = dec.decodeStringElement(descriptor, i).toIntOrNull()
1 -> txt = dec.decodeStringElement(descriptor, i)
else -> throw SerializationException("Unknown index $i")
}
} while (notFinished)
dec.endStructure(descriptor)
return EhEH(
x ?: throw MissingFieldException("x"),
txt ?: throw MissingFieldException("txt")
)
}
}
override fun toString(): String {
return "EhEH(i=$i, s='$txt')"
}
}
The manual-test function (whose value is simply printed) is:
@InternalSerializationApi
fun testCborString(): String {
var e: EhEH = EhEH(
7,
"poreccio"
//, listOf("just", "another", "vacuum", "test")
)
return Cbor.dumps(
NullableSerializer(
EhEH.Companion
), e
) + " <-> " + (
{
(Cbor.load(
NullableSerializer(
EhEH.Companion
),
Cbor.dump(
NullableSerializer(EhEH.Companion), e
)
//the dumped value should be "781d62632e7472797669756d2e6170692e726573706f6e7365732e45684548"
)
as EhEH).toString()
} as () -> String)()
}
When I try to run it, the exception lies in the Cbor.load
call and is:
kotlinx.serialization.cbor.CborDecodingException: Expected start of map, but found 78
Am I the only one in WHOLE internet having this problem?