/** * Get an optional JSONObject associated with a key. It returns null if * there is no such key, or if its value is not a JSONObject. * * @param key * A key string. * @return A JSONObject which is the value. */ public JSONObject optJSONObject(String key) { Object object = this.opt(key); return object instanceof JSONObject ? (JSONObject) object : null; }
/** * Get an optional JSONArray associated with a key. It returns null if there * is no such key, or if its value is not a JSONArray. * * @param key * A key string. * @return A JSONArray which is the value. */ public JSONArray optJSONArray(String key) { Object o = this.opt(key); return o instanceof JSONArray ? (JSONArray) o : null; }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}. Returns null otherwise. */ public JSONArray optJSONArray(String name) { Object object = opt(name); return object instanceof JSONArray ? (JSONArray) object : null; }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}. Returns null otherwise. */ public JSONObject optJSONObject(String name) { Object object = opt(name); return object instanceof JSONObject ? (JSONObject) object : null; }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or null otherwise. * * @param name The name of the field we want. * @return The value of the specified field (assuming it is a JSNOArray */ public JSONArray optJSONArray(String name) { Object object = opt(name); return object instanceof JSONArray ? (JSONArray) object : null; }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or null otherwise. * * @param name The name of the value we want. * @return The specified value. */ public JSONObject optJSONObject(String name) { Object object = opt(name); return object instanceof JSONObject ? (JSONObject) object : null; }
/** * Determine if the value associated with the key is null or if there is no * value. * * @param key * A key string. * @return true if there is no value associated with the key or if the value * is the JSONObject.NULL object. */ public boolean isNull(String key) { return JSONObject.NULL.equals(this.opt(key)); }
/** * Get an optional string associated with a key. It returns the defaultValue * if there is no such key. * * @param key * A key string. * @param defaultValue * The default. * @return A string which is the value. */ public String optString(String key, String defaultValue) { Object object = this.opt(key); return NULL.equals(object) ? defaultValue : object.toString(); }
/** * Returns the value mapped by {@code name} if it exists and is a long or * can be coerced to a long. Returns {@code fallback} otherwise. Note that JSON represents * numbers as doubles, so this is <a href="#lossy">lossy</a>; use strings to transfer * numbers via JSON. */ public long optLong(String name, long fallback) { Object object = opt(name); Long result = JSON.toLong(object); return result != null ? result : fallback; }
/** * Returns the value mapped by {@code name} if it exists and is a boolean or * can be coerced to a boolean. Returns {@code fallback} otherwise. */ public boolean optBoolean(String name, boolean fallback) { Object object = opt(name); Boolean result = JSON.toBoolean(object); return result != null ? result : fallback; }
/** * Returns the value mapped by {@code name} if it exists and is an int or * can be coerced to an int. Returns {@code fallback} otherwise. */ public int optInt(String name, int fallback) { Object object = opt(name); Integer result = JSON.toInteger(object); return result != null ? result : fallback; }
/** * Returns the value mapped by {@code name} if it exists and is a double or * can be coerced to a double. Returns {@code fallback} otherwise. */ public double optDouble(String name, double fallback) { Object object = opt(name); Double result = JSON.toDouble(object); return result != null ? result : fallback; }
/** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary. Returns {@code fallback} if no such mapping exists. */ public String optString(String name, String fallback) { Object object = opt(name); String result = JSON.toString(object); return result != null ? result : fallback; }
private void parseBizParams(BaseCell cell, JSONObject json) { if (json == null) { return; } Iterator<String> iterator = json.keys(); while (iterator.hasNext()) { String key = iterator.next(); cell.addBizParam(key, json.opt(key)); } }
public GfJsonObject getJSONObject(String key) { Object opt = jsonObject.opt(key); if (opt instanceof GfJsonObject) { return (GfJsonObject) opt; } if (opt == null) { return null; } return new GfJsonObject(opt); }
static boolean jsonObjectContainsValue(JSONObject jsonObject, Object value) { @SuppressWarnings("unchecked") Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { Object thisValue = jsonObject.opt(keys.next()); if (thisValue != null && thisValue.equals(value)) { return true; } } return false; }
static Collection<Object> jsonObjectValues(JSONObject jsonObject) { ArrayList<Object> result = new ArrayList<Object>(); @SuppressWarnings("unchecked") Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { result.add(jsonObject.opt(keys.next())); } return result; } }
static Set<Map.Entry<String, Object>> jsonObjectEntrySet(JSONObject jsonObject) { HashSet<Map.Entry<String, Object>> result = new HashSet<Map.Entry<String, Object>>(); @SuppressWarnings("unchecked") Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); Object value = jsonObject.opt(key); result.add(new JSONObjectEntry(key, value)); } return result; }
@Override public Object getMapValue(Object obj, String key) { try { JSONObject jsonObject = toJsonObject(obj); Object o = jsonObject.opt(key); if (o == null) { return UNDEFINED; } else { return unwrap(o); } } catch (JSONException e) { throw new JsonPathException(e); } }