commonMain.com.bselzer.ktx.value.enumeration.StringEnum.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.value.enumeration
import com.bselzer.ktx.serialization.context.JsonContext.Default.decode
import com.bselzer.ktx.serialization.context.JsonContext.Default.decodeOrNull
import com.bselzer.ktx.serialization.serializer.StringEnumSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
@Serializable(with = StringEnumSerializer::class)
class StringEnum @PublishedApi internal constructor(
private val value: String,
private val serializer: KSerializer
) : BindableEnum where T : Enum {
override fun toEnum(): T = value.decode(serializer)
override fun toEnumOrNull(): T? = value.decodeOrNull(serializer)
override fun toString(): String = value
}