package ru.dbotthepony.kstarbound.json

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import ru.dbotthepony.kommons.gson.consumeNull

object LongRangeAdapter : TypeAdapter<LongRange>() {
	override fun write(out: JsonWriter, value: LongRange?) {
		if (value == null)
			out.nullValue()
		else {
			out.beginArray()
			out.value(value.first)
			out.value(value.last)
			out.endArray()
		}
	}

	override fun read(`in`: JsonReader): LongRange? {
		if (`in`.consumeNull()) {
			return null
		} else {
			`in`.beginArray()
			val first = `in`.nextLong()
			val last = `in`.nextLong()
			`in`.endArray()

			return LongRange(first, last)
		}
	}
}
