interface KSerializer<T> : SerializationStrategy<T>, DeserializationStrategy<T>
KSerializer is responsible for the representation of a serial form of a type T
in terms of encoders and decoders and for constructing and deconstructing T
from/to a sequence of encoding primitives. For classes marked with @Serializable, can be
obtained from generated companion extension .serializer()
or from serializer() function.
Serialization is decoupled from the encoding process to make it completely format-agnostic. Serialization represents a type as its serial form and is abstracted from the actual format (whether its JSON, ProtoBuf or a hashing) and unaware of the underlying storage (whether it is a string builder, byte array or a network socket), while encoding/decoding is abstracted from a particular type and its serial form and is responsible for transforming primitives ("here in an int property 'foo'" call from a serializer) into a particular format-specific representation ("for a given int, append a property name in quotation marks, then append a colon, then append an actual value" for JSON) and how to retrieve a primitive ("give me an int that is 'foo' property") from the underlying representation ("expect the next string to be 'foo', parse it, then parse colon, then parse a string until the next comma as an int and return it).
Serial form consists of a structural description, declared by the descriptor and actual serialization and deserialization processes, defined by the corresponding serialize and deserialize methods implementation.
Structural description specifies how the T is represented in the serial form: its kind (e.g. whether it is represented as a primitive, a list or a class), its elements and their positional names.
Serialization process is defined as a sequence of calls to an Encoder, and transforms a type T into a stream of format-agnostic primitives that represent T, such as "here is an int, here is a double and here is another nested object". It can be demonstrated by the example:
class MyData(int: Int, stringList: List<String>, alwaysZero: Long)
// .. serialize method of a corresponding serializer
fun serialize(encoder: Encoder, value: MyData): Unit = encoder.encodeStructure(descriptor) {
// encodeStructure encodes beginning and end of the structure
// encode 'int' property as Int
encodeIntElement(descriptor, index = 0, value.int)
// encode 'stringList' property as List<String>
encodeSerializableElement(descriptor, index = 1, serializer<List<String>>, value.stringList)
// don't encode 'alwaysZero' property because we decided to do so
} // end of the structure
Deserialization process is symmetric and uses Decoder.
(common, js, jvm, native)
descriptor |
Describes the structure of the serializable representation of T, produced by this serializer. Knowing the structure of the descriptor is required to determine the shape of the serialized form (e.g. what elements are encoded as lists and what as primitives) along with its metadata such as alternative names. abstract val descriptor: SerialDescriptor |
(common, js, jvm, native)
patch |
open fun patch(decoder: Decoder, old: T): T |
(common, js, jvm, native)
ContextSerializer |
This class provides support for retrieving a serializer in runtime, instead of using the one precompiled by the serialization plugin. This serializer is enabled by ContextualSerialization. class ContextSerializer<T : Any> : KSerializer<T> |
(common, js, jvm, native)
JsonArraySerializer |
External Serializer object providing SerializationStrategy and DeserializationStrategy for JsonArray. It can only be used by with Json format an its input (JsonInput and JsonOutput). object JsonArraySerializer : KSerializer<JsonArray> |
(common, js, jvm, native)
JsonElementSerializer |
External Serializer object providing SerializationStrategy and DeserializationStrategy for JsonElement. It can only be used by with Json format an its input (JsonInput and JsonOutput). Currently, this hierarchy has no guarantees on descriptor content. object JsonElementSerializer : KSerializer<JsonElement> |
(common, js, jvm, native)
JsonLiteralSerializer |
External Serializer object providing SerializationStrategy and DeserializationStrategy for JsonLiteral. It can only be used by with Json format an its input (JsonInput and JsonOutput). object JsonLiteralSerializer : KSerializer<JsonLiteral> |
(common, js, jvm, native)
JsonNullSerializer |
External Serializer object providing SerializationStrategy and DeserializationStrategy for JsonNull. It can only be used by with Json format an its input (JsonInput and JsonOutput). object JsonNullSerializer : KSerializer<JsonNull> |
(common, js, jvm, native)
JsonObjectSerializer |
External Serializer object providing SerializationStrategy and DeserializationStrategy for JsonObject. It can only be used by with Json format an its input (JsonInput and JsonOutput). object JsonObjectSerializer : KSerializer<JsonObject> |
(common, js, jvm, native)
JsonParametricSerializer |
Base class for custom serializers that allows selecting polymorphic serializer without a dedicated class discriminator, on a content basis. abstract class JsonParametricSerializer<T : Any> : KSerializer<T> |
(common, js, jvm, native)
JsonPrimitiveSerializer |
External Serializer object providing SerializationStrategy and DeserializationStrategy for JsonPrimitive. It can only be used by with Json format an its input (JsonInput and JsonOutput). object JsonPrimitiveSerializer : KSerializer<JsonPrimitive> |
(common, js, jvm, native)
JsonTransformingSerializer |
Base class for custom serializers that allows manipulating an abstract JSON representation of the class before serialization or deserialization. abstract class JsonTransformingSerializer<T : Any> : KSerializer<T> |
(common, js, jvm, native)
LongAsStringSerializer |
Serializer that encodes and decodes Long as its string representation. object LongAsStringSerializer : KSerializer<Long> |