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

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

There is a newer version: 1.8.0
Show newest version
/*
 * 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
import kotlin.uuid.*


@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")
    }
}

@PublishedApi
@ExperimentalUuidApi
internal object UuidSerializer: KSerializer {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlin.uuid.Uuid", PrimitiveKind.STRING)

    override fun serialize(encoder: Encoder, value: Uuid) {
        encoder.encodeString(value.toString())
    }

    override fun deserialize(decoder: Decoder): Uuid {
        return Uuid.parse(decoder.decodeString())
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy