/** * Append an object value. This increases the array's length by one. * * @param value An object value. The value should be a Boolean, Double, Integer, JSONArray, * JSONObject, JSONLiteral, Long, or String, or the JSONObject.NULL singleton. * @return this array */ public JSONArray put(Object value) { assert value != null; JSONObject.testValidity(value); list.add(value); return this; }
public JSONArray put(int paramInt, Object paramObject) throws JSONException { JSONObject.testValidity(paramObject); if (paramInt < 0) throw new JSONException("JSONArray[" + paramInt + "] not found."); if (paramInt < length()) { this.myArrayList.set(paramInt, paramObject); return this; } while (paramInt != length()) put(JSONObject.NULL); put(paramObject); return this; }
/** * Append values to the array under a key. If the key does not exist in the JSONObject, then the * key is put in the JSONObject with its value being a JSONArray containing the value parameter. * If the key was already associated with a JSONArray, then the value parameter is appended to it. * * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws JSONException If the key is null or if the current value associated with the key is not * a JSONArray. */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); Object object = this.opt(key); if (object == null) { this.put(key, new JSONArray().put(value)); } else if (object instanceof JSONArray) { this.put(key, ((JSONArray) object).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); } return this; }
/** * Accumulate values under a key. It is similar to the put method except that if there is already * an object stored under the key then a JSONArray is stored under the key to hold all of the * accumulated values. If there is already a JSONArray, then the new value is appended to it. In * contrast, the put method replaces the previous value. * * <p>If only one value is accumulated that is not a JSONArray, then the result will be the same * as using put. But if multiple values are accumulated, then the result will be like append. * * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws JSONException If the value is an invalid number or if the key is null. */ public JSONObject accumulate(String key, Object value) throws JSONException { testValidity(value); Object object = this.opt(key); if (object == null) { this.put(key, value instanceof JSONArray ? new JSONArray().put(value) : value); } else if (object instanceof JSONArray) { ((JSONArray) object).put(value); } else { this.put(key, new JSONArray().put(object).put(value)); } return this; }
/** * Put or replace an object value in the JSONArray. If the index is greater than the length of the * JSONArray, then null elements will be added as necessary to pad it out. * * @param index The subscript. * @param value The value to put into the array. The value should be a Boolean, Double, Integer, * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object. * @return this. * @throws JSONException If the index is negative or if the the value is an invalid number. */ public JSONArray put(int index, Object value) throws JSONException { JSONObject.testValidity(value); if (index < 0) throw new JSONException("JSONArray[" + index + "] not found."); if (index < length()) { myArrayList.set(index, value); } else { while (index != length()) { this.put(JSONObject.NULL); } this.put(value); } return this; }
/** * Put or replace an object value in the JSONArray. If the index is greater than the length of the * JSONArray, then null elements will be added as necessary to pad it out. * * @param index The subscript. * @param value The value to put into the array. The value should be a Boolean, Double, Integer, * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object. * @return this. */ public JSONArray put(int index, Object value) { JSONObject.testValidity(value); if (index < 0) { throw new IllegalArgumentException("JSONArray[" + index + "] not found."); } if (index < this.length()) { this.myArrayList.set(index, value); } else { while (index != this.length()) { this.put(JSONObject.NULL); } this.put(value); } return this; }
/** * Put or replace an object value in the JSONArray. If the index is greater than the length of the * JSONArray, then null elements will be added as necessary to pad it out. * * @param index The subscript. * @param value The value to put into the array. The value should be a Boolean, Double, Integer, * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object. * @return this. * @throws JSONException If the index is negative or if the the value is an invalid number. */ private JSONArray set(int index, Object value) { JSONObject.testValidity(value); if (index < 0) { throw new RuntimeException("JSONArray[" + index + "] not found."); } if (index < this.size()) { this.myArrayList.set(index, value); } else { while (index != this.size()) { this.append(JSONObject.NULL); } this.append(value); } return this; }
/** * Put or replace an object value in the JSONArray. If the index is greater than the length of the * JSONArray, then null elements will be added as necessary to pad it out. * * @param index The subscript. * @param value The value to put into the array. The value should be a Boolean, Double, Integer, * JSONArray, JSONObject, JSONString, Long, or String, or the JSONObject.NULL singeton. * @return this array * @throws RuntimeException If the index is negative or if the the value is an invalid number. */ public JSONArray put(int index, Object value) { assert value != null; if (index < 0) { throw new RuntimeException("JSONArray[" + index + "] not found."); } JSONObject.testValidity(value); if (index < length()) { list.set(index, value); } else { while (index != length()) list.add(JSONObject.NULL); list.add(value); } return this; }
/** * Produce a string from a Number. * * @param number A Number * @return A String. * @throws JSONException If n is a non-finite number. */ public static String numberToString(Number number) throws JSONException { if (number == null) { throw new JSONException("Null pointer"); } testValidity(number); // Shave off trailing zeros and decimal point, if possible. String string = number.toString(); if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && string.indexOf('E') < 0) { while (string.endsWith("0")) { string = string.substring(0, string.length() - 1); } if (string.endsWith(".")) { string = string.substring(0, string.length() - 1); } } return string; }
/** * Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from * the JSONObject if it is present. * * @param key A key string. * @param value An object which is the value. It should be of one of these types: Boolean, Double, * Integer, JSONArray, JSONObject, Long, String, or the JSONObject.NULL object. * @return this. * @throws JSONException If the value is non-finite number or if the key is null. */ public JSONObject put(String key, Object value) throws JSONException { String pooled; if (key == null) { throw new JSONException("Null key."); } if (value != null) { testValidity(value); pooled = (String) keyPool.get(key); if (pooled == null) { if (keyPool.size() >= keyPoolSize) { keyPool = new HashMap(keyPoolSize); } keyPool.put(key, key); } else { key = pooled; } this.map.put(key, value); } else { this.remove(key); } return this; }
/** * Append a double value. This increases the array's length by one. * * @param value A double value. * @return this. */ public JSONArray put(double value) { Double d = new Double(value); JSONObject.testValidity(d); this.put(d); return this; }
/** * Append a double value. This increases the array's length by one. * * @param value A double value. * @return this. * @throws JSONException if the value is not finite. */ public JSONArray put(double value) throws JSONException { Double d = new Double(value); JSONObject.testValidity(d); put(d); return this; }