/** * Make a JSONException to signal a syntax error. * * @param message The error message. * @return A JSONException object, suitable for throwing */ public JSONException syntaxError(final String message) { return new JSONException(message + toString()); }
/** * Put a value in the JSONArray, where the value will be a * JSONObject which is produced from a Map. * @param value A Map value. * @return this. */ public JSONArray put(final Map value) { put(new JSONObject(value)); return this; }
private static JSONArray toJSONMsgArray(final Map<String, Object>[] messages) { JSONArray array = new JSONArray(); for (Map<String, Object> message : messages) { array.put(new JSONObject(message)); } return array; }
static String toJSON(final Map<String, Object>[] messages) { JSONArray array = new JSONArray(); for (Map<String, Object> message : messages) { array.put(new JSONObject(message)); } return array.toString(); }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONArray which is produced from a Collection. * @param key A key string. * @param value A Collection value. * @return this. * @throws JSONException */ public JSONObject put(final String key, final Collection value) throws JSONException { put(key, new JSONArray(value)); return this; }
/** * Get the optional boolean value associated with an index. * It returns false if there is no value at that index, * or if the value is not Boolean.TRUE or the String "true". * * @param index The index must be between 0 and length() - 1. * @return The truth. */ public boolean optBoolean(final int index) { return optBoolean(index, false); }
/** * Get the optional double value associated with an index. * NaN is returned if there is no value for the index, * or if the value is not a number and cannot be converted to a number. * * @param index The index must be between 0 and length() - 1. * @return The value. */ public double optDouble(final int index) { return optDouble(index, Double.NaN); }
/** * Get the optional int value associated with an index. * Zero is returned if there is no value for the index, * or if the value is not a number and cannot be converted to a number. * * @param index The index must be between 0 and length() - 1. * @return The value. */ public int optInt(final int index) { return optInt(index, 0); }
/** * Get the optional JSONArray associated with an index. * @param index subscript * @return A JSONArray value, or null if the index has no value, * or if the value is not a JSONArray. */ public JSONArray optJSONArray(final int index) { Object o = opt(index); return o instanceof JSONArray ? (JSONArray)o : null; }
/** * Get the optional long value associated with an index. * Zero is returned if there is no value for the index, * or if the value is not a number and cannot be converted to a number. * * @param index The index must be between 0 and length() - 1. * @return The value. */ public long optLong(final int index) { return optLong(index, 0); }
/** * Get an optional int value associated with a key, * or zero 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. * @return An object which is the value. */ public int optInt(final String key) { return optInt(key, 0); }
private static String toJSON(final Map<String, Object>[] messages) { JSONArray array = new JSONArray(); for (Map<String, Object> message : messages) { array.put(new JSONObject(message)); } return array.toString(); }
/** * Put a value in the JSONArray, where the value will be a * JSONObject which is produced from a Map. * @param value A Map value. * @return this. */ public JSONArray put(final Map value) { put(new JSONObject(value)); return this; }
/** * Put a key/value pair in the JSONObject, where the value will be a * JSONArray which is produced from a Collection. * @param key A key string. * @param value A Collection value. * @return this. * @throws JSONException */ public JSONObject put(final String key, final Collection value) throws JSONException { put(key, new JSONArray(value)); return this; }
/** * Make a JSONException to signal a syntax error. * * @param message The error message. * @return A JSONException object, suitable for throwing */ public JSONException syntaxError(final String message) { return new JSONException(message + toString()); }
/** * Get the optional boolean value associated with an index. * It returns false if there is no value at that index, * or if the value is not Boolean.TRUE or the String "true". * * @param index The index must be between 0 and length() - 1. * @return The truth. */ public boolean optBoolean(final int index) { return optBoolean(index, false); }
/** * Get the optional double value associated with an index. * NaN is returned if there is no value for the index, * or if the value is not a number and cannot be converted to a number. * * @param index The index must be between 0 and length() - 1. * @return The value. */ public double optDouble(final int index) { return optDouble(index, Double.NaN); }
/** * Get the optional int value associated with an index. * Zero is returned if there is no value for the index, * or if the value is not a number and cannot be converted to a number. * * @param index The index must be between 0 and length() - 1. * @return The value. */ public int optInt(final int index) { return optInt(index, 0); }
/** * Put a value in the JSONArray, where the value will be a * JSONObject which is produced from a Map. * @param index The subscript. * @param value The Map value. * @return this. * @throws JSONException If the index is negative or if the the value is * an invalid number. */ public JSONArray put(final int index, final Map value) throws JSONException { put(index, new JSONObject(value)); return this; }