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

commonMain.com.inkapplications.glassconsole.structures.SecondsDurationSerializer.kt Maven / Gradle / Ivy

package com.inkapplications.glassconsole.structures

import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
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

/**
 * Serializes a duration as a whole number of seconds.
 */
internal class SecondsDurationSerializer: KSerializer {
    private val delegate = Long.serializer()
    override val descriptor: SerialDescriptor = delegate.descriptor

    override fun deserialize(decoder: Decoder): Duration {
        return decoder.decodeSerializableValue(delegate).seconds
    }

    override fun serialize(encoder: Encoder, value: Duration) {
        encoder.encodeSerializableValue(delegate, value.inWholeSeconds)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy