CompositeEncoder

interface CompositeEncoder

CompositeEncoder is a part of encoding process that is bound to a particular structured part of the serialized form, described by the serial descriptor passed to Encoder.beginStructure.

All encode* methods have index and serialDescriptor parameters with a strict semantics and constraints:

  • descriptor is always the same as one used in Encoder.beginStructure. While this parameter may seem redundant, it is required for efficient serialization process to avoid excessive field spilling. If you are writing your own format, you can safely ignore this parameter and use one used in beginStructure for simplicity.

  • index of the element being encoded. This element at this index in the descriptor should be associated with the one being written.

The symmetric interface for the deserialization process is CompositeDecoder.

Not stable for inheritance

CompositeEncoder 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.

Functions

encodeBooleanElement
Link copied to clipboard
abstract 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.

encodeByteElement
Link copied to clipboard
abstract 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.

encodeCharElement
Link copied to clipboard
abstract 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.

encodeDoubleElement
Link copied to clipboard
abstract 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.

encodeFloatElement
Link copied to clipboard
abstract 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.

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

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

encodeIntElement
Link copied to clipboard
abstract 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.

encodeLongElement
Link copied to clipboard
abstract 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.

encodeNullableSerializableElement
Link copied to clipboard
abstract 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.

encodeSerializableElement
Link copied to clipboard
abstract 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.

encodeShortElement
Link copied to clipboard
abstract 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.

encodeStringElement
Link copied to clipboard
abstract 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.

endStructure
Link copied to clipboard
abstract 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.

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

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:

Properties

serializersModule
Link copied to clipboard
abstract val serializersModule: SerializersModule

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

Inheritors

AbstractEncoder
Link copied to clipboard