/** * Produce a string in double quotes with backslash sequences in all the * right places. A backslash will be inserted within </, producing <\/, * allowing JSON text to be delivered in HTML. In JSON text, a string * cannot contain a control character or an unescaped quote or backslash. * @param string A String * @return A String correctly formatted for insertion in a JSON text. */ static protected String quote(String string) { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { try { return quote(string, sw).toString(); } catch (IOException ignored) { // will never happen - we are writing to a string writer return ""; } } }
/** * Produce a string in double quotes with backslash sequences in all the * right places. A backslash will be inserted within </, producing <\/, * allowing JSON text to be delivered in HTML. In JSON text, a string * cannot contain a control character or an unescaped quote or backslash. * @param string A String * @return A String correctly formatted for insertion in a JSON text. */ static protected String quote(String string) { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { try { return quote(string, sw).toString(); } catch (IOException ignored) { // will never happen - we are writing to a string writer return ""; } } }
/** * Get the double value associated with a key. * @param key A key string. * @return The numeric value. * @throws RuntimeException if the key is not found or * if the value is not a Number object and cannot be converted to a number. */ public double getDouble(String key) { Object object = this.get(key); try { return object instanceof Number ? ((Number)object).doubleValue() : Double.parseDouble((String)object); } catch (Exception e) { throw new RuntimeException("JSONObject[" + quote(key) + "] is not a number."); } }
/** * Get the value object associated with a key. * * @param key A key string. * @return The object associated with the key. * @throws JSONException if the key is not found. */ private Object get(String key) { if (key == null) { throw new RuntimeException("Null key."); } Object object = this.opt(key); if (object == null) { throw new RuntimeException("JSONObject[" + quote(key) + "] not found."); } return object; }
/** * Get the long value associated with a key. * * @param key A key string. * @return The long value. * @throws RuntimeException if the key is not found or if the value cannot * be converted to a long. */ public long getLong(String key) { Object object = this.get(key); try { return object instanceof Number ? ((Number)object).longValue() : Long.parseLong((String)object); } catch (Exception e) { throw new RuntimeException("JSONObject[" + quote(key) + "] is not a long.", e); } }
/** * Get the double value associated with a key. * @param key A key string. * @return The numeric value. * @throws JSONException if the key is not found or * if the value is not a Number object and cannot be converted to a number. */ public double getDouble(String key) { Object object = this.get(key); try { return object instanceof Number ? ((Number)object).doubleValue() : Double.parseDouble((String)object); } catch (Exception e) { throw new RuntimeException("JSONObject[" + quote(key) + "] is not a number."); } }
/** * Get the long value associated with a key. * * @param key A key string. * @return The long value. * @throws JSONException if the key is not found or if the value cannot * be converted to a long. */ public long getLong(String key) { Object object = this.get(key); try { return object instanceof Number ? ((Number)object).longValue() : Long.parseLong((String)object); } catch (Exception e) { throw new RuntimeException("JSONObject[" + quote(key) + "] is not a long.", e); } }
/** * Get the JSONObject value associated with a key. * * @webref jsonobject:method * @brief Gets the JSONObject value associated with a key * @param key a key string * @return A JSONObject which is the value. * @throws JSONException if the key is not found or if the value is not a JSONObject. * @see JSONObject#getJSONArray(String) * @see JSONObject#setJSONObject(String, JSONObject) * @see JSONObject#setJSONArray(String, JSONArray) */ public JSONObject getJSONObject(String key) { Object object = this.get(key); if (object instanceof JSONObject) { return (JSONObject)object; } throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the JSONArray value associated with a key. * * @webref jsonobject:method * @brief Gets the JSONArray value associated with a key * @param key a key string * @return A JSONArray which is the value. * @throws JSONException if the key is not found or if the value is not a JSONArray. * @see JSONObject#getJSONObject(String) * @see JSONObject#setJSONObject(String, JSONObject) * @see JSONObject#setJSONArray(String, JSONArray) */ public JSONArray getJSONArray(String key) { Object object = this.get(key); if (object instanceof JSONArray) { return (JSONArray)object; } throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Gets the String associated with a key * * @webref jsonobject:method * @brief Gets the string value associated with a key * @param key a key string * @return A string which is the value. * @throws JSONException if there is no string value for the key. * @see JSONObject#getInt(String) * @see JSONObject#getFloat(String) * @see JSONObject#getBoolean(String) */ public String getString(String key) { Object object = this.get(key); if (object instanceof String) { return (String)object; } throw new RuntimeException("JSONObject[" + quote(key) + "] not a string."); }
/** * Get the value object associated with a key. * * @param key A key string. * @return The object associated with the key. * @throws RuntimeException if the key is not found. */ public Object get(String key) { if (key == null) { throw new RuntimeException("JSONObject.get(null) called"); } Object object = this.opt(key); if (object == null) { // Adding for rev 0257 in line with other p5 api return null; } if (object == null) { throw new RuntimeException("JSONObject[" + quote(key) + "] not found"); } return object; }
/** * Get the JSONObject value associated with a key. * * @webref jsonobject:method * @brief Gets the JSONObject value associated with a key * @param key a key string * @return A JSONObject which is the value or null if not available. * @throws RuntimeException if the value is not a JSONObject. * @see JSONObject#getJSONArray(String) * @see JSONObject#setJSONObject(String, JSONObject) * @see JSONObject#setJSONArray(String, JSONArray) */ public JSONObject getJSONObject(String key) { Object object = this.get(key); if (object == null) { return null; } if (object instanceof JSONObject) { return (JSONObject)object; } throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the JSONArray value associated with a key. * * @webref jsonobject:method * @brief Gets the JSONArray value associated with a key * @param key a key string * @return A JSONArray which is the value, or null if not present * @throws RuntimeException if the value is not a JSONArray. * @see JSONObject#getJSONObject(String) * @see JSONObject#setJSONObject(String, JSONObject) * @see JSONObject#setJSONArray(String, JSONArray) */ public JSONArray getJSONArray(String key) { Object object = this.get(key); if (object == null) { return null; } if (object instanceof JSONArray) { return (JSONArray)object; } throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Return this dictionary as a String in JSON format. */ public String toJSON() { StringList items = new StringList(); for (int i = 0; i < count; i++) { items.append(JSONObject.quote(keys[i])+ ": " + values[i]); } return "{ " + items.join(", ") + " }"; }
/** * Gets the String associated with a key * * @webref jsonobject:method * @brief Gets the string value associated with a key * @param key a key string * @return A string which is the value. * @throws RuntimeException if there is no string value for the key. * @see JSONObject#getInt(String) * @see JSONObject#getFloat(String) * @see JSONObject#getBoolean(String) */ public String getString(String key) { Object object = this.get(key); if (object == null) { // Adding for rev 0257 in line with other p5 api return null; } if (object instanceof String) { return (String)object; } throw new RuntimeException("JSONObject[" + quote(key) + "] is not a string"); }
/** * Return this dictionary as a String in JSON format. */ public String toJSON() { StringList items = new StringList(); for (int i = 0; i < count; i++) { items.append(JSONObject.quote(keys[i])+ ": " + values[i]); } return "{ " + items.join(", ") + " }"; }
/** * Gets the int value associated with a key * * @webref jsonobject:method * @brief Gets the int value associated with a key * @param key a key string * @return The integer value. * @throws JSONException if the key is not found or if the value cannot * be converted to an integer. * @see JSONObject#getFloat(String) * @see JSONObject#getString(String) * @see JSONObject#getBoolean(String) */ public int getInt(String key) { Object object = this.get(key); try { return object instanceof Number ? ((Number)object).intValue() : Integer.parseInt((String)object); } catch (Exception e) { throw new RuntimeException("JSONObject[" + quote(key) + "] is not an int."); } }
/** * Get the boolean value associated with a key. * * @webref jsonobject:method * @brief Gets the boolean value associated with a key * @param key a key string * @return The truth. * @throws RuntimeException if the value is not a Boolean or the String "true" or "false". * @see JSONObject#getInt(String) * @see JSONObject#getFloat(String) * @see JSONObject#getString(String) */ public boolean getBoolean(String key) { Object object = this.get(key); if (object.equals(Boolean.FALSE) || (object instanceof String && ((String)object).equalsIgnoreCase("false"))) { return false; } else if (object.equals(Boolean.TRUE) || (object instanceof String && ((String)object).equalsIgnoreCase("true"))) { return true; } throw new RuntimeException("JSONObject[" + quote(key) + "] is not a Boolean."); }
/** * Return this dictionary as a String in JSON format. */ public String toJSON() { StringList temp = new StringList(); for (String item : this) { temp.append(JSONObject.quote(item)); } return "[ " + temp.join(", ") + " ]"; }