Skip to content

Commit a642329

Browse files
author
John J. Aylward
committed
Updates value error messages to be consistent.
Provide both the type and value that failed conversion. Tries not to "toString" large value types like Arrays or Maps. For those types it will just output the type and not a value.
1 parent 9abb35a commit a642329

3 files changed

Lines changed: 57 additions & 64 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public boolean getBoolean(int index) throws JSONException {
288288
.equalsIgnoreCase("true"))) {
289289
return true;
290290
}
291-
throw wrongValueFormatException(index, "boolean", null);
291+
throw wrongValueFormatException(index, "boolean", object, null);
292292
}
293293

294294
/**
@@ -309,7 +309,7 @@ public double getDouble(int index) throws JSONException {
309309
try {
310310
return Double.parseDouble(object.toString());
311311
} catch (Exception e) {
312-
throw wrongValueFormatException(index, "double", e);
312+
throw wrongValueFormatException(index, "double", object, e);
313313
}
314314
}
315315

@@ -331,7 +331,7 @@ public float getFloat(int index) throws JSONException {
331331
try {
332332
return Float.parseFloat(object.toString());
333333
} catch (Exception e) {
334-
throw wrongValueFormatException(index, "float", e);
334+
throw wrongValueFormatException(index, "float", object, e);
335335
}
336336
}
337337

@@ -353,7 +353,7 @@ public Number getNumber(int index) throws JSONException {
353353
}
354354
return JSONObject.stringToNumber(object.toString());
355355
} catch (Exception e) {
356-
throw wrongValueFormatException(index, "number", e);
356+
throw wrongValueFormatException(index, "number", object, e);
357357
}
358358
}
359359

@@ -378,7 +378,7 @@ public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONExcep
378378
// If it did, I would re-implement this with the Enum.valueOf
379379
// method and place any thrown exception in the JSONException
380380
throw wrongValueFormatException(index, "enum of type "
381-
+ JSONObject.quote(clazz.getSimpleName()), null);
381+
+ JSONObject.quote(clazz.getSimpleName()), opt(index), null);
382382
}
383383
return val;
384384
}
@@ -441,7 +441,7 @@ public int getInt(int index) throws JSONException {
441441
try {
442442
return Integer.parseInt(object.toString());
443443
} catch (Exception e) {
444-
throw wrongValueFormatException(index, "int", e);
444+
throw wrongValueFormatException(index, "int", object, e);
445445
}
446446
}
447447

@@ -460,7 +460,7 @@ public JSONArray getJSONArray(int index) throws JSONException {
460460
if (object instanceof JSONArray) {
461461
return (JSONArray) object;
462462
}
463-
throw wrongValueFormatException(index, "JSONArray", null);
463+
throw wrongValueFormatException(index, "JSONArray", object, null);
464464
}
465465

466466
/**
@@ -478,7 +478,7 @@ public JSONObject getJSONObject(int index) throws JSONException {
478478
if (object instanceof JSONObject) {
479479
return (JSONObject) object;
480480
}
481-
throw wrongValueFormatException(index, "JSONObject", null);
481+
throw wrongValueFormatException(index, "JSONObject", object, null);
482482
}
483483

484484
/**
@@ -499,7 +499,7 @@ public long getLong(int index) throws JSONException {
499499
try {
500500
return Long.parseLong(object.toString());
501501
} catch (Exception e) {
502-
throw wrongValueFormatException(index, "long", e);
502+
throw wrongValueFormatException(index, "long", object, e);
503503
}
504504
}
505505

@@ -517,7 +517,7 @@ public String getString(int index) throws JSONException {
517517
if (object instanceof String) {
518518
return (String) object;
519519
}
520-
throw wrongValueFormatException(index, "String", null);
520+
throw wrongValueFormatException(index, "String", object, null);
521521
}
522522

523523
/**
@@ -1464,6 +1464,7 @@ public String toString() {
14641464
* &nbsp;<small>(right bracket)</small>.
14651465
* @throws JSONException if a called function fails
14661466
*/
1467+
@SuppressWarnings("resource")
14671468
public String toString(int indentFactor) throws JSONException {
14681469
StringWriter sw = new StringWriter();
14691470
synchronized (sw.getBuffer()) {
@@ -1513,6 +1514,7 @@ public Writer write(Writer writer) throws JSONException {
15131514
* @return The writer.
15141515
* @throws JSONException if a called function fails or unable to write
15151516
*/
1517+
@SuppressWarnings("resource")
15161518
public Writer write(Writer writer, int indentFactor, int indent)
15171519
throws JSONException {
15181520
try {
@@ -1680,22 +1682,6 @@ private void addAll(Object array, boolean wrap) throws JSONException {
16801682
}
16811683
}
16821684

1683-
/**
1684-
* Create a new JSONException in a common format for incorrect conversions.
1685-
* @param idx index of the item
1686-
* @param valueType the type of value being coerced to
1687-
* @param cause optional cause of the coercion failure
1688-
* @return JSONException that can be thrown.
1689-
*/
1690-
private static JSONException wrongValueFormatException(
1691-
int idx,
1692-
String valueType,
1693-
Throwable cause) {
1694-
return new JSONException(
1695-
"JSONArray[" + idx + "] is not a " + valueType + "."
1696-
, cause);
1697-
}
1698-
16991685
/**
17001686
* Create a new JSONException in a common format for incorrect conversions.
17011687
* @param idx index of the item
@@ -1708,8 +1694,19 @@ private static JSONException wrongValueFormatException(
17081694
String valueType,
17091695
Object value,
17101696
Throwable cause) {
1697+
if(value == null) {
1698+
return new JSONException(
1699+
"JSONArray[" + idx + "] is not a " + valueType + " (null)."
1700+
, cause);
1701+
}
1702+
// don't try to toString collections or known object types that could be large.
1703+
if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) {
1704+
return new JSONException(
1705+
"JSONArray[" + idx + "] is not a " + valueType + " (" + value.getClass() + ")."
1706+
, cause);
1707+
}
17111708
return new JSONException(
1712-
"JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
1709+
"JSONArray[" + idx + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
17131710
, cause);
17141711
}
17151712

src/main/java/org/json/JSONObject.java

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONExce
609609
// JSONException should really take a throwable argument.
610610
// If it did, I would re-implement this with the Enum.valueOf
611611
// method and place any thrown exception in the JSONException
612-
throw wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), null);
612+
throw wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), opt(key), null);
613613
}
614614
return val;
615615
}
@@ -635,7 +635,7 @@ public boolean getBoolean(String key) throws JSONException {
635635
.equalsIgnoreCase("true"))) {
636636
return true;
637637
}
638-
throw wrongValueFormatException(key, "Boolean", null);
638+
throw wrongValueFormatException(key, "Boolean", object, null);
639639
}
640640

641641
/**
@@ -697,7 +697,7 @@ public double getDouble(String key) throws JSONException {
697697
try {
698698
return Double.parseDouble(object.toString());
699699
} catch (Exception e) {
700-
throw wrongValueFormatException(key, "double", e);
700+
throw wrongValueFormatException(key, "double", object, e);
701701
}
702702
}
703703

@@ -719,7 +719,7 @@ public float getFloat(String key) throws JSONException {
719719
try {
720720
return Float.parseFloat(object.toString());
721721
} catch (Exception e) {
722-
throw wrongValueFormatException(key, "float", e);
722+
throw wrongValueFormatException(key, "float", object, e);
723723
}
724724
}
725725

@@ -741,7 +741,7 @@ public Number getNumber(String key) throws JSONException {
741741
}
742742
return stringToNumber(object.toString());
743743
} catch (Exception e) {
744-
throw wrongValueFormatException(key, "number", e);
744+
throw wrongValueFormatException(key, "number", object, e);
745745
}
746746
}
747747

@@ -763,7 +763,7 @@ public int getInt(String key) throws JSONException {
763763
try {
764764
return Integer.parseInt(object.toString());
765765
} catch (Exception e) {
766-
throw wrongValueFormatException(key, "int", e);
766+
throw wrongValueFormatException(key, "int", object, e);
767767
}
768768
}
769769

@@ -781,7 +781,7 @@ public JSONArray getJSONArray(String key) throws JSONException {
781781
if (object instanceof JSONArray) {
782782
return (JSONArray) object;
783783
}
784-
throw wrongValueFormatException(key, "JSONArray", null);
784+
throw wrongValueFormatException(key, "JSONArray", object, null);
785785
}
786786

787787
/**
@@ -798,7 +798,7 @@ public JSONObject getJSONObject(String key) throws JSONException {
798798
if (object instanceof JSONObject) {
799799
return (JSONObject) object;
800800
}
801-
throw wrongValueFormatException(key, "JSONObject", null);
801+
throw wrongValueFormatException(key, "JSONObject", object, null);
802802
}
803803

804804
/**
@@ -819,7 +819,7 @@ public long getLong(String key) throws JSONException {
819819
try {
820820
return Long.parseLong(object.toString());
821821
} catch (Exception e) {
822-
throw wrongValueFormatException(key, "long", e);
822+
throw wrongValueFormatException(key, "long", object, e);
823823
}
824824
}
825825

@@ -875,7 +875,7 @@ public String getString(String key) throws JSONException {
875875
if (object instanceof String) {
876876
return (String) object;
877877
}
878-
throw wrongValueFormatException(key, "string", null);
878+
throw wrongValueFormatException(key, "string", object, null);
879879
}
880880

881881
/**
@@ -1201,12 +1201,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
12011201
}
12021202
if (exact) {
12031203
return new BigDecimal(((Number)val).doubleValue());
1204-
}else {
1205-
// use the string constructor so that we maintain "nice" values for doubles and floats
1206-
// the double constructor will translate doubles to "exact" values instead of the likely
1207-
// intended representation
1208-
return new BigDecimal(val.toString());
12091204
}
1205+
// use the string constructor so that we maintain "nice" values for doubles and floats
1206+
// the double constructor will translate doubles to "exact" values instead of the likely
1207+
// intended representation
1208+
return new BigDecimal(val.toString());
12101209
}
12111210
if (val instanceof Long || val instanceof Integer
12121211
|| val instanceof Short || val instanceof Byte){
@@ -2021,6 +2020,7 @@ public Object optQuery(JSONPointer jsonPointer) {
20212020
* A String
20222021
* @return A String correctly formatted for insertion in a JSON text.
20232022
*/
2023+
@SuppressWarnings("resource")
20242024
public static String quote(String string) {
20252025
StringWriter sw = new StringWriter();
20262026
synchronized (sw.getBuffer()) {
@@ -2141,7 +2141,7 @@ public boolean similar(Object other) {
21412141
} else if (valueThis instanceof Number && valueOther instanceof Number) {
21422142
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
21432143
return false;
2144-
};
2144+
}
21452145
} else if (!valueThis.equals(valueOther)) {
21462146
return false;
21472147
}
@@ -2409,6 +2409,7 @@ public String toString() {
24092409
* @throws JSONException
24102410
* If the object contains an invalid number.
24112411
*/
2412+
@SuppressWarnings("resource")
24122413
public String toString(int indentFactor) throws JSONException {
24132414
StringWriter w = new StringWriter();
24142415
synchronized (w.getBuffer()) {
@@ -2502,9 +2503,7 @@ private static Object wrap(Object object, Set<Object> objectsRecord) {
25022503
if (objectsRecord != null) {
25032504
return new JSONObject(object, objectsRecord);
25042505
}
2505-
else {
2506-
return new JSONObject(object);
2507-
}
2506+
return new JSONObject(object);
25082507
}
25092508
catch (JSONException exception) {
25102509
throw exception;
@@ -2527,6 +2526,7 @@ public Writer write(Writer writer) throws JSONException {
25272526
return this.write(writer, 0, 0);
25282527
}
25292528

2529+
@SuppressWarnings("resource")
25302530
static final Writer writeValue(Writer writer, Object value,
25312531
int indentFactor, int indent) throws JSONException, IOException {
25322532
if (value == null || value.equals(null)) {
@@ -2604,6 +2604,7 @@ static final void indent(Writer writer, int indent) throws IOException {
26042604
* @throws JSONException if a called function has an error or a write error
26052605
* occurs
26062606
*/
2607+
@SuppressWarnings("resource")
26072608
public Writer write(Writer writer, int indentFactor, int indent)
26082609
throws JSONException {
26092610
try {
@@ -2686,22 +2687,6 @@ public Map<String, Object> toMap() {
26862687
return results;
26872688
}
26882689

2689-
/**
2690-
* Create a new JSONException in a common format for incorrect conversions.
2691-
* @param key name of the key
2692-
* @param valueType the type of value being coerced to
2693-
* @param cause optional cause of the coercion failure
2694-
* @return JSONException that can be thrown.
2695-
*/
2696-
private static JSONException wrongValueFormatException(
2697-
String key,
2698-
String valueType,
2699-
Throwable cause) {
2700-
return new JSONException(
2701-
"JSONObject[" + quote(key) + "] is not a " + valueType + "."
2702-
, cause);
2703-
}
2704-
27052690
/**
27062691
* Create a new JSONException in a common format for incorrect conversions.
27072692
* @param key name of the key
@@ -2714,8 +2699,20 @@ private static JSONException wrongValueFormatException(
27142699
String valueType,
27152700
Object value,
27162701
Throwable cause) {
2702+
if(value == null) {
2703+
2704+
return new JSONException(
2705+
"JSONObject[" + quote(key) + "] is not a " + valueType + " (null)."
2706+
, cause);
2707+
}
2708+
// don't try to toString collections or known object types that could be large.
2709+
if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) {
2710+
return new JSONException(
2711+
"JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")."
2712+
, cause);
2713+
}
27172714
return new JSONException(
2718-
"JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value + ")."
2715+
"JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")."
27192716
, cause);
27202717
}
27212718

src/main/java/org/json/XML.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ of this software and associated documentation files (the "Software"), to deal
2626

2727
import java.io.Reader;
2828
import java.io.StringReader;
29-
import java.lang.reflect.Method;
3029
import java.math.BigDecimal;
3130
import java.math.BigInteger;
3231
import java.util.Iterator;

0 commit comments

Comments
 (0)