kotlinx-serialization / kotlinx.serialization

Package kotlinx.serialization

Basic core concepts and annotations that set up serialization process.

Types

(common, js, jvm, native)

BinaryFormat

SerialFormat that allows conversion to and from ByteArray via dump and load methods

interface BinaryFormat : SerialFormat
(common, js, jvm, native)

CompositeDecoder

CompositeDecoder is a part of decoding process that is bound to a particular structured part of the serialized form, described by the serial descriptor passed to Decoder.beginStructure.

interface CompositeDecoder
(common, js, jvm, native)

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.

interface CompositeEncoder
(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)

Decoder

Decoder is a core deserialization primitive that encapsulates the knowledge of the underlying format and an underlying storage, exposing only structural methods to the deserializer, making it completely format-agnostic. Deserialization process takes a decoder and asks him for a sequence of primitive elements, defined by a deserializer serial form, while decoder knows how to retrieve these primitive elements from an actual format representations.

interface Decoder
(common, js, jvm, native)

DeserializationStrategy

Deserialization strategy defines the serial form of a type T, including its structural description, declared by the descriptor and the actual deserialization process, defined by the implementation of the deserialize method.

interface DeserializationStrategy<T>
(js)

DynamicObjectParser

Converts native JavaScript objects into Kotlin ones, verifying their types.

class DynamicObjectParser : SerialFormat
(common, js, jvm, native)

Encoder

Encoder is a core serialization primitive that encapsulates the knowledge of the underlying format and its storage, exposing only structural methods to the serializer, making it completely format-agnostic. Serialization process transforms a single value into the sequence of its primitive elements, also called its serial form, while encoding transforms these primitive elements into an actual format representation: JSON string, ProtoBuf ByteArray, in-memory map representation etc.

interface Encoder
(common, js, jvm, native)

KSerializer

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.

interface KSerializer<T> : SerializationStrategy<T>, DeserializationStrategy<T>
(common, js, jvm, native)

PolymorphicKind

Polymorphic kind represents a (bounded) polymorphic value, that is referred by some base class or interface, but its structure is defined by one of the possible implementations. Polymorphic kind is, by its definition, a union kind and is extracted to its own subtype to emphasize bounded and sealed polymorphism common property: not knowing the actual type statically and requiring formats to additionally encode it.

sealed class PolymorphicKind : SerialKind
(common, js, jvm, native)

PolymorphicSerializer

This class provides support for multiplatform polymorphic serialization for interfaces and abstract classes.

class PolymorphicSerializer<T : Any> : AbstractPolymorphicSerializer<T>
(common, js, jvm, native)

PrimitiveKind

Values of primitive kinds usually are represented as a single value. All default serializers for Kotlin primitives types and String have primitive kind.

sealed class PrimitiveKind : SerialKind
(common, js, jvm, native)

Properties

Transforms a Serializable class' properties into a single flat Map which consists of string keys and primitive type values, and vice versa. Located in separated kotlinx-serialization-properties artifact.

class Properties : SerialFormat
(common, js, jvm, native)

SealedClassSerializer

This class provides support for multiplatform polymorphic serialization of sealed classes.

class SealedClassSerializer<T : Any> : AbstractPolymorphicSerializer<T>
(common, js, jvm, native)

SerialDescriptor

Serial descriptor is an inherent property of KSerializer that describes the structure of the serializable type. The structure of the serializable type is not only the property of the type, but also of the serializer as well, meaning that one type can have multiple descriptors that have completely different structure.

interface SerialDescriptor
(common, js, jvm, native)

SerialDescriptorBuilder

Builder for SerialDescriptor to be used in custom serializers.

class SerialDescriptorBuilder
(common, js, jvm, native)

SerialFormat

Represents an instance of a serialization format that can interact with KSerializer and is a supertype of all entry points for a serialization. It does not impose any restrictions on a serialized form or underlying storage, neither it exposes them. Concrete data types and API for user-interaction are responsibility of a concrete subclass or subinterface, e.g. StringFormat, BinaryFormat or Json.

interface SerialFormat
(common, js, jvm, native)

SerializationStrategy

Serialization strategy defines the serial form of a type T, including its structural description, declared by the descriptor and the actual serialization process, defined by the implementation of the serialize method.

interface SerializationStrategy<in T>
(common, js, jvm, native)

SerialKind

