commonMain.io.ks3.standard.StringSerializer.kt Maven / Gradle / Ivy
package io.ks3.standard
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
inline fun stringSerializer(
crossinline decode: (String) -> T,
crossinline encode: (T) -> String = { it.toString() },
nameOverride: String? = null,
): KSerializer =
object : KSerializer {
override val descriptor = PrimitiveSerialDescriptor(nameOverride ?: T::class.simpleName!!, PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder) = decode(decoder.decodeString())
override fun serialize(
encoder: Encoder,
value: T,
) = encoder.encodeString(encode(value))
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy