commonMain.com.bselzer.ktx.serialization.serializer.LenientDurationSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datetime-serialization Show documentation
Show all versions of datetime-serialization Show documentation
kotlinx.serialization for kotlinx.datetime
The newest version!
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
/**
* A serializer for [Duration] with lenient deserialization using the [Duration.parse] method.
*/
class LenientDurationSerializer : KSerializer {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(LenientDurationSerializer::class.qualifiedName!!, PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Duration = Duration.parse(decoder.decodeString())
override fun serialize(encoder: Encoder, value: Duration) {
encoder.encodeString(value.toIsoString())
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy