JsonDecoder

interface JsonDecoder : Decoder, CompositeDecoder

Decoder used by Json during deserialization. This interface can be used to inject desired behaviour into a serialization process of Json.

Typical example of the usage:

// Class representing Either<Left|Right>
sealed class Either {
data class Left(val errorMsg: String) : Either()
data class Right(val data: Payload) : Either()
}

// Serializer injects custom behaviour by inspecting object content and writing
object EitherSerializer : KSerializer<Either> {
override val descriptor: SerialDescriptor = buildSerialDescriptor("package.Either", PolymorphicKind.SEALED) {
// ..
}

override fun deserialize(decoder: Decoder): Either {
val input = decoder as? JsonDecoder ?: throw SerializationException("This class can be decoded only by Json format")
val tree = input.decodeJsonElement() as? JsonObject ?: throw SerializationException("Expected JsonObject")
if ("error" in tree) return Either.Left(tree["error"]!!.jsonPrimitive.content)
return Either.Right(input.json.decodeFromJsonElement(Payload.serializer(), tree))
}

override fun serialize(encoder: Encoder, value: Either) {
val output = encoder as? JsonEncoder ?: throw SerializationException("This class can be encoded only by Json format")
val tree = when (value) {
is Either.Left -> JsonObject(mapOf("error" to JsonPrimitive(value.errorMsg)))
is Either.Right -> output.json.encodeToJsonElement(Payload.serializer(), value.data)
}
output.encodeJsonElement(tree)
}
}

Not stable for inheritance

JsonDecoder interface is not stable for inheritance in 3rd party libraries, as new methods might be added to this interface or contracts of the existing methods can be changed. Accepting this interface in your API methods, casting Decoder to JsonDecoder and invoking its methods is considered stable.

Functions

beginStructure
Link copied to clipboard
abstract fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder
decodeBoolean
Link copied to clipboard
abstract fun decodeBoolean(): Boolean
decodeBooleanElement
Link copied to clipboard
abstract fun decodeBooleanElement(descriptor: SerialDescriptor, index: Int): Boolean
decodeByte
Link copied to clipboard
abstract fun decodeByte(): Byte
decodeByteElement
Link copied to clipboard
abstract fun decodeByteElement(descriptor: SerialDescriptor, index: Int): Byte
decodeChar
Link copied to clipboard
abstract fun decodeChar(): Char
decodeCharElement
Link copied to clipboard
abstract fun decodeCharElement(descriptor: SerialDescriptor, index: Int): Char
decodeCollectionSize
Link copied to clipboard
open fun decodeCollectionSize(descriptor: SerialDescriptor): Int
decodeDouble
Link copied to clipboard
abstract fun decodeDouble(): Double
decodeDoubleElement
Link copied to clipboard
abstract fun decodeDoubleElement(descriptor: SerialDescriptor, index: Int): Double
decodeElementIndex
Link copied to clipboard
abstract fun decodeElementIndex(descriptor: SerialDescriptor): Int
decodeEnum
Link copied to clipboard
abstract fun decodeEnum(enumDescriptor: SerialDescriptor): Int
decodeFloat
Link copied to clipboard
abstract fun decodeFloat(): Float
decodeFloatElement
Link copied to clipboard
abstract fun decodeFloatElement(descriptor: SerialDescriptor, index: Int): Float
decodeInline
Link copied to clipboard
abstract fun decodeInline(inlineDescriptor: SerialDescriptor): Decoder
decodeInlineElement
Link copied to clipboard
abstract fun decodeInlineElement(descriptor: SerialDescriptor, index: Int): Decoder
decodeInt
Link copied to clipboard
abstract fun decodeInt(): Int
decodeIntElement
Link copied to clipboard
abstract fun decodeIntElement(descriptor: SerialDescriptor, index: Int): Int
decodeJsonElement
Link copied to clipboard
abstract fun decodeJsonElement(): JsonElement

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.

decodeLong
Link copied to clipboard
abstract fun decodeLong(): Long
decodeLongElement
Link copied to clipboard
abstract fun decodeLongElement(descriptor: SerialDescriptor, index: Int): Long
decodeNotNullMark
Link copied to clipboard
abstract fun decodeNotNullMark(): Boolean
decodeNull
Link copied to clipboard
abstract fun decodeNull(): Nothing?
decodeNullableSerializableElement
Link copied to clipboard
abstract fun <T : Any> decodeNullableSerializableElement(descriptor: SerialDescriptor, index: Int, deserializer: DeserializationStrategy<T?>, previousValue: T?): T?
decodeNullableSerializableValue
Link copied to clipboard
open fun <T : Any> decodeNullableSerializableValue(deserializer: DeserializationStrategy<T?>): T?
decodeSequentially
Link copied to clipboard
open fun decodeSequentially(): Boolean
decodeSerializableElement
Link copied to clipboard
abstract fun <T> decodeSerializableElement(descriptor: SerialDescriptor, index: Int, deserializer: DeserializationStrategy<T>, previousValue: T?): T
decodeSerializableValue
Link copied to clipboard
open fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T
decodeShort
Link copied to clipboard
abstract fun decodeShort(): Short
decodeShortElement
Link copied to clipboard
abstract fun decodeShortElement(descriptor: SerialDescriptor, index: Int): Short
decodeString
Link copied to clipboard
abstract fun decodeString(): String
decodeStringElement
Link copied to clipboard
abstract fun decodeStringElement(descriptor: SerialDescriptor, index: Int): String
endStructure
Link copied to clipboard
abstract fun endStructure(descriptor: SerialDescriptor)

Properties

json
Link copied to clipboard
abstract val json: Json

An instance of the current Json.

serializersModule
Link copied to clipboard
abstract val serializersModule: SerializersModule