@@ -609,7 +609,7 @@ public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONExce
609
609
// JSONException should really take a throwable argument.
610
610
// If it did, I would re-implement this with the Enum.valueOf
611
611
// 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 );
613
613
}
614
614
return val ;
615
615
}
@@ -635,7 +635,7 @@ public boolean getBoolean(String key) throws JSONException {
635
635
.equalsIgnoreCase ("true" ))) {
636
636
return true ;
637
637
}
638
- throw wrongValueFormatException (key , "Boolean" , null );
638
+ throw wrongValueFormatException (key , "Boolean" , object , null );
639
639
}
640
640
641
641
/**
@@ -697,7 +697,7 @@ public double getDouble(String key) throws JSONException {
697
697
try {
698
698
return Double .parseDouble (object .toString ());
699
699
} catch (Exception e ) {
700
- throw wrongValueFormatException (key , "double" , e );
700
+ throw wrongValueFormatException (key , "double" , object , e );
701
701
}
702
702
}
703
703
@@ -719,7 +719,7 @@ public float getFloat(String key) throws JSONException {
719
719
try {
720
720
return Float .parseFloat (object .toString ());
721
721
} catch (Exception e ) {
722
- throw wrongValueFormatException (key , "float" , e );
722
+ throw wrongValueFormatException (key , "float" , object , e );
723
723
}
724
724
}
725
725
@@ -741,7 +741,7 @@ public Number getNumber(String key) throws JSONException {
741
741
}
742
742
return stringToNumber (object .toString ());
743
743
} catch (Exception e ) {
744
- throw wrongValueFormatException (key , "number" , e );
744
+ throw wrongValueFormatException (key , "number" , object , e );
745
745
}
746
746
}
747
747
@@ -763,7 +763,7 @@ public int getInt(String key) throws JSONException {
763
763
try {
764
764
return Integer .parseInt (object .toString ());
765
765
} catch (Exception e ) {
766
- throw wrongValueFormatException (key , "int" , e );
766
+ throw wrongValueFormatException (key , "int" , object , e );
767
767
}
768
768
}
769
769
@@ -781,7 +781,7 @@ public JSONArray getJSONArray(String key) throws JSONException {
781
781
if (object instanceof JSONArray ) {
782
782
return (JSONArray ) object ;
783
783
}
784
- throw wrongValueFormatException (key , "JSONArray" , null );
784
+ throw wrongValueFormatException (key , "JSONArray" , object , null );
785
785
}
786
786
787
787
/**
@@ -798,7 +798,7 @@ public JSONObject getJSONObject(String key) throws JSONException {
798
798
if (object instanceof JSONObject ) {
799
799
return (JSONObject ) object ;
800
800
}
801
- throw wrongValueFormatException (key , "JSONObject" , null );
801
+ throw wrongValueFormatException (key , "JSONObject" , object , null );
802
802
}
803
803
804
804
/**
@@ -819,7 +819,7 @@ public long getLong(String key) throws JSONException {
819
819
try {
820
820
return Long .parseLong (object .toString ());
821
821
} catch (Exception e ) {
822
- throw wrongValueFormatException (key , "long" , e );
822
+ throw wrongValueFormatException (key , "long" , object , e );
823
823
}
824
824
}
825
825
@@ -875,7 +875,7 @@ public String getString(String key) throws JSONException {
875
875
if (object instanceof String ) {
876
876
return (String ) object ;
877
877
}
878
- throw wrongValueFormatException (key , "string" , null );
878
+ throw wrongValueFormatException (key , "string" , object , null );
879
879
}
880
880
881
881
/**
@@ -1201,12 +1201,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
1201
1201
}
1202
1202
if (exact ) {
1203
1203
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 ());
1209
1204
}
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 ());
1210
1209
}
1211
1210
if (val instanceof Long || val instanceof Integer
1212
1211
|| val instanceof Short || val instanceof Byte ){
@@ -2021,6 +2020,7 @@ public Object optQuery(JSONPointer jsonPointer) {
2021
2020
* A String
2022
2021
* @return A String correctly formatted for insertion in a JSON text.
2023
2022
*/
2023
+ @ SuppressWarnings ("resource" )
2024
2024
public static String quote (String string ) {
2025
2025
StringWriter sw = new StringWriter ();
2026
2026
synchronized (sw .getBuffer ()) {
@@ -2141,7 +2141,7 @@ public boolean similar(Object other) {
2141
2141
} else if (valueThis instanceof Number && valueOther instanceof Number ) {
2142
2142
if (!isNumberSimilar ((Number )valueThis , (Number )valueOther )) {
2143
2143
return false ;
2144
- };
2144
+ }
2145
2145
} else if (!valueThis .equals (valueOther )) {
2146
2146
return false ;
2147
2147
}
@@ -2409,6 +2409,7 @@ public String toString() {
2409
2409
* @throws JSONException
2410
2410
* If the object contains an invalid number.
2411
2411
*/
2412
+ @ SuppressWarnings ("resource" )
2412
2413
public String toString (int indentFactor ) throws JSONException {
2413
2414
StringWriter w = new StringWriter ();
2414
2415
synchronized (w .getBuffer ()) {
@@ -2502,9 +2503,7 @@ private static Object wrap(Object object, Set<Object> objectsRecord) {
2502
2503
if (objectsRecord != null ) {
2503
2504
return new JSONObject (object , objectsRecord );
2504
2505
}
2505
- else {
2506
- return new JSONObject (object );
2507
- }
2506
+ return new JSONObject (object );
2508
2507
}
2509
2508
catch (JSONException exception ) {
2510
2509
throw exception ;
@@ -2527,6 +2526,7 @@ public Writer write(Writer writer) throws JSONException {
2527
2526
return this .write (writer , 0 , 0 );
2528
2527
}
2529
2528
2529
+ @ SuppressWarnings ("resource" )
2530
2530
static final Writer writeValue (Writer writer , Object value ,
2531
2531
int indentFactor , int indent ) throws JSONException , IOException {
2532
2532
if (value == null || value .equals (null )) {
@@ -2604,6 +2604,7 @@ static final void indent(Writer writer, int indent) throws IOException {
2604
2604
* @throws JSONException if a called function has an error or a write error
2605
2605
* occurs
2606
2606
*/
2607
+ @ SuppressWarnings ("resource" )
2607
2608
public Writer write (Writer writer , int indentFactor , int indent )
2608
2609
throws JSONException {
2609
2610
try {
@@ -2686,22 +2687,6 @@ public Map<String, Object> toMap() {
2686
2687
return results ;
2687
2688
}
2688
2689
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
-
2705
2690
/**
2706
2691
* Create a new JSONException in a common format for incorrect conversions.
2707
2692
* @param key name of the key
@@ -2714,8 +2699,20 @@ private static JSONException wrongValueFormatException(
2714
2699
String valueType ,
2715
2700
Object value ,
2716
2701
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
+ }
2717
2714
return new JSONException (
2718
- "JSONObject[" + quote (key ) + "] is not a " + valueType + " (" + value + ")."
2715
+ "JSONObject[" + quote (key ) + "] is not a " + valueType + " (" + value . getClass () + " : " + value + ")."
2719
2716
, cause );
2720
2717
}
2721
2718
0 commit comments