JsonEncoder

interface JsonEncoder : Encoder, CompositeEncoder

Encoder used by Json during serialization. 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 JsonPrimitve(value.errorMsg)))
is Either.Right -> output.json.encodeToJsonElement(Payload.serializer(), value.data)
}
output.encodeJsonElement(tree)
}
}

Not stable for inheritance

JsonEncoder 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 Encoder to JsonEncoder and invoking its methods is considered stable.

Functions

beginCollection
Link copied to clipboard
open fun beginCollection(descriptor: SerialDescriptor, collectionSize: Int): CompositeEncoder
beginStructure
Link copied to clipboard
abstract fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder
encodeBoolean
Link copied to clipboard
abstract fun encodeBoolean(value: Boolean)
encodeBooleanElement
Link copied to clipboard
abstract fun encodeBooleanElement(descriptor: SerialDescriptor, index: Int, value: Boolean)
encodeByte
Link copied to clipboard
abstract fun encodeByte(value: Byte)
encodeByteElement
Link copied to clipboard
abstract fun encodeByteElement(descriptor: SerialDescriptor, index: Int, value: Byte)
encodeChar
Link copied to clipboard
abstract fun encodeChar(value: Char)
encodeCharElement
Link copied to clipboard
abstract fun encodeCharElement(descriptor: SerialDescriptor, index: Int, value: Char)
encodeDouble
Link copied to clipboard
abstract fun encodeDouble(value: Double)
encodeDoubleElement
Link copied to clipboard
abstract fun encodeDoubleElement(descriptor: SerialDescriptor, index: Int, value: Double)
encodeEnum
Link copied to clipboard
abstract fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int)
encodeFloat
Link copied to clipboard
abstract fun encodeFloat(value: Float)
encodeFloatElement
Link copied to clipboard
abstract fun encodeFloatElement(descriptor: SerialDescriptor, index: Int, value: Float)
encodeInline
Link copied to clipboard
abstract fun encodeInline(inlineDescriptor: SerialDescriptor): Encoder
encodeInlineElement
Link copied to clipboard
abstract fun encodeInlineElement(descriptor: SerialDescriptor, index: Int): Encoder
encodeInt
Link copied to clipboard
abstract fun encodeInt(value: Int)
encodeIntElement
Link copied to clipboard
abstract fun encodeIntElement(descriptor: SerialDescriptor, index: Int, value: Int)
encodeJsonElement
Link copied to clipboard
abstract fun encodeJsonElement(element: JsonElement)

Appends the given JSON element to the current output. This method is allowed to invoke only as the part of the whole serialization process of the class, calling this method after invoking beginStructure or any encode* method will lead to unspecified behaviour and may produce an invalid JSON result. For example:

encodeLong
Link copied to clipboard
abstract fun encodeLong(value: Long)
encodeLongElement
Link copied to clipboard
abstract fun encodeLongElement(descriptor: SerialDescriptor, index: Int, value: Long)
encodeNotNullMark
Link copied to clipboard
open fun encodeNotNullMark()
encodeNull
Link copied to clipboard
abstract fun encodeNull()
encodeNullableSerializableElement
Link copied to clipboard
abstract fun <T : Any> encodeNullableSerializableElement(descriptor: SerialDescriptor, index: Int, serializer: SerializationStrategy<T>, value: T?)
encodeNullableSerializableValue
Link copied to clipboard
open fun <T : Any> encodeNullableSerializableValue(serializer: SerializationStrategy<T>, value: T?)
encodeSerializableElement
Link copied to clipboard
abstract fun <T> encodeSerializableElement(descriptor: SerialDescriptor, index: Int, serializer: SerializationStrategy<T>, value: T)
encodeSerializableValue
Link copied to clipboard
open fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T)
encodeShort
Link copied to clipboard
abstract fun encodeShort(value: Short)
encodeShortElement
Link copied to clipboard
abstract fun encodeShortElement(descriptor: SerialDescriptor, index: Int, value: Short)
encodeString
Link copied to clipboard
abstract fun encodeString(value: String)
encodeStringElement
Link copied to clipboard
abstract fun encodeStringElement(descriptor: SerialDescriptor, index: Int, value: String)
endStructure
Link copied to clipboard
abstract fun endStructure(descriptor: SerialDescriptor)
shouldEncodeElementDefault
Link copied to clipboard
open fun shouldEncodeElementDefault(descriptor: SerialDescriptor, index: Int): Boolean

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