codecs.extended.LongRangeJSONCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-json-coding Show documentation
Show all versions of fluid-json-coding Show documentation
A JSON library written in pure Kotlin (coding extension)
package com.github.fluidsonic.fluid.json
object LongRangeJSONCodec : AbstractJSONCodec() {
override fun JSONDecoder.decode(valueType: JSONCodingType): LongRange {
var endInclusive = 0L
var endInclusiveProvided = false
var start = 0L
var startProvided = false
readFromMapByElementValue { key ->
when (key) {
Fields.endInclusive -> {
endInclusive = readLong()
endInclusiveProvided = true
}
Fields.start -> {
start = readLong()
startProvided = true
}
else -> skipValue()
}
}
if (!startProvided) missingPropertyError(Fields.start)
if (!endInclusiveProvided) missingPropertyError(Fields.endInclusive)
return start .. endInclusive
}
override fun JSONEncoder.encode(value: LongRange) {
writeIntoMap {
writeMapElement(Fields.start, long = value.first)
writeMapElement(Fields.endInclusive, long = value.last)
}
}
private object Fields {
const val endInclusive = "endInclusive"
const val start = "start"
}
}