2019-12-17 18:08:28 +01:00
|
|
|
package info.nightscout.androidaps.utils
|
|
|
|
|
|
|
|
import org.json.JSONException
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
|
|
|
object JsonHelper {
|
2020-01-01 23:23:16 +01:00
|
|
|
|
2019-12-17 18:08:28 +01:00
|
|
|
@JvmStatic
|
|
|
|
fun safeGetObject(json: JSONObject?, fieldName: String, defaultValue: Any): Any {
|
|
|
|
var result = defaultValue
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json[fieldName]
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetJSONObject(json: JSONObject?, fieldName: String, defaultValue: JSONObject?): JSONObject? {
|
|
|
|
var result = defaultValue
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getJSONObject(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetString(json: JSONObject?, fieldName: String): String? {
|
|
|
|
var result: String? = null
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getString(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetString(json: JSONObject?, fieldName: String, defaultValue: String): String {
|
|
|
|
var result = defaultValue
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getString(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetStringAllowNull(json: JSONObject?, fieldName: String, defaultValue: String?): String? {
|
|
|
|
var result = defaultValue
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getString(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetDouble(json: JSONObject?, fieldName: String): Double {
|
|
|
|
var result = 0.0
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getDouble(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-01 23:23:16 +01:00
|
|
|
@JvmStatic
|
|
|
|
fun safeGetDoubleAllowNull(json: JSONObject?, fieldName: String): Double? {
|
|
|
|
var result: Double? = null
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getDouble(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-12-17 18:08:28 +01:00
|
|
|
@JvmStatic
|
|
|
|
fun safeGetDouble(json: JSONObject?, fieldName: String, defaultValue: Double): Double {
|
|
|
|
var result = defaultValue
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getDouble(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
2019-12-31 11:57:58 +01:00
|
|
|
fun safeGetInt(json: JSONObject?, fieldName: String): Int =
|
|
|
|
safeGetInt(json, fieldName, 0)
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetInt(json: JSONObject?, fieldName: String, defaultValue: Int): Int {
|
|
|
|
var result = defaultValue
|
2019-12-17 18:08:28 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getInt(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetLong(json: JSONObject?, fieldName: String): Long {
|
|
|
|
var result: Long = 0
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getLong(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun safeGetBoolean(json: JSONObject?, fieldName: String): Boolean {
|
|
|
|
var result = false
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getBoolean(fieldName)
|
|
|
|
} catch (ignored: JSONException) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|