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

codecs.extended.CharRangeJSONCodec.kt Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
package com.github.fluidsonic.fluid.json


object CharRangeJSONCodec : AbstractJSONCodec() {

	override fun JSONDecoder.decode(valueType: JSONCodingType): CharRange {
		var endInclusive = 0.toChar()
		var endInclusiveProvided = false
		var start = 0.toChar()
		var startProvided = false

		readFromMapByElementValue { key ->
			when (key) {
				Fields.endInclusive -> {
					endInclusive = readChar()
					endInclusiveProvided = true
				}
				Fields.start -> {
					start = readChar()
					startProvided = true
				}
				else -> skipValue()
			}
		}

		if (!startProvided) missingPropertyError(Fields.start)
		if (!endInclusiveProvided) missingPropertyError(Fields.endInclusive)

		return start .. endInclusive
	}


	override fun JSONEncoder.encode(value: CharRange) {
		writeIntoMap {
			writeMapElement(Fields.start, char = value.first)
			writeMapElement(Fields.endInclusive, char = value.last)
		}
	}


	private object Fields {

		const val endInclusive = "endInclusive"
		const val start = "start"
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy