Skip to content

Instantly share code, notes, and snippets.

@tanmatra
Created September 18, 2019 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanmatra/0b77ad23dcd1e97c5f3aa548f1368881 to your computer and use it in GitHub Desktop.
Save tanmatra/0b77ad23dcd1e97c5f3aa548f1368881 to your computer and use it in GitHub Desktop.
Kotlin JSON builder
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
@DslMarker
annotation class JsonDSL
@JsonDSL
inline class JsonObjectBuilder(@PublishedApi internal val jsonObject: JsonObject)
{
infix fun String.to(value: String?) {
jsonObject.addProperty(this, value)
}
infix fun String.to(value: Number?) {
jsonObject.addProperty(this, value)
}
infix fun String.to(value: Boolean?) {
jsonObject.addProperty(this, value)
}
infix fun String.to(element: JsonElement?) {
jsonObject.add(this, element)
}
inline infix fun String.toObject(block: JsonObjectBuilder.() -> Unit) {
jsonObject.add(this, jsonObject(block))
}
inline infix fun String.toArray(block: JsonArrayBuilder.() -> Unit) {
jsonObject.add(this, jsonArray(block))
}
}
@JsonDSL
inline class JsonArrayBuilder(@PublishedApi internal val jsonArray: JsonArray)
{
operator fun String?.unaryPlus() {
jsonArray.add(this)
}
operator fun JsonElement?.unaryPlus() {
jsonArray.add(this)
}
fun add(element: JsonElement?) {
jsonArray.add(element)
}
fun add(number: Number?) {
jsonArray.add(number)
}
fun add(boolean: Boolean?) {
jsonArray.add(boolean)
}
inline fun addObject(block: JsonObjectBuilder.() -> Unit) {
jsonArray.add(jsonObject(block))
}
inline fun addArray(block: JsonArrayBuilder.() -> Unit) {
jsonArray.add(jsonArray(block))
}
}
inline fun jsonObject(block: JsonObjectBuilder.() -> Unit): JsonObject {
val jsonObject = JsonObject()
JsonObjectBuilder(jsonObject).apply(block)
return jsonObject
}
inline fun jsonArray(block: JsonArrayBuilder.() -> Unit): JsonArray {
val jsonArray = JsonArray()
JsonArrayBuilder(jsonArray).apply(block)
return jsonArray
}
val JsonElement.asObjectOrNull: JsonObject?
get() = if (isJsonObject) asJsonObject else null
val JsonElement.asArrayOrNull: JsonArray?
get() = if (isJsonArray) asJsonArray else null
val JsonElement.asStringOrNull: String?
get() = if (isJsonPrimitive && asJsonPrimitive.isString) asString else null
val JsonElement.asIntOrNull: Int?
get() = if (isJsonPrimitive) {
val primitive = asJsonPrimitive
when {
primitive.isNumber -> primitive.asNumber.toInt()
primitive.isString -> primitive.asString.toIntOrNull()
else -> null
}
} else {
null
}
val JsonElement.asBooleanOrNull: Boolean?
get() = if (isJsonPrimitive && asJsonPrimitive.isBoolean) asBoolean else null
fun JsonObject.getOrCreateObject(key: String): JsonObject {
return get(key)?.asObjectOrNull ?: JsonObject().also { add(key, it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment