@Override public Object getMapValue(final Object obj, final String key) { JSONObject json = (JSONObject) obj; if (!json.has(key)) { return UNDEFINED; } return json.get(key); }
@Test public void an_object_can_be_read() { JSONObject book = using(TAPESTRY_JSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[0]"); assertThat(book.get("author").toString()).isEqualTo("Nigel Rees"); }
@Override public Object getMapValue(final Object obj, final String key) { JSONObject json = (JSONObject) obj; if (!json.has(key)) { return UNDEFINED; } return json.get(key); }
@Override public Object getMapValue(final Object obj, final String key) { JSONObject json = (JSONObject) obj; if (!json.has(key)) { return UNDEFINED; } return json.get(key); }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or throws otherwise. * * @param name The name of the field that we want. * @return a specified field value (if it is a JSONObject) * @throws RuntimeException if the mapping doesn't exist or is not a {@code * JSONObject}. */ public JSONObject getJSONObject(String name) { Object object = get(name); if (object instanceof JSONObject) { return (JSONObject) object; } else { throw JSON.typeMismatch(false, name, object, "JSONObject"); } }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONObject}, or throws otherwise. * * @param name The name of the field that we want. * @return a specified field value (if it is a JSONObject) * @throws RuntimeException if the mapping doesn't exist or is not a {@code * JSONObject}. */ public JSONObject getJSONObject(String name) { Object object = get(name); if (object instanceof JSONObject) { return (JSONObject) object; } else { throw JSON.typeMismatch(false, name, object, "JSONObject"); } }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or throws otherwise. * * @param name The field we want to get. * @return The value of the field (if it is a JSONArray. * @throws RuntimeException if the mapping doesn't exist or is not a {@code * JSONArray}. */ public JSONArray getJSONArray(String name) { Object object = get(name); if (object instanceof JSONArray) { return (JSONArray) object; } else { throw JSON.typeMismatch(false, name, object, "JSONArray"); } }
/** * Returns the value mapped by {@code name} if it exists and is a {@code * JSONArray}, or throws otherwise. * * @param name The field we want to get. * @return The value of the field (if it is a JSONArray. * @throws RuntimeException if the mapping doesn't exist or is not a {@code * JSONArray}. */ public JSONArray getJSONArray(String name) { Object object = get(name); if (object instanceof JSONArray) { return (JSONArray) object; } else { throw JSON.typeMismatch(false, name, object, "JSONArray"); } }
/** * Merge obj1 with obj2. Obj2 has predominance. * * @param obj1 * @param obj2 * @return null if obj1 is null. Else return obj1 merged with obj2 */ public final static JSONObject merge(JSONObject obj1, JSONObject obj2) { if (obj1 == null) return null; if (obj2 == null) return obj1; for (String key : obj2.keys()) { obj1.put(key, obj2.get(key)); } return null; }
/** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary, or throws if no such mapping exists. * * @param name The name of the field we want. * @return The value of the field. * @throws RuntimeException if no such mapping exists. */ public String getString(String name) { Object object = get(name); String result = JSON.toString(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "String"); } return result; }
/** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary, or throws if no such mapping exists. * * @param name The name of the field we want. * @return The value of the field. * @throws RuntimeException if no such mapping exists. */ public String getString(String name) { Object object = get(name); String result = JSON.toString(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "String"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is a boolean or * can be coerced to a boolean, or throws otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to a boolean. */ public boolean getBoolean(String name) { Object object = get(name); Boolean result = JSON.toBoolean(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "Boolean"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is a double or * can be coerced to a double, or throws otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to a double. */ public double getDouble(String name) { Object object = get(name); Double result = JSON.toDouble(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "number"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is a boolean or * can be coerced to a boolean, or throws otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to a boolean. */ public boolean getBoolean(String name) { Object object = get(name); Boolean result = JSON.toBoolean(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "Boolean"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is a double or * can be coerced to a double, or throws otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to a double. */ public double getDouble(String name) { Object object = get(name); Double result = JSON.toDouble(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "number"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is an int or * can be coerced to an int, or throws otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to an int. */ public int getInt(String name) { Object object = get(name); Integer result = JSON.toInteger(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "int"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is an int or * can be coerced to an int, or throws otherwise. * * @param name The name of the field we want. * @return The selected value if it exists. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to an int. */ public int getInt(String name) { Object object = get(name); Integer result = JSON.toInteger(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "int"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is a long or * can be coerced to a long, or throws otherwise. * Note that JSON represents numbers as doubles, * * so this is <a href="#lossy">lossy</a>; use strings to transfer numbers * via JSON without loss. * * @param name The name of the field that we want. * @return The value of the field. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to a long. */ public long getLong(String name) { Object object = get(name); Long result = JSON.toLong(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "long"); } return result; }
/** * Returns the value mapped by {@code name} if it exists and is a long or * can be coerced to a long, or throws otherwise. * Note that JSON represents numbers as doubles, * * so this is <a href="#lossy">lossy</a>; use strings to transfer numbers * via JSON without loss. * * @param name The name of the field that we want. * @return The value of the field. * @throws RuntimeException if the mapping doesn't exist or cannot be coerced * to a long. */ public long getLong(String name) { Object object = get(name); Long result = JSON.toLong(object); if (result == null) { throw JSON.typeMismatch(false, name, object, "long"); } return result; }
events.put(key, existing.get(key));