ALL_JSON_OBJECTS

Include class discriminators whenever possible.

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

This mode is generally intended to produce JSON for consumption by third-party libraries. Given that JsonBuilder.classDiscriminatorMode does not affect deserialization, kotlinx.serialization does not expect every object to have discriminator, which may trigger deserialization errors. If you experience such problems, refrain from using ALL_JSON_OBJECTS or use JsonBuilder.ignoreUnknownKeys.

In the example:

@Serializable class Plain(val p: String)
@Serializable sealed class Base
@Serializable object Impl: Base()

@Serializable class All(val p: Plain, val b: Base, val i: Impl)

setting JsonBuilder.classDiscriminatorMode to ClassDiscriminatorMode.ALL_JSON_OBJECTS adds class discriminator to All.p, All.b, All.i, and to All object itself.

Properties

Link copied to clipboard
expect val name: String
Link copied to clipboard
expect val ordinal: Int