commonMain.dk.cachet.carp.common.infrastructure.serialization.PolymorphicEnumSerializer.kt Maven / Gradle / Ivy
Go to download
Helper classes and base types relied upon by all subsystems. This library does not contain any domain logic.
The newest version!
package dk.cachet.carp.common.infrastructure.serialization
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
/**
* A serializer which supports registering [Enum]s as subclasses in polymorphic serialization when class discriminators are used.
* When class discriminators are used, an enum is not encoded as a structure which the class discriminator can be added to.
* An exception is thrown when initializing Json: "Serializer for of kind ENUM cannot be serialized polymorphically with class discriminator."
* This serializer encodes the enum as a structure with a single `value` holding the enum value.
*
* Use this serializer to register the enum in the serializers module, e.g.:
* `subclass( ::class, PolymorphicEnumSerializer( .serializer() )`
*/
@OptIn( ExperimentalSerializationApi::class )
class PolymorphicEnumSerializer>( private val enumSerializer: KSerializer ) : KSerializer
{
override val descriptor: SerialDescriptor = buildClassSerialDescriptor( enumSerializer.descriptor.serialName )
{
element( "value", enumSerializer.descriptor )
}
override fun deserialize( decoder: Decoder ): T =
decoder.decodeStructure( descriptor )
{
decodeElementIndex( descriptor )
decodeSerializableElement( descriptor, 0, enumSerializer )
}
override fun serialize( encoder: Encoder, value: T ) =
encoder.encodeStructure( descriptor )
{
encodeSerializableElement( descriptor, 0, enumSerializer, value )
}
}