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

commonMain.com.bselzer.ktx.datetime.serialization.MillisecondDurationSerializer.kt Maven / Gradle / Ivy

The newest version!
package com.bselzer.ktx.datetime.serialization

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.milliseconds
import kotlin.time.DurationUnit

/**
 * A serializer for milliseconds in the form of a [Duration]
 */
class MillisecondDurationSerializer : KSerializer {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Duration", PrimitiveKind.DOUBLE)

    override fun deserialize(decoder: Decoder): Duration = decoder.decodeDouble().milliseconds

    override fun serialize(encoder: Encoder, value: Duration) {
        encoder.encodeDouble(value.toDouble(DurationUnit.MILLISECONDS))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy