Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Add Comment
Please, Sign In to add comment