package ru.dbotthepony.kstarbound.util import com.google.gson.JsonArray import com.google.gson.JsonElement import com.google.gson.JsonNull import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import com.google.gson.JsonSyntaxException import com.google.gson.TypeAdapter import ru.dbotthepony.kstarbound.io.json.InternedJsonElementAdapter operator fun JsonObject.set(key: String, value: JsonElement?) { add(key, value) } operator fun JsonObject.set(key: String, value: String) { add(key, JsonPrimitive(value)) } operator fun JsonObject.set(key: String, value: Int) { add(key, JsonPrimitive(value)) } operator fun JsonObject.set(key: String, value: Long) { add(key, JsonPrimitive(value)) } operator fun JsonObject.set(key: String, value: Float) { add(key, JsonPrimitive(value)) } operator fun JsonObject.set(key: String, value: Double) { add(key, JsonPrimitive(value)) } operator fun JsonObject.set(key: String, value: Boolean) { add(key, InternedJsonElementAdapter.of(value)) } operator fun JsonObject.set(key: String, value: Nothing?) { add(key, JsonNull.INSTANCE) } operator fun JsonObject.contains(key: String): Boolean = has(key) fun JsonObject.get(key: String, default: Boolean): Boolean { if (!has(key)) return default val value = this[key] if (value !is JsonPrimitive || !value.isBoolean) { throw JsonSyntaxException("Expected boolean at $key; got $value") } return value.asBoolean } fun JsonObject.get(key: String, default: String): String { if (!has(key)) return default val value = this[key] if (value !is JsonPrimitive || !value.isString) { throw JsonSyntaxException("Expected string at $key; got $value") } return value.asString } fun JsonObject.get(key: String, default: Int): Int { if (!has(key)) return default val value = this[key] if (value !is JsonPrimitive || !value.isNumber) { throw JsonSyntaxException("Expected integer at $key; got $value") } return value.asInt } fun JsonObject.get(key: String, default: Long): Long { if (!has(key)) return default val value = this[key] if (value !is JsonPrimitive || !value.isNumber) { throw JsonSyntaxException("Expected long at $key; got $value") } return value.asLong } fun JsonObject.get(key: String, default: Double): Double { if (!has(key)) return default val value = this[key] if (value !is JsonPrimitive || !value.isNumber) { throw JsonSyntaxException("Expected double at $key; got $value") } return value.asDouble } fun JsonObject.get(key: String, default: JsonObject): JsonObject { if (!has(key)) return default val value = this[key] if (value !is JsonObject) throw JsonSyntaxException("Expected json object at $key; got $value") return value } fun JsonObject.get(key: String, default: JsonArray): JsonArray { if (!has(key)) return default val value = this[key] if (value !is JsonArray) throw JsonSyntaxException("Expected json array at $key; got $value") return value } fun JsonObject.get(key: String, default: JsonPrimitive): JsonElement { if (!has(key)) return default return this[key] } fun JsonObject.get(key: String, type: TypeAdapter): T { return get(key, type) { throw JsonSyntaxException("Expected value at $key, got nothing") } } inline fun JsonObject.get(key: String, type: TypeAdapter, orElse: () -> T): T { if (!has(key)) return orElse.invoke() return type.fromJsonTree(this[key]) } fun JsonObject.getArray(key: String): JsonArray { if (!has(key)) throw JsonSyntaxException("Expected array at $key, got nothing") return this[key] as? JsonArray ?: throw JsonSyntaxException("Expected array at $key, got ${this[key]}") } fun JsonObject.getObject(key: String): JsonObject { if (!has(key)) throw JsonSyntaxException("Expected object at $key, got nothing") return this[key] as? JsonObject ?: throw JsonSyntaxException("Expected object at $key, got ${this[key]}") }