commonMain.LongRange.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protocol Show documentation
Show all versions of protocol Show documentation
A lyrics finder written in Kotlin
package dev.schlaubi.lyrics.protocol
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.*
import kotlin.properties.Delegates
/**
* Serializable version of a [LongRange].
*/
public typealias SerializableLongRange = @Serializable(with = LongRangeSerializer::class) LongRange
/**
* Serializer for [SerializableLongRange].
*/
public object LongRangeSerializer : KSerializer {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("dev.schlaubi.lyrics.protocol.LongRange") {
element("start")
element("end")
}
override fun deserialize(decoder: Decoder): LongRange {
var start by Delegates.notNull()
var end by Delegates.notNull()
decoder.decodeStructure(descriptor) {
while (true) {
when (decodeElementIndex(descriptor)) {
0 -> start = decodeLongElement(descriptor, 0)
1 -> end = decodeLongElement(descriptor, 1)
CompositeDecoder.DECODE_DONE -> break
}
}
}
return start..end
}
override fun serialize(encoder: Encoder, value: LongRange): Unit = encoder.encodeStructure(descriptor) {
encodeLongElement(descriptor, 0, value.first)
encodeLongElement(descriptor, 1, value.last)
}
}