@@ -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
0 commit comments