ClassDiscriminatorMode



Defines which classes and objects should have their serial name included in the json as so-called class discriminator.

Class discriminator is a androidx.savedstate.SavedState field added by kotlinx.serialization with a key (type by default), and class' 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. This behavior can be changed to match various JSON schemas.

Summary

Constants

const Int

Include class discriminators whenever possible.

Cmn
const Int

Include class discriminators for polymorphic classes.

Cmn

Constants

ALL_OBJECTS

const val ALL_OBJECTS = 1: Int

Include 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

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