I'm trying to convert a piece of Moshi code in a J...
# serialization
p
I'm trying to convert a piece of Moshi code in a JsonAdapter to a equivalent KSerializer without success. Code in thread 🧵
Moshi:
Copy code
reader.beginObject()
while (reader.hasNext()) {
    when (reader.nextName()) {
        "Id" -> id = reader.nextInt()
        "Order" -> order = reader.nextInt()
        "Type" -> type = ComponentType.fromString(reader.nextString())!!
        "Data" -> dataString = ioMoshi.toJson(reader.readJsonValue())
    }
}
reader.endObject()
What i have tried in Kotlinx serialization:
Copy code
decoder.decodeStructure(descriptor) {
    while (true) {
        when (val index = decodeElementIndex(descriptor)) {
            0 -> id = decodeIntElement(descriptor, index)
            1 -> order = decodeIntElement(descriptor, index)
            2 -> type = ComponentType.fromString(decodeStringElement(descriptor, index))
            3 -> dataString =
                json.encode<Any>(
                    this.decodeSerializableElement(
                        descriptor,
                        index,
                        Any.serializer()
                    )
                )
            CompositeDecoder.DECODE_DONE -> break
        }
    }
}
However this does not work at all i have tried all sorts of variations. It always results in some new exception. What is the correct way i Kotlin serialization