All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.bselzer.ktx.serialization.serializer.StringEnumSerializer.kt Maven / Gradle / Ivy

The newest version!
package com.bselzer.ktx.serialization.serializer

import com.bselzer.ktx.value.enumeration.StringEnum
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

class StringEnumSerializer(private val enumSerializer: KSerializer) : KSerializer> where T : Enum {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(StringEnum::class.qualifiedName!!, PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): StringEnum = StringEnum(decoder.decodeString(), enumSerializer)

    override fun serialize(encoder: Encoder, value: StringEnum) {
        encoder.encodeString(value.toString())
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy