Skip to content

Commit 044b035

Browse files
authored
Merge pull request stleary#486 from johnjaylward/StandardizeExceptionMessages
Standardize Exception Messages
2 parents 5b62cf1 + e9c27ab commit 044b035

File tree

2 files changed

+167
-45
lines changed

2 files changed

+167
-45
lines changed

JSONArray.java

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public boolean getBoolean(int index) throws JSONException {
249249
.equalsIgnoreCase("true"))) {
250250
return true;
251251
}
252-
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
252+
throw wrongValueFormatException(index, "boolean", null);
253253
}
254254

255255
/**
@@ -263,7 +263,15 @@ public boolean getBoolean(int index) throws JSONException {
263263
* to a number.
264264
*/
265265
public double getDouble(int index) throws JSONException {
266-
return this.getNumber(index).doubleValue();
266+
final Object object = this.get(index);
267+
if(object instanceof Number) {
268+
return ((Number)object).doubleValue();
269+
}
270+
try {
271+
return Double.parseDouble(object.toString());
272+
} catch (Exception e) {
273+
throw wrongValueFormatException(index, "double", e);
274+
}
267275
}
268276

269277
/**
@@ -277,7 +285,15 @@ public double getDouble(int index) throws JSONException {
277285
* object and cannot be converted to a number.
278286
*/
279287
public float getFloat(int index) throws JSONException {
280-
return this.getNumber(index).floatValue();
288+
final Object object = this.get(index);
289+
if(object instanceof Number) {
290+
return ((Float)object).floatValue();
291+
}
292+
try {
293+
return Float.parseFloat(object.toString());
294+
} catch (Exception e) {
295+
throw wrongValueFormatException(index, "float", e);
296+
}
281297
}
282298

283299
/**
@@ -298,7 +314,7 @@ public Number getNumber(int index) throws JSONException {
298314
}
299315
return JSONObject.stringToNumber(object.toString());
300316
} catch (Exception e) {
301-
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
317+
throw wrongValueFormatException(index, "number", e);
302318
}
303319
}
304320

@@ -322,8 +338,8 @@ public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONExcep
322338
// JSONException should really take a throwable argument.
323339
// If it did, I would re-implement this with the Enum.valueOf
324340
// method and place any thrown exception in the JSONException
325-
throw new JSONException("JSONArray[" + index + "] is not an enum of type "
326-
+ JSONObject.quote(clazz.getSimpleName()) + ".");
341+
throw wrongValueFormatException(index, "enum of type "
342+
+ JSONObject.quote(clazz.getSimpleName()), null);
327343
}
328344
return val;
329345
}
@@ -345,8 +361,7 @@ public BigDecimal getBigDecimal (int index) throws JSONException {
345361
Object object = this.get(index);
346362
BigDecimal val = JSONObject.objectToBigDecimal(object, null);
347363
if(val == null) {
348-
throw new JSONException("JSONArray[" + index +
349-
"] could not convert to BigDecimal ("+ object + ").");
364+
throw wrongValueFormatException(index, "BigDecimal", object, null);
350365
}
351366
return val;
352367
}
@@ -365,8 +380,7 @@ public BigInteger getBigInteger (int index) throws JSONException {
365380
Object object = this.get(index);
366381
BigInteger val = JSONObject.objectToBigInteger(object, null);
367382
if(val == null) {
368-
throw new JSONException("JSONArray[" + index +
369-
"] could not convert to BigDecimal ("+ object + ").");
383+
throw wrongValueFormatException(index, "BigInteger", object, null);
370384
}
371385
return val;
372386
}
@@ -381,7 +395,15 @@ public BigInteger getBigInteger (int index) throws JSONException {
381395
* If the key is not found or if the value is not a number.
382396
*/
383397
public int getInt(int index) throws JSONException {
384-
return this.getNumber(index).intValue();
398+
final Object object = this.get(index);
399+
if(object instanceof Number) {
400+
return ((Number)object).intValue();
401+
}
402+
try {
403+
return Integer.parseInt(object.toString());
404+
} catch (Exception e) {
405+
throw wrongValueFormatException(index, "int", e);
406+
}
385407
}
386408

387409
/**
@@ -399,7 +421,7 @@ public JSONArray getJSONArray(int index) throws JSONException {
399421
if (object instanceof JSONArray) {
400422
return (JSONArray) object;
401423
}
402-
throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
424+
throw wrongValueFormatException(index, "JSONArray", null);
403425
}
404426

405427
/**
@@ -417,7 +439,7 @@ public JSONObject getJSONObject(int index) throws JSONException {
417439
if (object instanceof JSONObject) {
418440
return (JSONObject) object;
419441
}
420-
throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
442+
throw wrongValueFormatException(index, "JSONObject", null);
421443
}
422444

423445
/**
@@ -431,7 +453,15 @@ public JSONObject getJSONObject(int index) throws JSONException {
431453
* to a number.
432454
*/
433455
public long getLong(int index) throws JSONException {
434-
return this.getNumber(index).longValue();
456+
final Object object = this.get(index);
457+
if(object instanceof Number) {
458+
return ((Number)object).longValue();
459+
}
460+
try {
461+
return Long.parseLong(object.toString());
462+
} catch (Exception e) {
463+
throw wrongValueFormatException(index, "long", e);
464+
}
435465
}
436466

437467
/**
@@ -448,7 +478,7 @@ public String getString(int index) throws JSONException {
448478
if (object instanceof String) {
449479
return (String) object;
450480
}
451-
throw new JSONException("JSONArray[" + index + "] not a string.");
481+
throw wrongValueFormatException(index, "String", null);
452482
}
453483

454484
/**
@@ -1454,5 +1484,38 @@ public List<Object> toList() {
14541484
public boolean isEmpty() {
14551485
return this.myArrayList.isEmpty();
14561486
}
1487+
1488+
/**
1489+
* Create a new JSONException in a common format for incorrect conversions.
1490+
* @param idx index of the item
1491+
* @param valueType the type of value being coerced to
1492+
* @param cause optional cause of the coercion failure
1493+
* @return JSONException that can be thrown.
1494+
*/
1495+
private static JSONException wrongValueFormatException(
1496+
int idx,
1497+
String valueType,
1498+
Throwable cause) {
1499+
return new JSONException(
1500+
"JSONArray[" + idx + "] is not a " + valueType + "."
1501+
, cause);
1502+
}
1503+
1504+
/**
1505+
* Create a new JSONException in a common format for incorrect conversions.
1506+
* @param idx index of the item
1507+
* @param valueType the type of value being coerced to
1508+
* @param cause optional cause of the coercion failure
1509+
* @return JSONException that can be thrown.
1510+
*/
1511+
private static JSONException wrongValueFormatException(
1512+
int idx,
1513+
String valueType,
1514+
Object value,
1515+
Throwable cause) {
1516+
return new JSONException(
1517+
"JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
1518+
, cause);
1519+
}
14571520

14581521
}

0 commit comments

Comments
 (0)