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

commonMain.dev.inmo.micro_utils.common.IndexedValueSerializer.kt Maven / Gradle / Ivy

The newest version!
package dev.inmo.micro_utils.common

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

class IndexedValueSerializer(private val subSerializer: KSerializer) : KSerializer> {
    private val originalSerializer = PairSerializer(Int.serializer(), subSerializer)
    override val descriptor: SerialDescriptor
        get() = originalSerializer.descriptor

    override fun deserialize(decoder: Decoder): IndexedValue {
        val pair = originalSerializer.deserialize(decoder)
        return IndexedValue(
            pair.first,
            pair.second
        )
    }

    override fun serialize(encoder: Encoder, value: IndexedValue) {
        originalSerializer.serialize(
            encoder,
            Pair(value.index, value.value)
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy