2019-02-26 20:38:27 +01:00
|
|
|
package info.nightscout.androidaps.utils;
|
2018-01-04 22:22:08 +01:00
|
|
|
|
2019-05-16 13:57:37 +02:00
|
|
|
import androidx.annotation.Nullable;
|
2018-04-12 21:45:59 +02:00
|
|
|
|
2018-01-04 22:22:08 +01:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JSonHelper is a Helper class which contains several methods to safely get data from the ggiven JSONObject.
|
|
|
|
*
|
|
|
|
* Created by triplem on 04.01.18.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class JsonHelper {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(JsonHelper.class);
|
|
|
|
|
2019-07-27 19:56:07 +02:00
|
|
|
private JsonHelper() {}
|
2018-01-04 22:22:08 +01:00
|
|
|
|
2018-04-12 21:45:59 +02:00
|
|
|
public static Object safeGetObject(JSONObject json, String fieldName, Object defaultValue) {
|
|
|
|
Object result = defaultValue;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-12 21:45:59 +02:00
|
|
|
try {
|
|
|
|
result = json.get(fieldName);
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2018-04-05 09:39:18 +02:00
|
|
|
public static String safeGetString(JSONObject json, String fieldName) {
|
2018-01-04 22:22:08 +01:00
|
|
|
String result = null;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-05 09:39:18 +02:00
|
|
|
try {
|
|
|
|
result = json.getString(fieldName);
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
2018-01-04 22:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:39:18 +02:00
|
|
|
public static String safeGetString(JSONObject json, String fieldName, String defaultValue) {
|
2018-03-12 11:22:19 +01:00
|
|
|
String result = defaultValue;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-05 09:39:18 +02:00
|
|
|
try {
|
|
|
|
result = json.getString(fieldName);
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
2018-03-12 11:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:39:18 +02:00
|
|
|
public static double safeGetDouble(JSONObject json, String fieldName) {
|
2018-01-04 22:22:08 +01:00
|
|
|
double result = 0d;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-05 09:39:18 +02:00
|
|
|
try {
|
|
|
|
result = json.getDouble(fieldName);
|
|
|
|
} catch (JSONException ignored) {
|
2019-08-23 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double safeGetDouble(JSONObject json, String fieldName, double defaultValue) {
|
|
|
|
double result = defaultValue;
|
|
|
|
|
|
|
|
if (json != null && json.has(fieldName)) {
|
|
|
|
try {
|
|
|
|
result = json.getDouble(fieldName);
|
|
|
|
} catch (JSONException ignored) {
|
2018-04-05 09:39:18 +02:00
|
|
|
}
|
2018-01-04 22:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:39:18 +02:00
|
|
|
public static int safeGetInt(JSONObject json, String fieldName) {
|
2018-01-04 22:22:08 +01:00
|
|
|
int result = 0;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-05 09:39:18 +02:00
|
|
|
try {
|
|
|
|
result = json.getInt(fieldName);
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long safeGetLong(JSONObject json, String fieldName) {
|
|
|
|
long result = 0;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-05 09:39:18 +02:00
|
|
|
try {
|
|
|
|
result = json.getLong(fieldName);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean safeGetBoolean(JSONObject json, String fieldName) {
|
|
|
|
boolean result = false;
|
|
|
|
|
2018-10-31 07:59:04 +01:00
|
|
|
if (json != null && json.has(fieldName)) {
|
2018-04-05 09:39:18 +02:00
|
|
|
try {
|
|
|
|
result = json.getBoolean(fieldName);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
}
|
2018-01-04 22:22:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|