commonMain.com.bselzer.ktx.serialization.serializer.StringEnumSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of value-enumeration-jvm Show documentation
Show all versions of value-enumeration-jvm Show documentation
Value class wrappers for enumerations.
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())
}
}