decodeJsonElement

Decodes the next element in the current input as JsonElement. The type of the decoded element depends on the current state of the input and, when received by serializer in its KSerializer.serialize method, the type of the token directly matches the kind.

This method is allowed to invoke only as the part of the whole deserialization process of the class, calling this method after invoking beginStructure or any decode* method will lead to unspecified behaviour. For example:

class Holder(val value: Int, val list: List<Int>())

// Holder deserialize method
fun deserialize(decoder: Decoder): Holder {
// Completely okay, the whole Holder object is read
val jsonObject = (decoder as JsonDecoder).decodeJsonElement()
// ...
}

// Incorrect Holder deserialize method
fun deserialize(decoder: Decoder): Holder {
// decode "value" key unconditionally
decoder.decodeElementIndex(descriptor)
val value = decode.decodeInt()
// Incorrect, decoder is already in an intermediate state after decodeInt
val json = (decoder as JsonDecoder).decodeJsonElement()
// ...
}