Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize Exception Messages #486

Merged
merged 1 commit into from Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 78 additions & 15 deletions JSONArray.java
Expand Up @@ -249,7 +249,7 @@ public boolean getBoolean(int index) throws JSONException {
.equalsIgnoreCase("true"))) {
return true;
}
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
throw wrongValueFormatException(index, "boolean", null);
}

/**
Expand All @@ -263,7 +263,15 @@ public boolean getBoolean(int index) throws JSONException {
* to a number.
*/
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);
}
}

/**
Expand All @@ -277,7 +285,15 @@ public double getDouble(int index) throws JSONException {
* object and cannot be converted to a number.
*/
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);
}
}

/**
Expand All @@ -298,7 +314,7 @@ public Number getNumber(int index) throws JSONException {
}
return JSONObject.stringToNumber(object.toString());
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
throw wrongValueFormatException(index, "number", e);
}
}

Expand All @@ -322,8 +338,8 @@ public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONExcep
// JSONException should really take a throwable argument.
// If it did, I would re-implement this with the Enum.valueOf
// method and place any thrown exception in the JSONException
throw new JSONException("JSONArray[" + index + "] is not an enum of type "
+ JSONObject.quote(clazz.getSimpleName()) + ".");
throw wrongValueFormatException(index, "enum of type "
+ JSONObject.quote(clazz.getSimpleName()), null);
}
return val;
}
Expand All @@ -345,8 +361,7 @@ public BigDecimal getBigDecimal (int index) throws JSONException {
Object object = this.get(index);
BigDecimal val = JSONObject.objectToBigDecimal(object, null);
if(val == null) {
throw new JSONException("JSONArray[" + index +
"] could not convert to BigDecimal ("+ object + ").");
throw wrongValueFormatException(index, "BigDecimal", object, null);
}
return val;
}
Expand All @@ -365,8 +380,7 @@ public BigInteger getBigInteger (int index) throws JSONException {
Object object = this.get(index);
BigInteger val = JSONObject.objectToBigInteger(object, null);
if(val == null) {
throw new JSONException("JSONArray[" + index +
"] could not convert to BigDecimal ("+ object + ").");
throw wrongValueFormatException(index, "BigInteger", object, null);
}
return val;
}
Expand All @@ -381,7 +395,15 @@ public BigInteger getBigInteger (int index) throws JSONException {
* If the key is not found or if the value is not a number.
*/
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);
}
}

/**
Expand All @@ -399,7 +421,7 @@ public JSONArray getJSONArray(int index) throws JSONException {
if (object instanceof JSONArray) {
return (JSONArray) object;
}
throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
throw wrongValueFormatException(index, "JSONArray", null);
}

/**
Expand All @@ -417,7 +439,7 @@ public JSONObject getJSONObject(int index) throws JSONException {
if (object instanceof JSONObject) {
return (JSONObject) object;
}
throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
throw wrongValueFormatException(index, "JSONObject", null);
}

/**
Expand All @@ -431,7 +453,15 @@ public JSONObject getJSONObject(int index) throws JSONException {
* to a number.
*/
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);
}
}

/**
Expand All @@ -448,7 +478,7 @@ public String getString(int index) throws JSONException {
if (object instanceof String) {
return (String) object;
}
throw new JSONException("JSONArray[" + index + "] not a string.");
throw wrongValueFormatException(index, "String", null);
}

/**
Expand Down Expand Up @@ -1454,5 +1484,38 @@ public List<Object> toList() {
public boolean 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);
}

}