ClassDiscriminatorMode



Defines which classes and objects should have their serial name included in the SavedState as a class discriminator.

The class discriminator is a SavedState field added by kotlinx.serialization with a key (type by default), and the class's serial name as a value (fully qualified name by default).

Class discriminator is important for serializing and deserializing polymorphic class hierarchies. Default ClassDiscriminatorMode.POLYMORPHIC mode adds discriminator only to polymorphic classes.

Summary

Constants

const Int

Includes class discriminators whenever possible.

Cmn
const Int

Includes class discriminators for polymorphic classes.

Cmn

Constants

ALL_OBJECTS

const val ALL_OBJECTS = 1: Int

Includes class discriminators whenever possible.

Given that class discriminator is added as a SavedState field, adding class discriminator is possible when the resulting SavedState is an object — i.e., for Kotlin classes, objects, and interfaces. More specifically, discriminator is added to the output of serializers which descriptors have a kotlinx.serialization.descriptors.SerialKind of either kotlinx.serialization.descriptors.StructureKind.CLASS or kotlinx.serialization.descriptors.StructureKind.OBJECT.

POLYMORPHIC

const val POLYMORPHIC = 2: Int

Includes class discriminators for polymorphic classes.

Sealed classes, abstract classes, and interfaces are polymorphic classes by definition. Open classes can be polymorphic if they are serializable with kotlinx.serialization.PolymorphicSerializer and properly registered in the kotlinx.serialization.modules.SerializersModule. See kotlinx.serialization polymorphism guide for details.