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

commonMain.nation.LooseCurrencySerializer.kt Maven / Gradle / Ivy

There is a newer version: 3.0.13
Show newest version
package nation

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

object LooseCurrencySerializer : KSerializer {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("nation.Currency", PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: Currency) = encoder.encodeString(value.name)
    override fun deserialize(decoder: Decoder): Currency {
        val value = decoder.decodeString()
        val currencies = Currency.entries
        return currencies.firstOrNull {
            it.name == value
        } ?: currencies.firstOrNull {
            it.globalSymbol == value
        } ?: currencies.firstOrNull {
            it.localSymbol == value
        } ?: Currency.UXX
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy