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 #486 from johnjaylward/StandardizeExceptionMessages

Standardize Exception Messages
This commit is contained in:
Sean Leary 2019-09-24 20:29:17 -05:00 committed by GitHub
commit 044b035847
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 167 additions and 45 deletions

View file

@ -249,7 +249,7 @@ public class JSONArray implements Iterable<Object> {
.equalsIgnoreCase("true"))) { .equalsIgnoreCase("true"))) {
return true; return true;
} }
throw new JSONException("JSONArray[" + index + "] is not a boolean."); throw wrongValueFormatException(index, "boolean", null);
} }
/** /**
@ -263,7 +263,15 @@ public class JSONArray implements Iterable<Object> {
* to a number. * to a number.
*/ */
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
return this.getNumber(index).doubleValue(); final Object object = this.get(index);
if(object instanceof Number) {
return ((Number)object).doubleValue();
}
try {
return Double.parseDouble(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(index, "double", e);
}
} }
/** /**
@ -277,7 +285,15 @@ public class JSONArray implements Iterable<Object> {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public float getFloat(int index) throws JSONException { public float getFloat(int index) throws JSONException {
return this.getNumber(index).floatValue(); final Object object = this.get(index);
if(object instanceof Number) {
return ((Float)object).floatValue();
}
try {
return Float.parseFloat(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(index, "float", e);
}
} }
/** /**
@ -298,7 +314,7 @@ public class JSONArray implements Iterable<Object> {
} }
return JSONObject.stringToNumber(object.toString()); return JSONObject.stringToNumber(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e); throw wrongValueFormatException(index, "number", e);
} }
} }
@ -322,8 +338,8 @@ public class JSONArray implements Iterable<Object> {
// JSONException should really take a throwable argument. // JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf // If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException // method and place any thrown exception in the JSONException
throw new JSONException("JSONArray[" + index + "] is not an enum of type " throw wrongValueFormatException(index, "enum of type "
+ JSONObject.quote(clazz.getSimpleName()) + "."); + JSONObject.quote(clazz.getSimpleName()), null);
} }
return val; return val;
} }
@ -345,8 +361,7 @@ public class JSONArray implements Iterable<Object> {
Object object = this.get(index); Object object = this.get(index);
BigDecimal val = JSONObject.objectToBigDecimal(object, null); BigDecimal val = JSONObject.objectToBigDecimal(object, null);
if(val == null) { if(val == null) {
throw new JSONException("JSONArray[" + index + throw wrongValueFormatException(index, "BigDecimal", object, null);
"] could not convert to BigDecimal ("+ object + ").");
} }
return val; return val;
} }
@ -365,8 +380,7 @@ public class JSONArray implements Iterable<Object> {
Object object = this.get(index); Object object = this.get(index);
BigInteger val = JSONObject.objectToBigInteger(object, null); BigInteger val = JSONObject.objectToBigInteger(object, null);
if(val == null) { if(val == null) {
throw new JSONException("JSONArray[" + index + throw wrongValueFormatException(index, "BigInteger", object, null);
"] could not convert to BigDecimal ("+ object + ").");
} }
return val; return val;
} }
@ -381,7 +395,15 @@ public class JSONArray implements Iterable<Object> {
* If the key is not found or if the value is not a number. * If the key is not found or if the value is not a number.
*/ */
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
return this.getNumber(index).intValue(); final Object object = this.get(index);
if(object instanceof Number) {
return ((Number)object).intValue();
}
try {
return Integer.parseInt(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(index, "int", e);
}
} }
/** /**
@ -399,7 +421,7 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray) object; return (JSONArray) object;
} }
throw new JSONException("JSONArray[" + index + "] is not a JSONArray."); throw wrongValueFormatException(index, "JSONArray", null);
} }
/** /**
@ -417,7 +439,7 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject) object; return (JSONObject) object;
} }
throw new JSONException("JSONArray[" + index + "] is not a JSONObject."); throw wrongValueFormatException(index, "JSONObject", null);
} }
/** /**
@ -431,7 +453,15 @@ public class JSONArray implements Iterable<Object> {
* to a number. * to a number.
*/ */
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
return this.getNumber(index).longValue(); final Object object = this.get(index);
if(object instanceof Number) {
return ((Number)object).longValue();
}
try {
return Long.parseLong(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(index, "long", e);
}
} }
/** /**
@ -448,7 +478,7 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof String) { if (object instanceof String) {
return (String) object; return (String) object;
} }
throw new JSONException("JSONArray[" + index + "] not a string."); throw wrongValueFormatException(index, "String", null);
} }
/** /**
@ -1454,5 +1484,38 @@ public class JSONArray implements Iterable<Object> {
public boolean isEmpty() { public boolean isEmpty() {
return this.myArrayList.isEmpty(); return this.myArrayList.isEmpty();
} }
/**
* Create a new JSONException in a common format for incorrect conversions.
* @param idx index of the item
* @param valueType the type of value being coerced to
* @param cause optional cause of the coercion failure
* @return JSONException that can be thrown.
*/
private static JSONException wrongValueFormatException(
int idx,
String valueType,
Throwable cause) {
return new JSONException(
"JSONArray[" + idx + "] is not a " + valueType + "."
, cause);
}
/**
* Create a new JSONException in a common format for incorrect conversions.
* @param idx index of the item
* @param valueType the type of value being coerced to
* @param cause optional cause of the coercion failure
* @return JSONException that can be thrown.
*/
private static JSONException wrongValueFormatException(
int idx,
String valueType,
Object value,
Throwable cause) {
return new JSONException(
"JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
, cause);
}
} }

