CLOVIS
06/22/2025, 8:50 AMAbstractEncoder
.
Let's say I want to serialize:
@Serializable
class User(val id: Int)
From what I understand, KotlinX.Serialization will call:
MyEncoder.beginStructure(User.serializer.serialDescriptor).apply {
encodeInt(1234)
endStructure(User.serializer.serialDescriptor)
}
What I don't understand is: when encodeInt(1234)
is called, how do I know it's the field id
and not another field that could exist? Am I supposed to store the serialDescriptor
of the enclosing class, keep track of the index of the last written element, and use that to query the descriptor to know what the field is?
TL;DR: within the implementation of encodeInt()
I need to find to know the current field's name, but I don't know how to.Adam S
06/22/2025, 12:24 PMid
should be encoded using AbstractEncoder.encodeElement(descriptor: SerialDescriptor, index: Int)
(which will eventually call encodeInt()
)Adam S
06/22/2025, 12:28 PMbeginStructure(...)
helps
https://github.com/Kotlin/kotlinx.serialization/blob/4667a1891a925dc9e3e10490c274a875b0be4da6/core/commonMain/src/kotlinx/serialization/encoding/Encoding.kt#L230-L263CLOVIS
06/23/2025, 7:39 AM