1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #405 from johnjaylward/FixNPE

Updates javadoc to match actual exceptions thrown.
This commit is contained in:
Sean Leary 2018-03-11 15:57:23 -05:00 committed by GitHub
commit 1c1ef5b211
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 124 additions and 72 deletions

View file

@ -36,6 +36,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* A JSONArray is an ordered sequence of values. Its external text form is a * A JSONArray is an ordered sequence of values. Its external text form is a
* string wrapped in square brackets with commas separating the values. The * string wrapped in square brackets with commas separating the values. The
@ -182,7 +183,7 @@ public class JSONArray implements Iterable<Object> {
* Construct a JSONArray from an array * Construct a JSONArray from an array
* *
* @throws JSONException * @throws JSONException
* If not an array. * If not an array or if an array value is non-finite number.
*/ */
public JSONArray(Object array) throws JSONException { public JSONArray(Object array) throws JSONException {
this(); this();
@ -465,11 +466,11 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Determine if the value is null. * Determine if the value is <code>null</code>.
* *
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1.
* @return true if the value at the index is null, or if there is no value. * @return true if the value at the index is <code>null</code>, or if there is no value.
*/ */
public boolean isNull(int index) { public boolean isNull(int index) {
return JSONObject.NULL.equals(this.opt(index)); return JSONObject.NULL.equals(this.opt(index));
@ -953,8 +954,7 @@ public class JSONArray implements Iterable<Object> {
* @return this. * @return this.
*/ */
public JSONArray put(boolean value) { public JSONArray put(boolean value) {
this.put(value ? Boolean.TRUE : Boolean.FALSE); return this.put(value ? Boolean.TRUE : Boolean.FALSE);
return this;
} }
/** /**
@ -964,10 +964,11 @@ public class JSONArray implements Iterable<Object> {
* @param value * @param value
* A Collection value. * A Collection value.
* @return this. * @return this.
* @throws JSONException
* If the value is non-finite number.
*/ */
public JSONArray put(Collection<?> value) { public JSONArray put(Collection<?> value) {
this.put(new JSONArray(value)); return this.put(new JSONArray(value));
return this;
} }
/** /**
@ -975,15 +976,25 @@ public class JSONArray implements Iterable<Object> {
* *
* @param value * @param value
* A double value. * A double value.
* @return this.
* @throws JSONException * @throws JSONException
* if the value is not finite. * if the value is not finite.
* @return this.
*/ */
public JSONArray put(double value) throws JSONException { public JSONArray put(double value) throws JSONException {
Double d = new Double(value); return this.put(Double.valueOf(value));
JSONObject.testValidity(d); }
this.put(d);
return this; /**
* Append a float value. This increases the array's length by one.
*
* @param value
* A float value.
* @return this.
* @throws JSONException
* if the value is not finite.
*/
public JSONArray put(float value) throws JSONException {
return this.put(Float.valueOf(value));
} }
/** /**
@ -994,8 +1005,7 @@ public class JSONArray implements Iterable<Object> {
* @return this. * @return this.
*/ */
public JSONArray put(int value) { public JSONArray put(int value) {
this.put(new Integer(value)); return this.put(Integer.valueOf(value));
return this;
} }
/** /**
@ -1006,8 +1016,7 @@ public class JSONArray implements Iterable<Object> {
* @return this. * @return this.
*/ */
public JSONArray put(long value) { public JSONArray put(long value) {
this.put(new Long(value)); return this.put(Long.valueOf(value));
return this;
} }
/** /**
@ -1017,10 +1026,13 @@ public class JSONArray implements Iterable<Object> {
* @param value * @param value
* A Map value. * A Map value.
* @return this. * @return this.
* @throws JSONException
* If a value in the map is non-finite number.
* @throws NullPointerException
* If a key in the map is <code>null</code>
*/ */
public JSONArray put(Map<?, ?> value) { public JSONArray put(Map<?, ?> value) {
this.put(new JSONObject(value)); return this.put(new JSONObject(value));
return this;
} }
/** /**
@ -1031,8 +1043,11 @@ public class JSONArray implements Iterable<Object> {
* Integer, JSONArray, JSONObject, Long, or String, or the * Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object. * JSONObject.NULL object.
* @return this. * @return this.
* @throws JSONException
* If the value is non-finite number.
*/ */
public JSONArray put(Object value) { public JSONArray put(Object value) {
JSONObject.testValidity(value);
this.myArrayList.add(value); this.myArrayList.add(value);
return this; return this;
} }
@ -1051,8 +1066,7 @@ public class JSONArray implements Iterable<Object> {
* If the index is negative. * If the index is negative.
*/ */
public JSONArray put(int index, boolean value) throws JSONException { public JSONArray put(int index, boolean value) throws JSONException {
this.put(index, value ? Boolean.TRUE : Boolean.FALSE); return this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
return this;
} }
/** /**
@ -1065,11 +1079,10 @@ public class JSONArray implements Iterable<Object> {
* A Collection value. * A Collection value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the index is negative or if the value is not finite. * If the index is negative or if the value is non-finite.
*/ */
public JSONArray put(int index, Collection<?> value) throws JSONException { public JSONArray put(int index, Collection<?> value) throws JSONException {
this.put(index, new JSONArray(value)); return this.put(index, new JSONArray(value));
return this;
} }
/** /**
@ -1083,11 +1096,27 @@ public class JSONArray implements Iterable<Object> {
* A double value. * A double value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the index is negative or if the value is not finite. * If the index is negative or if the value is non-finite.
*/ */
public JSONArray put(int index, double value) throws JSONException { public JSONArray put(int index, double value) throws JSONException {
this.put(index, new Double(value)); return this.put(index, Double.valueOf(value));
return this; }
/**
* Put or replace a float value. 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
* A float value.
* @return this.
* @throws JSONException
* If the index is negative or if the value is non-finite.
*/
public JSONArray put(int index, float value) throws JSONException {
return this.put(index, Float.valueOf(value));
} }
/** /**
@ -1104,8 +1133,7 @@ public class JSONArray implements Iterable<Object> {
* If the index is negative. * If the index is negative.
*/ */
public JSONArray put(int index, int value) throws JSONException { public JSONArray put(int index, int value) throws JSONException {
this.put(index, new Integer(value)); return this.put(index, Integer.valueOf(value));
return this;
} }
/** /**
@ -1122,8 +1150,7 @@ public class JSONArray implements Iterable<Object> {
* If the index is negative. * If the index is negative.
*/ */
public JSONArray put(int index, long value) throws JSONException { public JSONArray put(int index, long value) throws JSONException {
this.put(index, new Long(value)); return this.put(index, Long.valueOf(value));
return this;
} }
/** /**
@ -1138,6 +1165,8 @@ public class JSONArray implements Iterable<Object> {
* @throws JSONException * @throws JSONException
* If the index is negative or if the the value is an invalid * If the index is negative or if the the value is an invalid
* number. * number.
* @throws NullPointerException
* If a key in the map is <code>null</code>
*/ */
public JSONArray put(int index, Map<?, ?> value) throws JSONException { public JSONArray put(int index, Map<?, ?> value) throws JSONException {
this.put(index, new JSONObject(value)); this.put(index, new JSONObject(value));
@ -1161,25 +1190,26 @@ public class JSONArray implements Iterable<Object> {
* number. * number.
*/ */
public JSONArray put(int index, Object value) throws JSONException { public JSONArray put(int index, Object value) throws JSONException {
JSONObject.testValidity(value);
if (index < 0) { if (index < 0) {
throw new JSONException("JSONArray[" + index + "] not found."); throw new JSONException("JSONArray[" + index + "] not found.");
} }
if (index < this.length()) { if (index < this.length()) {
JSONObject.testValidity(value);
this.myArrayList.set(index, value); this.myArrayList.set(index, value);
} else if(index == this.length()){ return this;
// simple append
this.put(value);
} else {
// if we are inserting past the length, we want to grow the array all at once
// instead of incrementally.
this.myArrayList.ensureCapacity(index + 1);
while (index != this.length()) {
this.put(JSONObject.NULL);
}
this.put(value);
} }
return this; if(index == this.length()){
// simple append
return this.put(value);
}
// if we are inserting past the length, we want to grow the array all at once
// instead of incrementally.
this.myArrayList.ensureCapacity(index + 1);
while (index != this.length()) {
// we don't need to test validity of NULL objects
this.myArrayList.add(JSONObject.NULL);
}
return this.put(value);
} }
/** /**

View file

@ -271,6 +271,10 @@ public class JSONObject {
* @param m * @param m
* A map object that can be used to initialize the contents of * A map object that can be used to initialize the contents of
* the JSONObject. * the JSONObject.
* @throws JSONException
* If a value in the map is non-finite number.
* @throws NullPointerException
* If a key in the map is <code>null</code>
*/ */
public JSONObject(Map<?, ?> m) { public JSONObject(Map<?, ?> m) {
if (m == null) { if (m == null) {
@ -278,6 +282,9 @@ public class JSONObject {
} else { } else {
this.map = new HashMap<String, Object>(m.size()); this.map = new HashMap<String, Object>(m.size());
for (final Entry<?, ?> e : m.entrySet()) { for (final Entry<?, ?> e : m.entrySet()) {
if(e.getKey() == null) {
throw new NullPointerException("Null key.");
}
final Object value = e.getValue(); final Object value = e.getValue();
if (value != null) { if (value != null) {
this.map.put(String.valueOf(e.getKey()), wrap(value)); this.map.put(String.valueOf(e.getKey()), wrap(value));
@ -428,7 +435,9 @@ public class JSONObject {
* An object to be accumulated under the key. * An object to be accumulated under the key.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the value is an invalid number or if the key is null. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject accumulate(String key, Object value) throws JSONException { public JSONObject accumulate(String key, Object value) throws JSONException {
testValidity(value); testValidity(value);
@ -457,8 +466,10 @@ public class JSONObject {
* An object to be accumulated under the key. * An object to be accumulated under the key.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the key is null or if the current value associated with * If the value is non-finite number or if the current value associated with
* the key is not a JSONArray. * the key is not a JSONArray.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject append(String key, Object value) throws JSONException { public JSONObject append(String key, Object value) throws JSONException {
testValidity(value); testValidity(value);
@ -856,13 +867,13 @@ public class JSONObject {
} }
/** /**
* Determine if the value associated with the key is null or if there is no * Determine if the value associated with the key is <code>null</code> or if there is no
* value. * value.
* *
* @param key * @param key
* A key string. * A key string.
* @return true if there is no value associated with the key or if the value * @return true if there is no value associated with the key or if the value
* is the JSONObject.NULL object. * is the JSONObject.NULL object.
*/ */
public boolean isNull(String key) { public boolean isNull(String key) {
return JSONObject.NULL.equals(this.opt(key)); return JSONObject.NULL.equals(this.opt(key));
@ -922,7 +933,7 @@ public class JSONObject {
* JSONObject. * JSONObject.
* *
* @return A JSONArray containing the key strings, or null if the JSONObject * @return A JSONArray containing the key strings, or null if the JSONObject
* is empty. * is empty.
*/ */
public JSONArray names() { public JSONArray names() {
if(this.map.isEmpty()) { if(this.map.isEmpty()) {
@ -1485,11 +1496,12 @@ public class JSONObject {
* A boolean which is the value. * A boolean which is the value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the key is null. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, boolean value) throws JSONException { public JSONObject put(String key, boolean value) throws JSONException {
this.put(key, value ? Boolean.TRUE : Boolean.FALSE); return this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
return this;
} }
/** /**
@ -1502,10 +1514,12 @@ public class JSONObject {
* A Collection value. * A Collection value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, Collection<?> value) throws JSONException { public JSONObject put(String key, Collection<?> value) throws JSONException {
this.put(key, new JSONArray(value)); return this.put(key, new JSONArray(value));
return this;
} }
/** /**
@ -1517,11 +1531,12 @@ public class JSONObject {
* A double which is the value. * A double which is the value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the key is null or if the number is invalid. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, double value) throws JSONException { public JSONObject put(String key, double value) throws JSONException {
this.put(key, Double.valueOf(value)); return this.put(key, Double.valueOf(value));
return this;
} }
/** /**
@ -1533,11 +1548,12 @@ public class JSONObject {
* A float which is the value. * A float which is the value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the key is null or if the number is invalid. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, float value) throws JSONException { public JSONObject put(String key, float value) throws JSONException {
this.put(key, Float.valueOf(value)); return this.put(key, Float.valueOf(value));
return this;
} }
/** /**
@ -1549,11 +1565,12 @@ public class JSONObject {
* An int which is the value. * An int which is the value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the key is null. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, int value) throws JSONException { public JSONObject put(String key, int value) throws JSONException {
this.put(key, Integer.valueOf(value)); return this.put(key, Integer.valueOf(value));
return this;
} }
/** /**
@ -1565,11 +1582,12 @@ public class JSONObject {
* A long which is the value. * A long which is the value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the key is null. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, long value) throws JSONException { public JSONObject put(String key, long value) throws JSONException {
this.put(key, Long.valueOf(value)); return this.put(key, Long.valueOf(value));
return this;
} }
/** /**
@ -1582,14 +1600,16 @@ public class JSONObject {
* A Map value. * A Map value.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, Map<?, ?> value) throws JSONException { public JSONObject put(String key, Map<?, ?> value) throws JSONException {
this.put(key, new JSONObject(value)); return this.put(key, new JSONObject(value));
return this;
} }
/** /**
* Put a key/value pair in the JSONObject. If the value is null, then the * Put a key/value pair in the JSONObject. If the value is <code>null</code>, then the
* key will be removed from the JSONObject if it is present. * key will be removed from the JSONObject if it is present.
* *
* @param key * @param key
@ -1600,7 +1620,9 @@ public class JSONObject {
* String, or the JSONObject.NULL object. * String, or the JSONObject.NULL object.
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* If the value is non-finite number or if the key is null. * If the value is non-finite number.
* @throws NullPointerException
* If the key is <code>null</code>.
*/ */
public JSONObject put(String key, Object value) throws JSONException { public JSONObject put(String key, Object value) throws JSONException {
if (key == null) { if (key == null) {
@ -1631,7 +1653,7 @@ public class JSONObject {
if (this.opt(key) != null) { if (this.opt(key) != null) {
throw new JSONException("Duplicate key \"" + key + "\""); throw new JSONException("Duplicate key \"" + key + "\"");
} }
this.put(key, value); return this.put(key, value);
} }
return this; return this;
} }
@ -1652,7 +1674,7 @@ public class JSONObject {
*/ */
public JSONObject putOpt(String key, Object value) throws JSONException { public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) { if (key != null && value != null) {
this.put(key, value); return this.put(key, value);
} }
return this; return this;
} }
@ -2130,7 +2152,7 @@ public class JSONObject {
} }
/** /**
* Wrap an object, if necessary. If the object is null, return the NULL * Wrap an object, if necessary. If the object is <code>null</code>, return the NULL
* object. If it is an array or collection, wrap it in a JSONArray. If it is * object. If it is an array or collection, wrap it in a JSONArray. If it is
* a map, wrap it in a JSONObject. If it is a standard property (Double, * a map, wrap it in a JSONObject. If it is a standard property (Double,
* String, et al) then it is already wrapped. Otherwise, if it comes from * String, et al) then it is already wrapped. Otherwise, if it comes from