View file

@ -521,8 +521,7 @@ public class JSONObject {
} else if (object instanceof JSONArray) { } else if (object instanceof JSONArray) {
this.put(key, ((JSONArray) object).put(value)); this.put(key, ((JSONArray) object).put(value));
} else { } else {
throw new JSONException("JSONObject[" + key throw wrongValueFormatException(key, "JSONArray", null, null);
+ "] is not a JSONArray.");
} }
return this; return this;
} }
@ -595,9 +594,7 @@ public class JSONObject {
// JSONException should really take a throwable argument. // JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf // If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException // method and place any thrown exception in the JSONException
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), null);
+ "] is not an enum of type " + quote(clazz.getSimpleName())
+ ".");
} }
return val; return val;
} }
@ -623,8 +620,7 @@ public class JSONObject {
.equalsIgnoreCase("true"))) { .equalsIgnoreCase("true"))) {
return true; return true;
} }
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "Boolean", null);
+ "] is not a Boolean.");
} }
/** /**
@ -643,8 +639,7 @@ public class JSONObject {
if (ret != null) { if (ret != null) {
return ret; return ret;
} }
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "BigInteger", object, null);
+ "] could not be converted to BigInteger (" + object + ").");
} }
/** /**
@ -666,8 +661,7 @@ public class JSONObject {
if (ret != null) { if (ret != null) {
return ret; return ret;
} }
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "BigDecimal", object, null);
+ "] could not be converted to BigDecimal (" + object + ").");
} }
/** /**
@ -681,7 +675,15 @@ public class JSONObject {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
return this.getNumber(key).doubleValue(); final Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).doubleValue();
}
try {
return Double.parseDouble(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(key, "double", e);
}
} }
/** /**
@ -695,7 +697,15 @@ public class JSONObject {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public float getFloat(String key) throws JSONException { public float getFloat(String key) throws JSONException {
return this.getNumber(key).floatValue(); final Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).floatValue();
}
try {
return Float.parseFloat(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(key, "float", e);
}
} }
/** /**
@ -716,8 +726,7 @@ public class JSONObject {
} }
return stringToNumber(object.toString()); return stringToNumber(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "number", e);
+ "] is not a number.", e);
} }
} }
@ -732,7 +741,15 @@ public class JSONObject {
* to an integer. * to an integer.
*/ */
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
return this.getNumber(key).intValue(); final Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).intValue();
}
try {
return Integer.parseInt(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(key, "int", e);
}
} }
/** /**
@ -749,8 +766,7 @@ public class JSONObject {
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray) object; return (JSONArray) object;
} }
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "JSONArray", null);
+ "] is not a JSONArray.");
} }
/** /**
@ -767,8 +783,7 @@ public class JSONObject {
if (object instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject) object; return (JSONObject) object;
} }
throw new JSONException("JSONObject[" + quote(key) throw wrongValueFormatException(key, "JSONObject", null);
+ "] is not a JSONObject.");
} }
/** /**
@ -782,7 +797,15 @@ public class JSONObject {
* to a long. * to a long.
*/ */
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
return this.getNumber(key).longValue(); final Object object = this.get(key);
if(object instanceof Number) {
return ((Number)object).longValue();
}
try {
return Long.parseLong(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(key, "long", e);
}
} }
/** /**
@ -837,7 +860,7 @@ public class JSONObject {
if (object instanceof String) { if (object instanceof String) {
return (String) object; return (String) object;
} }
throw new JSONException("JSONObject[" + quote(key) + "] not a string."); throw wrongValueFormatException(key, "string", null);
} }
/** /**
@ -853,8 +876,11 @@ public class JSONObject {
/** /**
* Increment a property of a JSONObject. If there is no such property, * Increment a property of a JSONObject. If there is no such property,
* create one with a value of 1. If there is such a property, and if it is * create one with a value of 1 (Integer). If there is such a property, and if it is
* an Integer, Long, Double, or Float, then add one to it. * an Integer, Long, Double, Float, BigInteger, or BigDecimal then add one to it.
* No overflow bounds checking is performed, so callers should initialize the key
* prior to this call with an appropriate type that can handle the maximum expected
* value.
* *
* @param key * @param key
* A key string. * A key string.
@ -867,18 +893,18 @@ public class JSONObject {
Object value = this.opt(key); Object value = this.opt(key);
if (value == null) { if (value == null) {
this.put(key, 1); this.put(key, 1);
} else if (value instanceof BigInteger) {
this.put(key, ((BigInteger)value).add(BigInteger.ONE));
} else if (value instanceof BigDecimal) {
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
this.put(key, ((Integer) value).intValue() + 1); this.put(key, ((Integer) value).intValue() + 1);
} else if (value instanceof Long) { } else if (value instanceof Long) {
this.put(key, ((Long) value).longValue() + 1L); this.put(key, ((Long) value).longValue() + 1L);
} else if (value instanceof Double) { } else if (value instanceof BigInteger) {
this.put(key, ((Double) value).doubleValue() + 1.0d); this.put(key, ((BigInteger)value).add(BigInteger.ONE));
} else if (value instanceof Float) { } else if (value instanceof Float) {
this.put(key, ((Float) value).floatValue() + 1.0f); this.put(key, ((Float) value).floatValue() + 1.0f);
} else if (value instanceof Double) {
this.put(key, ((Double) value).doubleValue() + 1.0d);
} else if (value instanceof BigDecimal) {
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
} else { } else {
throw new JSONException("Unable to increment [" + quote(key) + "]."); throw new JSONException("Unable to increment [" + quote(key) + "].");
} }
@ -2548,4 +2574,37 @@ public class JSONObject {
} }
return results; return results;
} }
/**
* Create a new JSONException in a common format for incorrect conversions.
* @param key name of the key
* @param valueType the type of value being coerced to
* @param cause optional cause of the coercion failure
* @return JSONException that can be thrown.
*/
private static JSONException wrongValueFormatException(
String key,
String valueType,
Throwable cause) {
return new JSONException(
"JSONObject[" + quote(key) + "] is not a " + valueType + "."
, cause);
}
/**
* Create a new JSONException in a common format for incorrect conversions.
* @param key name of the key
* @param valueType the type of value being coerced to
* @param cause optional cause of the coercion failure
* @return JSONException that can be thrown.
*/
private static JSONException wrongValueFormatException(
String key,
String valueType,
Object value,
Throwable cause) {
return new JSONException(
"JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value + ")."
, cause);
}
} }