JsonEncoder

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.

Properties

Link copied to clipboard
abstract val json: Json

An instance of the current Json.

Link copied to clipboard

Functions

Link copied to clipboard
open fun beginCollection(descriptor: SerialDescriptor, collectionSize: Int): CompositeEncoder
Link copied to clipboard
Link copied to clipboard
abstract fun encodeBoolean(value: Boolean)
Link copied to clipboard
abstract fun encodeBooleanElement(descriptor: SerialDescriptor, index: Int, value: Boolean)
Link copied to clipboard
abstract fun encodeByte(value: Byte)
Link copied to clipboard
abstract fun encodeByteElement(descriptor: SerialDescriptor, index: Int, value: Byte)
Link copied to clipboard
abstract fun encodeChar(value: Char)
Link copied to clipboard
abstract fun encodeCharElement(descriptor: SerialDescriptor, index: Int, value: Char)
Link copied to clipboard
abstract fun encodeDouble(value: Double)
Link copied to clipboard
abstract fun encodeDoubleElement(descriptor: SerialDescriptor, index: Int, value: Double)
Link copied to clipboard
abstract fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int)
Link copied to clipboard
abstract fun encodeFloat(value: Float)
Link copied to clipboard
abstract fun encodeFloatElement(descriptor: SerialDescriptor, index: Int, value: Float)
Link copied to clipboard
abstract fun encodeInline(descriptor: SerialDescriptor): Encoder
Link copied to clipboard
abstract fun encodeInlineElement(descriptor: SerialDescriptor, index: Int): Encoder
Link copied to clipboard
abstract fun encodeInt(value: Int)
Link copied to clipboard
abstract fun encodeIntElement(descriptor: SerialDescriptor, index: Int, value: Int)
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:

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