/** * Appends {@code value} to the end of this array. * * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double, or {@link JSONObject#NULL}}. May * not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() * infinities}. Unsupported values are not permitted and will cause the * array to be in an inconsistent state. * @return this array. */ public JSONArray put(Object value) { JSONObject.testValidity(value); values.add(value); return this; }
/** * Appends {@code value} to the end of this array. * * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double, or {@link JSONObject#NULL}}. May * not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() * infinities}. Unsupported values are not permitted and will cause the * array to be in an inconsistent state. * @return this array. */ public JSONArray put(Object value) { JSONObject.testValidity(value); values.add(value); return this; }
/** * Same as {@link #put}, with added validity checks. * * @param value The value to append. */ void checkedPut(Object value) { JSONObject.testValidity(value); if (value instanceof Number) { JSON.checkDouble(((Number) value).doubleValue()); } put(value); }
/** * Same as {@link #put}, with added validity checks. * * @param value The value to append. */ void checkedPut(Object value) { JSONObject.testValidity(value); if (value instanceof Number) { JSON.checkDouble(((Number) value).doubleValue()); } put(value); }
/** * Sets the value at {@code index} to {@code value}, null padding this array * to the required length if necessary. If a value already exists at {@code * index}, it will be replaced. * * @param index Where to put the value. * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May * not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() * infinities}. * @return this array. * @throws RuntimeException If the value cannot be represented as a finite double value. */ public JSONArray put(int index, Object value) { if (index < 0) { throw new RuntimeException("JSONArray[" + index + "] not found."); } JSONObject.testValidity(value); if (value instanceof Number) { // deviate from the original by checking all Numbers, not just floats & doubles JSON.checkDouble(((Number) value).doubleValue()); } while (values.size() <= index) { values.add(null); } values.set(index, value); return this; }
/** * Sets the value at {@code index} to {@code value}, null padding this array * to the required length if necessary. If a value already exists at {@code * index}, it will be replaced. * * @param index Where to put the value. * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May * not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() * infinities}. * @return this array. * @throws RuntimeException If the value cannot be represented as a finite double value. */ public JSONArray put(int index, Object value) { if (index < 0) { throw new RuntimeException("JSONArray[" + index + "] not found."); } JSONObject.testValidity(value); if (value instanceof Number) { // deviate from the original by checking all Numbers, not just floats & doubles JSON.checkDouble(((Number) value).doubleValue()); } while (values.size() <= index) { values.add(null); } values.set(index, value); return this; }
/** * Maps {@code name} to {@code value}, clobbering any existing name/value * mapping with the same name. If the value is {@code null}, any existing * mapping for {@code name} is removed. * * @param name The name of the new value. * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double, {@link #NULL}, or {@code null}. May not be * {@link Double#isNaN() NaNs} or {@link Double#isInfinite() * infinities}. * @return this object. * @throws RuntimeException if the value is an invalid double (infinite or NaN). */ public JSONObject put(String name, Object value) { if (value == null) { nameValuePairs.remove(name); return this; } testValidity(value); if (value instanceof Number) { // deviate from the original by checking all Numbers, not just floats & doubles JSON.checkDouble(((Number) value).doubleValue()); } nameValuePairs.put(checkName(name), value); return this; }
/** * Maps {@code name} to {@code value}, clobbering any existing name/value * mapping with the same name. If the value is {@code null}, any existing * mapping for {@code name} is removed. * * @param name The name of the new value. * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double, {@link #NULL}, or {@code null}. May not be * {@link Double#isNaN() NaNs} or {@link Double#isInfinite() * infinities}. * @return this object. * @throws RuntimeException if the value is an invalid double (infinite or NaN). */ public JSONObject put(String name, Object value) { if (value == null) { nameValuePairs.remove(name); return this; } testValidity(value); if (value instanceof Number) { // deviate from the original by checking all Numbers, not just floats & doubles JSON.checkDouble(((Number) value).doubleValue()); } nameValuePairs.put(checkName(name), value); return this; }
/** * Appends values to the array mapped to {@code name}. A new {@link JSONArray} * mapping for {@code name} will be inserted if no mapping exists. If the existing * mapping for {@code name} is not a {@link JSONArray}, a {@link RuntimeException} * will be thrown. * * @param name The name of the array to which the value should be appended. * @param value The value to append. * @return this object. * @throws RuntimeException if {@code name} is {@code null} or if the mapping for * {@code name} is non-null and is not a {@link JSONArray}. */ public JSONObject append(String name, Object value) { testValidity(value); Object current = nameValuePairs.get(checkName(name)); final JSONArray array; if (current instanceof JSONArray) { array = (JSONArray) current; } else if (current == null) { JSONArray newArray = new JSONArray(); nameValuePairs.put(name, newArray); array = newArray; } else { throw new RuntimeException("JSONObject[\"" + name + "\"] is not a JSONArray."); } array.checkedPut(value); return this; }
/** * Appends values to the array mapped to {@code name}. A new {@link JSONArray} * mapping for {@code name} will be inserted if no mapping exists. If the existing * mapping for {@code name} is not a {@link JSONArray}, a {@link RuntimeException} * will be thrown. * * @param name The name of the array to which the value should be appended. * @param value The value to append. * @return this object. * @throws RuntimeException if {@code name} is {@code null} or if the mapping for * {@code name} is non-null and is not a {@link JSONArray}. */ public JSONObject append(String name, Object value) { testValidity(value); Object current = nameValuePairs.get(checkName(name)); final JSONArray array; if (current instanceof JSONArray) { array = (JSONArray) current; } else if (current == null) { JSONArray newArray = new JSONArray(); nameValuePairs.put(name, newArray); array = newArray; } else { throw new RuntimeException("JSONObject[\"" + name + "\"] is not a JSONArray."); } array.checkedPut(value); return this; }