beginStructure

open override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder(source)

Encodes the beginning of the nested structure in a serialized form and returns CompositeDecoder responsible for encoding this very structure. E.g the hierarchy:

class StringHolder(val stringValue: String)
class Holder(val stringHolder: StringHolder)

with the following serialized form in JSON:

{
"stringHolder" : { "stringValue": "value" }
}

will be roughly represented as the following sequence of calls:

// Holder serializer
fun serialize(encoder: Encoder, value: Holder) {
val composite = encoder.beginStructure(descriptor) // the very first opening bracket '{'
composite.encodeSerializableElement(descriptor, 0, value.stringHolder) // Serialize nested StringHolder
composite.endStructure(descriptor) // The very last closing bracket
}

// StringHolder serializer
fun serialize(encoder: Encoder, value: StringHolder) {
val composite = encoder.beginStructure(descriptor) // One more '{' when the key "stringHolder" is already written
composite.encodeStringElement(descriptor, 0, value.stringValue) // Serialize actual value
composite.endStructure(descriptor) // Closing bracket
}