Serial kind is an intrinsic property of SerialDescriptor that indicates how the corresponding type is structurally represented by its serializer.

sealed class SerialKind
(common, js, jvm, native)

StringFormat

SerialFormat that allows conversion to and from String via stringify and parse methods

interface StringFormat : SerialFormat
(common, js, jvm, native)

StructureKind

Structure kind represents values with composite structure of nested elements of depth and arbitrary number. We acknowledge following structured kinds:

sealed class StructureKind : SerialKind
(common, js, jvm, native)

UnionKind

Union structure kind represents a tagged union structure, meaning that the type is represent by one of a multiple possible values (potentially unknown). An example of such union kind can be enum or its derivatives, such as "one of known strings".

sealed class UnionKind : SerialKind
(common, js, jvm, native)

UpdateMode

enum class UpdateMode

Annotations

(common, js, jvm, native)

ContextualSerialization

Instructs to use ContextSerializer on an annotated property or type usage. If used on a file, instructs to use ContextSerializer for all listed KClasses.

annotation class ContextualSerialization
(common, js, jvm, native)

ImplicitReflectionSerializer

This annotation marks declaration which try to obtain serializer implicitly using reflection, e.g. from KClass or instance itself.

annotation class ImplicitReflectionSerializer
(common, js, jvm, native)

InternalSerializationApi

Public API marked with this annotation is effectively internal, which means it should not be used outside of kotlinx.serialization. Signature, semantics, source and binary compatibilities are not guaranteed for this API and will be changed without any warnings or migration aids. If you cannot avoid using internal API to solve your problem, please report your use-case to serialization's issue tracker.

annotation class InternalSerializationApi
(common, js, jvm, native)

Polymorphic

Instructs to use PolymorphicSerializer on an annotated property or type usage. When used on class, replaces its serializer with PolymorphicSerializer everywhere.

annotation class Polymorphic
(common, js, jvm, native)

Required

Indicates that property must be present during deserialization process, even if it has default value.

annotation class Required
(common, js, jvm, native)

SerialInfo

When annotation class is marked with @SerialInfo, compiler plugin can instantiate it and put into SerialDescriptor, to be retrieved later during serialization process.

annotation class SerialInfo
(common, js, jvm, native)

Serializable

Sets specific serializer for class, property or type argument. When argument is omitted, plugin will generate default implementation inside the class.

annotation class Serializable
(common, js, jvm, native)

Serializer

Instructs plugin to turn this class into serializer for specified class forClass. However, it would not be used automatically. To apply it on particular class or property, use Serializable or UseSerializers, or ContextualSerialization with runtime registration.

annotation class Serializer
(common, js, jvm, native)

SerialName

Overrides name visible to the runtime part of serialization framework

annotation class SerialName
(common, js, jvm, native)

Transient

Marks this property invisible for whole serialization framework. Transient properties must have default values.

annotation class Transient
(common, js, jvm, native)

UnstableDefault

This annotation marks declarations with default parameters that are subject to semantic change without a migration path.

annotation class UnstableDefault
(common, js, jvm, native)

UseSerializers

Adds serializerClasses to serializers resolving process inside the plugin. Each of serializerClasses must implement KSerializer.

annotation class UseSerializers

Exceptions

(common, js, jvm, native)

MissingFieldException

Thrown when KSerializer hasn't received property from Decoder, and this property was not optional.

class MissingFieldException : SerializationException

SerializationException

A generic exception indicating the problem during serialization or deserialization process

(common, js, native) open class SerializationException : RuntimeException
(jvm) open class SerializationException : RuntimeException
(common, js, jvm, native)

UnknownFieldException

Thrown when KSerializer received unknown property index from CompositeDecoder.decodeElementIndex.

class UnknownFieldException : SerializationException
(common, js, jvm, native)

UpdateNotSupportedException

class UpdateNotSupportedException : SerializationException

Extensions for External Classes

(common, js, jvm, native)

kotlin.reflect.KClass

Properties

(common, js, jvm, native)

capturedKClass

Retrieves KClass associated with serializer and its descriptor, if it was captured.

val SerialDescriptor.capturedKClass: KClass<*>?
(common, js, jvm, native)

nullable

Returns new serial descriptor for the same type with isNullable property set to true.

val SerialDescriptor.nullable: SerialDescriptor

Functions

(common, js, jvm, native)

decode

Alias for Decoder.decodeSerializableValue

fun <T> Decoder.decode(deserializer: DeserializationStrategy<T>): T

typeOf-based version of Decoder.decodeSerializableValue

fun <T : Any> Decoder.decode(): T
(common, js, jvm, native)

decodeStructure

Begins a structure, decodes it using the given block, ends it and returns decoded element.

fun <T> Decoder.decodeStructure(descriptor: SerialDescriptor, block: CompositeDecoder.() -> T): T
(common, js, jvm, native)

dump

fun <T : Any> BinaryFormat.dump(value: T): ByteArray
(common, js, jvm, native)

dumps

Serializes value to ByteArray using given serializer and then represents bytes in a human-readable form.

fun <T> BinaryFormat.dumps(serializer: SerializationStrategy<T>, value: T): Stringfun <T : Any> BinaryFormat.dumps(value: T): String
(common, js, jvm, native)

elementDescriptors

Creates a List out of a child descriptors retrieved via SerialDescriptor.getElementDescriptor.

fun SerialDescriptor.elementDescriptors(): List<SerialDescriptor>
(common, js, jvm, native)

elementNames

Returns a List out of all serial names of serial descriptor elements

fun SerialDescriptor.elementNames(): List<String>
(common, js, jvm, native)

encode

Alias for Encoder.encodeSerializableValue

fun <T> Encoder.encode(strategy: SerializationStrategy<T>, value: T): Unit

typeOf-based version of Encoder.encodeSerializableValue

fun <T : Any> Encoder.encode(obj: T): Unit
(common, js, jvm, native)

encodeStructure

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

fun Encoder.encodeStructure(descriptor: SerialDescriptor, block: CompositeEncoder.() -> Unit): Unit
(common, js, jvm, native)

getElementIndexOrThrow

Same as SerialDescriptor.getElementIndex, but throws SerializationException if given name is not associated with any element in the descriptor.

fun SerialDescriptor.getElementIndexOrThrow(name: String): Int
(common, js, jvm, native)

load

fun <T : Any> BinaryFormat.load(raw: ByteArray): T
(common, js, jvm, native)

loads

Parses hex string as a byte array and then deserializes it to an object of type T using given deserializer.

fun <T> BinaryFormat.loads(deserializer: DeserializationStrategy<T>, hex: String): Tfun <T : Any> BinaryFormat.loads(hex: String): T
(common, js, jvm, native)

parse

fun <T : Any> StringFormat.parse(str: String): T
(common, js, jvm, native)

parseList

fun <T : Any> StringFormat.parseList(objects: String): List<T>
(common, js, jvm, native)

parseMap

fun <K : Any, V : Any> StringFormat.parseMap(map: String): Map<K, V>
(common, js, jvm, native)

PrimitiveDescriptor

Factory to create a trivial primitive descriptors. Primitive descriptors should be used when the serialized form of the data has a primitive form, for example:

fun PrimitiveDescriptor(serialName: String, kind: PrimitiveKind): SerialDescriptor
(common, js, jvm, native)

SerialDescriptor

Builder for SerialDescriptor. The resulting descriptor will be uniquely identified by the given serialName, with the corresponding kind and structure described in builder function.

fun SerialDescriptor(serialName: String, kind: SerialKind = StructureKind.CLASS, builder: SerialDescriptorBuilder.() -> Unit = {}): SerialDescriptor
(common, js, jvm, native)

serializer

Creates a serializer for the provided reified type T with support of user-defined generic classes. This method is a reified version of serializer(KType)

fun <T> serializer(): KSerializer<T>

Creates a serializer for the given type with support of user-defined generic classes. type argument can be obtained with experimental typeOf method.

fun serializer(type: KType): KSerializer<Any?>
(jvm)

serializerByTypeToken

Constructs a serializer for the given reflective Java type. serializerByTypeToken is intended to be used as an interoperability layer for libraries like GSON and Retrofit, that operate with reflective Java Type and cannot use typeOf.

fun serializerByTypeToken(type: Type): KSerializer<Any>
(common, js, jvm, native)

stringify

fun <T : Any> StringFormat.stringify(value: T): String
fun <T : Any> StringFormat.stringify(objects: List<T>): String
fun <K : Any, V : Any> StringFormat.stringify(map: Map<K, V>): String