-
-
Save mitac-lien/3dd3a9d08d320651086cee9b838bb182 to your computer and use it in GitHub Desktop.
I was having trouble with safely parsing Gson objects without loads of try catch statements or a single try catch which would cause the whole object parsing to fail if a single field was incorrect. I also wanted to replicate org.json.JSONObject opt style functions to return null instead of throwing an exception so I can use Kotlin's null handlin…
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
import com.google.gson.* | |
import java.math.BigDecimal | |
import java.math.BigInteger | |
/** | |
* Created by Terence Baker on 13/02/2018. | |
*/ | |
val JsonElement.optString: String? | |
get() = safeConversion { asString } | |
val JsonElement.optLong: Long? | |
get() = safeConversion { asLong } | |
val JsonElement.optBoolean: Boolean? | |
get() = safeConversion { asBoolean } | |
val JsonElement.optFloat: Float? | |
get() = safeConversion { asFloat } | |
val JsonElement.optDouble: Double? | |
get() = safeConversion { asDouble } | |
val JsonElement.optJsonObject: JsonObject? | |
get() = safeConversion { asJsonObject } | |
val JsonElement.optJsonArray: JsonArray? | |
get() = safeConversion { asJsonArray } | |
val JsonElement.optJsonPrimitive: JsonPrimitive? | |
get() = safeConversion { asJsonPrimitive } | |
val JsonElement.optInt: Int? | |
get() = safeConversion { asInt } | |
val JsonElement.optBigDecimal: BigDecimal? | |
get() = safeConversion { asBigDecimal } | |
val JsonElement.optBigInteger: BigInteger? | |
get() = safeConversion { asBigInteger } | |
val JsonElement.optByte: Byte? | |
get() = safeConversion { asByte } | |
val JsonElement.optShort: Short? | |
get() = safeConversion { asShort } | |
val JsonElement.optJsonNull: JsonNull? | |
get() = safeConversion { asJsonNull } | |
val JsonElement.optCharacter: Char? | |
get() = safeConversion { asCharacter } | |
private fun <T> JsonElement.safeConversion(converter: () -> T?): T? { | |
return try { | |
converter() | |
} | |
catch (e: Exception) { | |
null | |
} | |
} |
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
import com.google.gson.JsonArray | |
import com.google.gson.JsonElement | |
import com.google.gson.JsonObject | |
import com.google.gson.JsonPrimitive | |
/** | |
* Created by Terence Baker on 13/02/2018. | |
*/ | |
fun JsonObject.optGet(key: String): JsonElement? = get(key) | |
fun JsonObject.optGetJsonArray(key: String): JsonArray? = getAsJsonArray(key) | |
fun JsonObject.optGetJsonObject(key: String): JsonObject? = getAsJsonObject(key) | |
fun JsonObject.optGetJsonPrimitive(key: String): JsonPrimitive? = getAsJsonPrimitive(key) | |
fun JsonObject.optString(key: String) = optGet(key)?.asString | |
fun JsonObject.optLong(key: String) = optGet(key)?.asLong | |
fun JsonObject.optBoolean(key: String) = optGet(key)?.asBoolean | |
fun JsonObject.optFloat(key: String) = optGet(key)?.asFloat | |
fun JsonObject.optDouble(key: String) = optGet(key)?.asDouble | |
fun JsonObject.optJsonObject(key: String) = optGet(key)?.asJsonObject | |
fun JsonObject.optJsonArray(key: String) = optGet(key)?.asJsonArray | |
fun JsonObject.optJsonPrimitive(key: String) = optGet(key)?.asJsonPrimitive | |
fun JsonObject.optInt(key: String) = optGet(key)?.asInt | |
fun JsonObject.optBigDecimal(key: String) = optGet(key)?.asBigDecimal | |
fun JsonObject.optBigInteger(key: String) = optGet(key)?.asBigInteger | |
fun JsonObject.optByte(key: String) = optGet(key)?.asByte | |
fun JsonObject.optShort(key: String) = optGet(key)?.asShort | |
fun JsonObject.optJsonNull(key: String) = optGet(key)?.asJsonNull | |
fun JsonObject.optCharacter(key: String) = optGet(key)?.asCharacter |
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
open class MyClass { | |
var myString = "" | |
var myDouble = 0.0 | |
var myInt = 0 | |
fun setup(json:JsonObject) { | |
json.optString("stringJsonKey")?.let { myString = it } | |
json.optGet("doubleJsonKey")?.optDouble?.let { myDouble = it } | |
json.optInt("intJsonKey")?.let { myInt = it } | |
} | |
} | |
/* | |
{ | |
"stringJsonKey":"I am a string", | |
"doubleJsonKey":2.5, | |
"intJsonKey":5 | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment