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

commonMain.serialization.FlakyIntSerializer.kt Maven / Gradle / Ivy

The newest version!
package qbittorrent.models.serialization

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

internal class FlakyIntSerializer : KSerializer {
    override val descriptor: SerialDescriptor =
        PrimitiveSerialDescriptor("FlakyInt", PrimitiveKind.INT)

    override fun deserialize(decoder: Decoder): Int {
        return try {
            decoder.decodeInt()
        } catch (e: SerializationException) {
            decoder.decodeString().toIntOrNull() ?: -1
        }
    }

    override fun serialize(encoder: Encoder, value: Int) {
        encoder.encodeInt(value)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy