kotlinx-serialization / kotlinx.serialization / SealedClassSerializer

SealedClassSerializer

(common, js, jvm, native) class SealedClassSerializer<T : Any> : AbstractPolymorphicSerializer<T>

This class provides support for multiplatform polymorphic serialization of sealed classes.

In contrary to PolymorphicSerializer, all known subclasses with serializers must be passed in subclasses and subSerializers constructor parameters. If a subclass is a sealed class itself, all its subclasses are registered as well.

If sealed hierarchy is marked @Serializable, an instance of this class is constructed automatically. In most of the cases, you won't need to perform any manual setup:

@Serializable
sealed class SimpleSealed {
    @Serializable
    public data class SubSealedA(val s: String) : SimpleSealed()

    @Serializable
    public data class SubSealedB(val i: Int) : SimpleSealed()
}

// will perform correct polymorphic serialization and deserialization:
Json.stringify(SimpleSealed.serializer(), SubSealedA("foo"))

However, it is possible to register additional subclasses using regular SerializersModule. It is required when one of the subclasses is an abstract class itself:

@Serializable
sealed class ProtocolWithAbstractClass {
    @Serializable
    abstract class Message : ProtocolWithAbstractClass() {
        @Serializable
        data class StringMessage(val description: String, val message: String) : Message()

        @Serializable
        data class IntMessage(val description: String, val message: Int) : Message()
    }

    @Serializable
    data class ErrorMessage(val error: String) : ProtocolWithAbstractClass()
}

In this case, ErrorMessage would be registered automatically by the plugin, but StringMessage and IntMessage require manual registration, as described in PolymorphicSerializer documentation:

val abstractContext = SerializersModule {
    polymorphic(ProtocolWithAbstractClass::class, ProtocolWithAbstractClass.Message::class) {
        subclass<ProtocolWithAbstractClass.Message.IntMessage>()
        subclass<ProtocolWithAbstractClass.Message.StringMessage>()
        // no need to register ProtocolWithAbstractClass.ErrorMessage
    }
}

Constructors

(common, js, jvm, native)

<init>

This class provides support for multiplatform polymorphic serialization of sealed classes.

<init>(serialName: String, baseClass: KClass<T>, subclasses: Array<KClass<out T>>, subclassSerializers: Array<KSerializer<out T>>)

Properties

(common, js, jvm, native)

baseClass

Base class for all classes that this polymorphic serializer can serialize or deserialize.

val baseClass: KClass<T>
(common, js, jvm, native)

descriptor

Describes the structure of the serializable representation of T, produced by this serializer. Knowing the structure of the descriptor is required to determine the shape of the serialized form (e.g. what elements are encoded as lists and what as primitives) along with its metadata such as alternative names.

val descriptor: SerialDescriptor

Functions

(common, js, jvm, native)

findPolymorphicSerializer

Lookups an actual serializer for given klassName withing the current base class. May use context from the decoder. Throws SerializationException if serializer is not found.

fun findPolymorphicSerializer(decoder: CompositeDecoder, klassName: String): DeserializationStrategy<out T>

Lookups an actual serializer for given value within the current base class. May use context from the encoder. Throws SerializationException if serializer is not found.

fun findPolymorphicSerializer(encoder: Encoder, value: T): SerializationStrategy<T>