AbstractEncoder

A skeleton implementation of both Encoder and CompositeEncoder that can be used for simple formats and for testability purpose. Most of the encode* methods have default implementation that delegates encodeValue(value: Any). See Encoder documentation for information about each particular encode* method.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

Context of the current serialization process, including contextual and polymorphic serialization and, potentially, a format-specific configuration.

Functions

Link copied to clipboard
open fun beginCollection(descriptor: SerialDescriptor, collectionSize: Int): CompositeEncoder

Encodes the beginning of the collection with size collectionSize and the given serializer of its type parameters. This method has to be implemented only if you need to know collection size in advance, otherwise, beginStructure can be used.

Link copied to clipboard
open override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder

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

Link copied to clipboard
open override fun encodeBoolean(value: Boolean)

Encodes a boolean value. Corresponding kind is PrimitiveKind.BOOLEAN.

Link copied to clipboard
override fun encodeBooleanElement(descriptor: SerialDescriptor, index: Int, value: Boolean)

Encodes a boolean value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.BOOLEAN kind.

Link copied to clipboard
open override fun encodeByte(value: Byte)

Encodes a single byte value. Corresponding kind is PrimitiveKind.BYTE.

Link copied to clipboard
override fun encodeByteElement(descriptor: SerialDescriptor, index: Int, value: Byte)

Encodes a single byte value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.BYTE kind.

Link copied to clipboard
open override fun encodeChar(value: Char)

Encodes a 16-bit unicode character value. Corresponding kind is PrimitiveKind.CHAR.

Link copied to clipboard
override fun encodeCharElement(descriptor: SerialDescriptor, index: Int, value: Char)

Encodes a 16-bit unicode character value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.CHAR kind.

Link copied to clipboard
inline fun Encoder.encodeCollection(descriptor: SerialDescriptor, collectionSize: Int, crossinline block: CompositeEncoder.() -> Unit)

Begins a collection, encodes it using the given block and ends it.

inline fun <E> Encoder.encodeCollection(descriptor: SerialDescriptor, collection: Collection<E>, crossinline block: CompositeEncoder.(index: Int, E) -> Unit)

Begins a collection, calls block with each item and ends the collections.

Link copied to clipboard
open override fun encodeDouble(value: Double)

Encodes a 64-bit IEEE 754 floating point value. Corresponding kind is PrimitiveKind.DOUBLE.

Link copied to clipboard
override fun encodeDoubleElement(descriptor: SerialDescriptor, index: Int, value: Double)

Encodes a 64-bit IEEE 754 floating point value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.DOUBLE kind.

Link copied to clipboard
open fun encodeElement(descriptor: SerialDescriptor, index: Int): Boolean

Invoked before writing an element that is part of the structure to determine whether it should be encoded. Element information can be obtained from the descriptor by the given index.

Link copied to clipboard
open override fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int)

Encodes a enum value that is stored at the index in enumDescriptor elements collection. Corresponding kind is SerialKind.ENUM.

Link copied to clipboard
open override fun encodeFloat(value: Float)

Encodes a 32-bit IEEE 754 floating point value. Corresponding kind is PrimitiveKind.FLOAT.

Link copied to clipboard
override fun encodeFloatElement(descriptor: SerialDescriptor, index: Int, value: Float)

Encodes a 32-bit IEEE 754 floating point value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.FLOAT kind.

Link copied to clipboard
open override fun encodeInline(descriptor: SerialDescriptor): Encoder

Returns Encoder for encoding an underlying type of a value class in an inline manner. descriptor describes a serializable value class.

Link copied to clipboard
override fun encodeInlineElement(descriptor: SerialDescriptor, index: Int): Encoder

Returns Encoder for decoding an underlying type of a value class in an inline manner. Serializable value class is described by the child descriptor of given descriptor at index.

Link copied to clipboard
open override fun encodeInt(value: Int)

Encodes a 32-bit integer value. Corresponding kind is PrimitiveKind.INT.

Link copied to clipboard
override fun encodeIntElement(descriptor: SerialDescriptor, index: Int, value: Int)

Encodes a 32-bit integer value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.INT kind.

Link copied to clipboard
open override fun encodeLong(value: Long)

Encodes a 64-bit integer value. Corresponding kind is PrimitiveKind.LONG.

Link copied to clipboard
override fun encodeLongElement(descriptor: SerialDescriptor, index: Int, value: Long)

Encodes a 64-bit integer value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.LONG kind.

Link copied to clipboard

Notifies the encoder that value of a nullable type that is being serialized is not null. It should be called before writing a non-null value of nullable type:

Link copied to clipboard
open override fun encodeNull()

Encodes null value.

Link copied to clipboard
open override fun <T : Any> encodeNullableSerializableElement(descriptor: SerialDescriptor, index: Int, serializer: SerializationStrategy<T>, value: T?)

Delegates nullable value encoding of the type T to the given serializer. value is associated with an element at the given index in serial descriptor.

Link copied to clipboard

Encodes the nullable value of type T by delegating the encoding process to the given serializer.

Link copied to clipboard
open override fun <T> encodeSerializableElement(descriptor: SerialDescriptor, index: Int, serializer: SerializationStrategy<T>, value: T)

Delegates value encoding of the type T to the given serializer. value is associated with an element at the given index in serial descriptor.

Link copied to clipboard
open fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T)

Encodes the value of type T by delegating the encoding process to the given serializer. For example, encodeInt call is equivalent to delegating integer encoding to Int.serializer: encodeSerializableValue(Int.serializer())

Link copied to clipboard
open override fun encodeShort(value: Short)

Encodes a 16-bit short value. Corresponding kind is PrimitiveKind.SHORT.

Link copied to clipboard
override fun encodeShortElement(descriptor: SerialDescriptor, index: Int, value: Short)

Encodes a 16-bit short value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.SHORT kind.

Link copied to clipboard
open override fun encodeString(value: String)

Encodes a string value. Corresponding kind is PrimitiveKind.STRING.

Link copied to clipboard
override fun encodeStringElement(descriptor: SerialDescriptor, index: Int, value: String)

Encodes a string value associated with an element at the given index in serial descriptor. The element at the given index should have PrimitiveKind.STRING kind.

Link copied to clipboard
inline fun Encoder.encodeStructure(descriptor: SerialDescriptor, crossinline block: CompositeEncoder.() -> Unit)

Begins a structure, encodes it using the given block and ends it.

Link copied to clipboard
open fun encodeValue(value: Any)

Invoked to encode a value when specialized encode* method was not overridden.

Link copied to clipboard
open override fun endStructure(descriptor: SerialDescriptor)

Denotes the end of the structure associated with current encoder. For example, composite encoder of JSON format will write a closing bracket in the underlying input and reduce the number of nesting for pretty printing.

Link copied to clipboard

Whether the format should encode values that are equal to the default values. This method is used by plugin-generated serializers for properties with default values: