commonMain.serialization.FlakyIntSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qbittorrent-models Show documentation
Show all versions of qbittorrent-models Show documentation
Kotlin wrapper for the qBittorrent Web API using Ktor.
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