Package kotlinx.serialization.descriptors

Basic concepts of serial description to programmatically describe the serial form for serializers in an introspectable manner.

Types

ClassSerialDescriptorBuilder
Link copied to clipboard
class ClassSerialDescriptorBuilder

Builder for SerialDescriptor for user-defined serializers.

PolymorphicKind
Link copied to clipboard
sealed class PolymorphicKind : SerialKind

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.

PrimitiveKind
Link copied to clipboard
sealed class PrimitiveKind : SerialKind

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

SerialDescriptor
Link copied to clipboard
interface 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.

SerialKind
Link copied to clipboard
sealed class SerialKind

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

StructureKind
Link copied to clipboard
sealed class StructureKind : SerialKind

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

Functions

buildClassSerialDescriptor
Link copied to clipboard
fun buildClassSerialDescriptor(serialName: String, vararg typeParameters: SerialDescriptor, builderAction: ClassSerialDescriptorBuilder.() -> Unit = {}): SerialDescriptor

Builder for SerialDescriptor. The resulting descriptor will be uniquely identified by the given serialName, typeParameters and elements structure described in builderAction function.

buildSerialDescriptor
Link copied to clipboard
fun buildSerialDescriptor(serialName: String, kind: SerialKind, vararg typeParameters: SerialDescriptor, builder: ClassSerialDescriptorBuilder.() -> Unit = {}): SerialDescriptor

An unsafe alternative to buildClassSerialDescriptor that supports an arbitrary SerialKind. This function is left public only for migration of pre-release users and is not intended to be used as generally-safe and stable mechanism. Beware that it can produce inconsistent or non spec-compliant instances.

element
Link copied to clipboard
inline fun <T> ClassSerialDescriptorBuilder.element(elementName: String, annotations: List<Annotation> = emptyList(), isOptional: Boolean = false)

A reified version of element function that extract descriptor using serializer<T>().descriptor call with all the restrictions of serializer<T>().descriptor.

getContextualDescriptor
Link copied to clipboard
fun SerializersModule.getContextualDescriptor(descriptor: SerialDescriptor): SerialDescriptor?

Looks up a descriptor of serializer registered for contextual serialization in this, using SerialDescriptor.capturedKClass as a key.

getPolymorphicDescriptors
Link copied to clipboard

Retrieves a collection of descriptors which serializers are registered for polymorphic serialization in this with base class equal to descriptor's SerialDescriptor.capturedKClass. This method does not retrieve serializers registered with PolymorphicModuleBuilder.default.

listSerialDescriptor
Link copied to clipboard
inline fun <T> listSerialDescriptor(): SerialDescriptor

Creates a descriptor for the type List<T>.

fun listSerialDescriptor(elementDescriptor: SerialDescriptor): SerialDescriptor

Creates a descriptor for the type List<T> where T is the type associated with elementDescriptor.

mapSerialDescriptor
Link copied to clipboard
inline fun <K, V> mapSerialDescriptor(): SerialDescriptor

Creates a descriptor for the type Map<K, V>.

fun mapSerialDescriptor(keyDescriptor: SerialDescriptor, valueDescriptor: SerialDescriptor): SerialDescriptor

Creates a descriptor for the type Map<K, V> where K and V are types associated with keyDescriptor and valueDescriptor respectively.

PrimitiveSerialDescriptor
Link copied to clipboard
fun PrimitiveSerialDescriptor(serialName: String, kind: PrimitiveKind): SerialDescriptor

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:

SerialDescriptor
Link copied to clipboard
inline fun <T> serialDescriptor(): SerialDescriptor

Retrieves descriptor of type T using reified serializer function.

fun serialDescriptor(type: KType): SerialDescriptor

Retrieves descriptor of type associated with the given KType

setSerialDescriptor
Link copied to clipboard
inline fun <T> setSerialDescriptor(): SerialDescriptor

Creates a descriptor for the type Set<T>.

fun setSerialDescriptor(elementDescriptor: SerialDescriptor): SerialDescriptor

Creates a descriptor for the type Set<T> where T is the type associated with elementDescriptor.

Properties

capturedKClass
Link copied to clipboard
val SerialDescriptor.capturedKClass: KClass<*>?

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

elementDescriptors
Link copied to clipboard

Returns an iterable of all descriptor elements.

elementNames
Link copied to clipboard
val SerialDescriptor.elementNames: Iterable<String>

Returns an iterable of all descriptor element names.

nullable
Link copied to clipboard
val SerialDescriptor.nullable: SerialDescriptor

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