ClassDiscriminatorMode
public static class 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.
| See also | |
|---|---|
classDiscriminatorMode |
Summary
Constants |
|
|---|---|
static final int |
ALL_OBJECTS = 1Include class discriminators whenever possible. |
static final int |
POLYMORPHIC = 2Include class discriminators for polymorphic classes. |
Public fields |
|
|---|---|
static @NonNull ClassDiscriminatorMode |
Constants
ALL_OBJECTS
public static final int ALL_OBJECTS = 1
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
public static final int POLYMORPHIC = 2
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.