/** * @webref input:files * @param input String to parse as a JSONObject * @see PApplet#loadJSONObject(String) * @see PApplet#saveJSONObject(JSONObject, String) */ public JSONObject parseJSONObject(String input) { return new JSONObject(new StringReader(input)); }
/** * Return the JSON data formatted with two spaces for indents. * Chosen to do this since it's the most common case (e.g. with println()). * Same as format(2). Use the format() function for more options. */ @Override public String toString() { try { return format(2); } catch (Exception e) { return null; } }
/** * 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."); }
try { boolean commanate = false; final int length = this.size(); Iterator keys = this.keyIterator(); writer.write('{'); writer.write(quote(key.toString())); writer.write(':'); if (actualFactor > 0) { writer.write(' '); writeValue(writer, this.map.get(key), actualFactor, indent); } else if (length != 0) { final int newindent = indent + actualFactor; writer.write('\n'); indent(writer, newindent); writer.write(quote(key.toString())); writer.write(':'); if (actualFactor > 0) { writer.write(' '); writeValue(writer, this.map.get(key), actualFactor, newindent); commanate = true; indent(writer, indent);
return numberToString((Number) value); return new JSONObject((Map)value).toString(); return new JSONArray(value).toString(); return quote(value.toString());
writer.write("null"); } else if (value instanceof JSONObject) { ((JSONObject) value).write(writer, indentFactor, indent); } else if (value instanceof JSONArray) { ((JSONArray) value).write(writer, indentFactor, indent); } else if (value instanceof Map) { new JSONObject((Map) value).write(writer, indentFactor, indent); } else if (value instanceof Collection) { new JSONArray((Collection) value).write(writer, indentFactor, new JSONArray(value).write(writer, indentFactor, indent); } else if (value instanceof Number) { writer.write(numberToString((Number) value)); } else if (value instanceof Boolean) { writer.write(value.toString()); quote(value.toString(), writer);
writer.write("null"); } else if (value instanceof JSONObject) { ((JSONObject) value).writeInternal(writer, indentFactor, indent); } else if (value instanceof JSONArray) { ((JSONArray) value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Map) { new JSONObject(value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Collection) { new JSONArray(value).writeInternal(writer, indentFactor, new JSONArray(value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Number) { writer.write(numberToString((Number) value)); } else if (value instanceof Boolean) { writer.write(value.toString()); quote(value.toString(), writer);
case '{': this.back(); return new JSONObject(this); case '[': this.back(); throw new RuntimeException("Missing value"); return JSONObject.stringToValue(string);
JSONObject.writeValue(writer, this.myArrayList.get(0), thisFactor, indent); } else if (length != 0) { writer.write('\n'); JSONObject.indent(writer, newindent); JSONObject.writeValue(writer, this.myArrayList.get(i), thisFactor, newindent); commanate = true; writer.write('\n'); JSONObject.indent(writer, indent);
/** * @webref jsonobject:method * @brief Gets the float value associated with a key * @param key a key string * @see JSONObject#getInt(String) * @see JSONObject#getString(String) * @see JSONObject#getBoolean(String) */ public float getFloat(String key) { return (float) getDouble(key); }
public float getFloat(String key, float defaultValue) { try { return getFloat(key); } catch (Exception e) { return defaultValue; } }
/** * Get an optional boolean associated with a key. * It returns the defaultValue if there is no such key, or if it is not * a Boolean or the String "true" or "false" (case insensitive). * * @param key A key string. * @param defaultValue The default. * @return The truth. */ public boolean getBoolean(String key, boolean defaultValue) { try { return this.getBoolean(key); } catch (Exception e) { return defaultValue; } }
/** * Get an optional long value associated with a key, * or the default if there is no such key or if the value is not a number. * If the value is a string, an attempt will be made to evaluate it as * a number. * * @param key A key string. * @param defaultValue The default. * @return An object which is the value. */ public long getLong(String key, long defaultValue) { try { return this.getLong(key); } catch (Exception e) { return defaultValue; } }
/** * Get an optional int value associated with a key, * or the default if there is no such key or if the value is not a number. * If the value is a string, an attempt will be made to evaluate it as * a number. * * @param key A key string. * @param defaultValue The default. * @return An object which is the value. */ public int getInt(String key, int defaultValue) { try { return this.getInt(key); } catch (Exception e) { return defaultValue; } }
try { boolean commanate = false; final int length = this.size(); Iterator keys = this.keyIterator(); writer.write('{'); writer.write(quote(key.toString())); writer.write(':'); if (actualFactor > 0) { writeValue(writer, this.map.get(key), indentFactor, indent); } else if (length != 0) { final int newIndent = indent + actualFactor; writer.write('\n'); indent(writer, newIndent); writer.write(quote(key.toString())); writer.write(':'); if (actualFactor > 0) { writeValue(writer, this.map.get(key), indentFactor, newIndent); commanate = true; writer.write('\n'); indent(writer, indent);
return numberToString((Number) value); return new JSONObject(value).toString(); return new JSONArray(value).toString(); return quote(value.toString());
case '{': this.back(); return new JSONObject(this); case '[': this.back(); throw new RuntimeException("Missing value"); return JSONObject.stringToValue(string);
JSONObject.writeValue(writer, this.myArrayList.get(0), indentFactor, indent); writer.write('\n'); JSONObject.indent(writer, newIndent); JSONObject.writeValue(writer, this.myArrayList.get(i), indentFactor, newIndent); commanate = true; writer.write('\n'); JSONObject.indent(writer, indent);
/** * @webref jsonobject:method * @brief Gets the float value associated with a key * @param key a key string * @see JSONObject#getInt(String) * @see JSONObject#getString(String) * @see JSONObject#getBoolean(String) */ public float getFloat(String key) { return (float) getDouble(key); }
public float getFloat(String key, float defaultValue) { try { return getFloat(key); } catch (Exception e) { return defaultValue; } }