Last active
October 20, 2024 12:22
-
-
Save bitsycore/fa0b0c27bc0e0af04acac7e38c3ca6e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================= | |
// MARK: JSON-PRIMITIVE | |
// ================================= | |
// ----------------- | |
// Get | |
fun JsonPrimitive.get() : Any { | |
if(isString) return content | |
booleanOrNull?.let { return it } | |
intOrNull?.let { return it } | |
longOrNull?.let { return it } | |
floatOrNull?.let { return it } | |
doubleOrNull?.let { return it } | |
return content | |
} | |
// ----------------- | |
// Require | |
private inline fun errorPrimitive(type: String): Nothing = error("This primitive is not a $type") | |
fun JsonPrimitive.requireString() = if(isString) content else errorPrimitive("string") | |
fun JsonPrimitive.requireBoolean() = booleanOrNull ?: errorPrimitive("boolean") | |
fun JsonPrimitive.requireInt() = intOrNull ?: errorPrimitive("int") | |
fun JsonPrimitive.requireFloat() = floatOrNull ?: errorPrimitive("float") | |
fun JsonPrimitive.requireDouble() = doubleOrNull ?: errorPrimitive("double") | |
fun JsonPrimitive.requireLong() = longOrNull ?: errorPrimitive("long") | |
// ----------------- | |
// Opt | |
fun JsonPrimitive.optString(opt : String = "") = if(isString) content else opt | |
fun JsonPrimitive.optBoolean(opt : Boolean = false) = booleanOrNull ?: opt | |
fun JsonPrimitive.optInt(opt : Int = -1) = intOrNull ?: opt | |
fun JsonPrimitive.optFloat(opt : Float = -1f) = floatOrNull ?: opt | |
fun JsonPrimitive.optDouble(opt : Double = -1.0) = doubleOrNull ?: opt | |
fun JsonPrimitive.optLong(opt : Long = -1L) = longOrNull ?: opt | |
// ================================= | |
// MARK: JSON-OBJECT | |
// ================================= | |
private inline fun JsonObject.jsonPrimitiveOrNull(key: String) : JsonPrimitive? = try { | |
this[key] as JsonPrimitive? | |
} catch (_: Exception) { | |
null | |
} | |
// ----------------- | |
// Get | |
fun JsonObject.getOrNull(key: String): Any? = try { | |
when (val element = this[key]) { | |
is JsonPrimitive -> element.get() | |
is JsonArray -> element | |
is JsonObject -> element | |
else -> null | |
} | |
} catch (_: Exception) { | |
null | |
} | |
fun JsonObject.getStringOrNull(key: String) : String? = jsonPrimitiveOrNull(key)?.contentOrNull | |
fun JsonObject.getBooleanOrNull(key: String) : Boolean? = jsonPrimitiveOrNull(key)?.booleanOrNull | |
fun JsonObject.getIntOrNull(key: String) : Int? = jsonPrimitiveOrNull(key)?.intOrNull | |
fun JsonObject.getFloatOrNull(key: String) : Float? = jsonPrimitiveOrNull(key)?.floatOrNull | |
fun JsonObject.getDoubleOrNull(key: String): Double? = jsonPrimitiveOrNull(key)?.doubleOrNull | |
fun JsonObject.getLongOrNull(key: String) : Long? = jsonPrimitiveOrNull(key)?.longOrNull | |
fun JsonObject.getJsonObjectOrNull(key: String) : JsonObject? = try { | |
this[key] as JsonObject? | |
} catch (_: Exception) { | |
null | |
} | |
fun JsonObject.getJsonArrayOrNull(key: String) : JsonArray? = try { | |
this[key] as JsonArray? | |
} catch (_: Exception) { | |
null | |
} | |
// ----------------- | |
// Require | |
private inline fun errorObject(key: String, type: String): Nothing = error("JsonObject[$key] is not a $type") | |
fun JsonObject.require(key: String) = getOrNull(key) ?: error("JsonObject have no key \"$key\"") | |
fun JsonObject.requireString(key: String) = getStringOrNull(key) ?: errorObject(key, "string") | |
fun JsonObject.requireBoolean(key: String) = getBooleanOrNull(key) ?: errorObject(key, "boolean") | |
fun JsonObject.requireInt(key: String) = getIntOrNull(key) ?: errorObject(key, "int") | |
fun JsonObject.requireFloat(key: String) = getFloatOrNull(key) ?: errorObject(key, "float") | |
fun JsonObject.requireDouble(key: String) = getDoubleOrNull(key) ?: errorObject(key, "double") | |
fun JsonObject.requireLong(key: String) = getLongOrNull(key) ?: errorObject(key, "long") | |
fun JsonObject.requireJsonObject(key: String) = getJsonObjectOrNull(key) ?: errorObject(key, "JsonObject") | |
fun JsonObject.requireJsonArray(key: String) = getJsonArrayOrNull(key) ?: errorObject(key, "JsonArray") | |
// ----------------- | |
// Opt | |
fun JsonObject.opt(key: String, opt : Any) : Any = getOrNull(key) ?: opt | |
fun JsonObject.optString(key: String, opt : String = "") = getStringOrNull(key) ?: opt | |
fun JsonObject.optBoolean(key: String, opt : Boolean = false) = getBooleanOrNull(key) ?: opt | |
fun JsonObject.optInt(key: String, opt : Int = -1) = getIntOrNull(key) ?: opt | |
fun JsonObject.optFloat(key: String, opt : Float = -1f) = getFloatOrNull(key) ?: opt | |
fun JsonObject.optDouble(key: String, opt : Double = -1.0) = getDoubleOrNull(key) ?: opt | |
fun JsonObject.optLong(key: String, opt : Long = -1L) = getLongOrNull(key) ?: opt | |
fun JsonObject.optJsonObject(key: String, opt : JsonObject = JsonObject(mapOf())) = getJsonObjectOrNull(key) ?: opt | |
fun JsonObject.optJsonArray(key: String, opt : JsonArray = JsonArray(listOf())) = getJsonArrayOrNull(key) ?: opt | |
// ----------------- | |
// Map | |
fun JsonObject.toMap(): Map<String, Any> { | |
return mapValues { (_, value) -> value.toKotlinObject() } | |
} | |
// ================================= | |
// MARK: JSON-ARRAY | |
// ================================= | |
private inline fun JsonArray.jsonPrimitiveOrNull(index: Int) : JsonPrimitive? = try { | |
this[index] as JsonPrimitive? | |
} catch (_: Exception) { | |
null | |
} | |
// -------------------------- | |
// Get | |
fun JsonArray.getOrNull(index: Int): Any? = try { | |
when (val element = this[index]) { | |
is JsonPrimitive -> element.get() | |
is JsonArray -> element | |
is JsonObject -> element | |
else -> null | |
} | |
} catch (_: Exception) { | |
null | |
} | |
fun JsonArray.getStringOrNull(index: Int) : String? = jsonPrimitiveOrNull(index)?.contentOrNull | |
fun JsonArray.getBooleanOrNull(index: Int) : Boolean? = jsonPrimitiveOrNull(index)?.booleanOrNull | |
fun JsonArray.getIntOrNull(index: Int) : Int? = jsonPrimitiveOrNull(index)?.intOrNull | |
fun JsonArray.getFloatOrNull(index: Int) : Float? = jsonPrimitiveOrNull(index)?.floatOrNull | |
fun JsonArray.getDoubleOrNull(index: Int) : Double? = jsonPrimitiveOrNull(index)?.doubleOrNull | |
fun JsonArray.getLongOrNull(index: Int) : Long? = jsonPrimitiveOrNull(index)?.longOrNull | |
fun JsonArray.getJsonObjectOrNull(index: Int) : JsonObject? = try { | |
this[index] as JsonObject? | |
} catch (_: Exception) { | |
null | |
} | |
fun JsonArray.getJsonArrayOrNull(index: Int) : JsonArray? = try { | |
this[index] as JsonArray? | |
} catch (_: Exception) { | |
null | |
} | |
// -------------------------- | |
// Require | |
private inline fun errorArray(index: Int, type: String): Nothing = error("JsonArray[$index] is not a $type") | |
fun JsonArray.require(index: Int) = getOrNull(index) ?: error("JsonArray[$index] is out of bound") | |
fun JsonArray.requireString(index: Int) = getStringOrNull(index) ?: errorArray(index, "string") | |
fun JsonArray.requireBoolean(index: Int) = getBooleanOrNull(index) ?: errorArray(index, "boolean") | |
fun JsonArray.requireInt(index: Int) = getIntOrNull(index) ?: errorArray(index, "int") | |
fun JsonArray.requireFloat(index: Int) = getFloatOrNull(index) ?: errorArray(index, "float") | |
fun JsonArray.requireDouble(index: Int) = getDoubleOrNull(index) ?: errorArray(index, "double") | |
fun JsonArray.requireLong(index: Int) = getLongOrNull(index) ?: errorArray(index, "long") | |
fun JsonArray.requireJsonObject(index: Int) = getJsonObjectOrNull(index) ?: errorArray(index, "JsonObject") | |
fun JsonArray.requireJsonArray(index: Int) = getJsonArrayOrNull(index) ?: errorArray(index, "JsonArray") | |
// -------------------------- | |
// Opt | |
fun JsonArray.opt(index: Int, opt : Any) = getOrNull(index) ?: opt | |
fun JsonArray.optString(index: Int, opt : String = "") = getStringOrNull(index) ?: opt | |
fun JsonArray.optBoolean(index: Int, opt : Boolean = false) = getBooleanOrNull(index) ?: opt | |
fun JsonArray.optInt(index: Int, opt : Int = -1) = getIntOrNull(index) ?: opt | |
fun JsonArray.optFloat(index: Int, opt : Float = -1f) = getFloatOrNull(index) ?: opt | |
fun JsonArray.optDouble(index: Int, opt : Double = -1.0) = getDoubleOrNull(index) ?: opt | |
fun JsonArray.optLong(index: Int, opt : Long = -1L) = getLongOrNull(index) ?: opt | |
fun JsonArray.optJsonObject(index: Int, opt : JsonObject = JsonObject(mapOf())) = getJsonObjectOrNull(index) ?: opt | |
fun JsonArray.optJsonArray(index: Int, opt : JsonArray = JsonArray(listOf())) = getJsonArrayOrNull(index) ?: opt | |
// -------------------------- | |
// List | |
fun JsonArray.toList(): List<Any> { | |
return map { it.toKotlinObject() } | |
} | |
fun <T> JsonArray.toListTyped(opt: List<T> = listOf()): List<T> { | |
return try { | |
map { | |
@Suppress("UNCHECKED_CAST") | |
it.toKotlinObject() as T | |
} | |
} catch (_: Exception) { | |
opt | |
} | |
} | |
// ================================= | |
// MARK: JSON-ELEMENT | |
// ================================= | |
private fun JsonElement.toKotlinObject(): Any { | |
return when (this) { | |
is JsonObject -> mapValues { (_, value) -> value.toKotlinObject() } | |
is JsonArray -> map { it.toKotlinObject() } | |
is JsonPrimitive -> this.get() | |
is JsonNull -> "<JsonNull>" | |
else -> "<Unknown>" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment