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

commonMain.kotlinx.serialization.internal.BuiltInSerializers.kt Maven / Gradle / Ivy

/*
 * Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */
package kotlinx.serialization.internal

import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.time.Duration


@PublishedApi
internal object DurationSerializer : KSerializer {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlin.time.Duration", PrimitiveKind.STRING)

    override fun serialize(encoder: Encoder, value: Duration) {
        encoder.encodeString(value.toIsoString())
    }

    override fun deserialize(decoder: Decoder): Duration {
        return Duration.parseIsoString(decoder.decodeString())
    }
}

@PublishedApi
internal object NothingSerializer : KSerializer {
    override val descriptor: SerialDescriptor = NothingSerialDescriptor

    override fun serialize(encoder: Encoder, value: Nothing) {
        throw SerializationException("'kotlin.Nothing' cannot be serialized")
    }

    override fun deserialize(decoder: Decoder): Nothing {
        throw SerializationException("'kotlin.Nothing' does not have instances")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy