commonMain.com.bselzer.ktx.serialization.serializer.SecondDurationSerializer.kt Maven / Gradle / Ivy
package com.bselzer.ktx.serialization.serializer
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
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
/**
* A serializer for seconds in the form of a [Duration]
*/
class SecondDurationSerializer : KSerializer {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(SecondDurationSerializer::class.qualifiedName!!, PrimitiveKind.DOUBLE)
override fun deserialize(decoder: Decoder): Duration = decoder.decodeDouble().seconds
override fun serialize(encoder: Encoder, value: Duration) {
encoder.encodeDouble(value.toDouble(DurationUnit.SECONDS))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy