diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 7e95a96a8..2b33e1db8 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -288,7 +288,7 @@ public boolean getBoolean(int index) throws JSONException { .equalsIgnoreCase("true"))) { return true; } - throw wrongValueFormatException(index, "boolean", null); + throw wrongValueFormatException(index, "boolean", object, null); } /** @@ -309,7 +309,7 @@ public double getDouble(int index) throws JSONException { try { return Double.parseDouble(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(index, "double", e); + throw wrongValueFormatException(index, "double", object, e); } } @@ -331,7 +331,7 @@ public float getFloat(int index) throws JSONException { try { return Float.parseFloat(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(index, "float", e); + throw wrongValueFormatException(index, "float", object, e); } } @@ -353,7 +353,7 @@ public Number getNumber(int index) throws JSONException { } return JSONObject.stringToNumber(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(index, "number", e); + throw wrongValueFormatException(index, "number", object, e); } } @@ -378,7 +378,7 @@ public > E getEnum(Class clazz, int index) throws JSONExcep // If it did, I would re-implement this with the Enum.valueOf // method and place any thrown exception in the JSONException throw wrongValueFormatException(index, "enum of type " - + JSONObject.quote(clazz.getSimpleName()), null); + + JSONObject.quote(clazz.getSimpleName()), opt(index), null); } return val; } @@ -441,7 +441,7 @@ public int getInt(int index) throws JSONException { try { return Integer.parseInt(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(index, "int", e); + throw wrongValueFormatException(index, "int", object, e); } } @@ -460,7 +460,7 @@ public JSONArray getJSONArray(int index) throws JSONException { if (object instanceof JSONArray) { return (JSONArray) object; } - throw wrongValueFormatException(index, "JSONArray", null); + throw wrongValueFormatException(index, "JSONArray", object, null); } /** @@ -478,7 +478,7 @@ public JSONObject getJSONObject(int index) throws JSONException { if (object instanceof JSONObject) { return (JSONObject) object; } - throw wrongValueFormatException(index, "JSONObject", null); + throw wrongValueFormatException(index, "JSONObject", object, null); } /** @@ -499,7 +499,7 @@ public long getLong(int index) throws JSONException { try { return Long.parseLong(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(index, "long", e); + throw wrongValueFormatException(index, "long", object, e); } } @@ -517,7 +517,7 @@ public String getString(int index) throws JSONException { if (object instanceof String) { return (String) object; } - throw wrongValueFormatException(index, "String", null); + throw wrongValueFormatException(index, "String", object, null); } /** @@ -1464,6 +1464,7 @@ public String toString() { *  (right bracket). * @throws JSONException if a called function fails */ + @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { @@ -1513,6 +1514,7 @@ public Writer write(Writer writer) throws JSONException { * @return The writer. * @throws JSONException if a called function fails or unable to write */ + @SuppressWarnings("resource") public Writer write(Writer writer, int indentFactor, int indent) throws JSONException { try { @@ -1680,22 +1682,6 @@ private void addAll(Object array, boolean wrap) throws JSONException { } } - /** - * 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 @@ -1708,8 +1694,19 @@ private static JSONException wrongValueFormatException( String valueType, Object value, Throwable cause) { + if(value == null) { + return new JSONException( + "JSONArray[" + idx + "] is not a " + valueType + " (null)." + , cause); + } + // don't try to toString collections or known object types that could be large. + if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) { + return new JSONException( + "JSONArray[" + idx + "] is not a " + valueType + " (" + value.getClass() + ")." + , cause); + } return new JSONException( - "JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")." + "JSONArray[" + idx + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")." , cause); } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5c37249e3..ea549e3f7 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -609,7 +609,7 @@ public > E getEnum(Class clazz, String key) throws JSONExce // 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 wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), null); + throw wrongValueFormatException(key, "enum of type " + quote(clazz.getSimpleName()), opt(key), null); } return val; } @@ -635,7 +635,7 @@ public boolean getBoolean(String key) throws JSONException { .equalsIgnoreCase("true"))) { return true; } - throw wrongValueFormatException(key, "Boolean", null); + throw wrongValueFormatException(key, "Boolean", object, null); } /** @@ -697,7 +697,7 @@ public double getDouble(String key) throws JSONException { try { return Double.parseDouble(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(key, "double", e); + throw wrongValueFormatException(key, "double", object, e); } } @@ -719,7 +719,7 @@ public float getFloat(String key) throws JSONException { try { return Float.parseFloat(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(key, "float", e); + throw wrongValueFormatException(key, "float", object, e); } } @@ -741,7 +741,7 @@ public Number getNumber(String key) throws JSONException { } return stringToNumber(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(key, "number", e); + throw wrongValueFormatException(key, "number", object, e); } } @@ -763,7 +763,7 @@ public int getInt(String key) throws JSONException { try { return Integer.parseInt(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(key, "int", e); + throw wrongValueFormatException(key, "int", object, e); } } @@ -781,7 +781,7 @@ public JSONArray getJSONArray(String key) throws JSONException { if (object instanceof JSONArray) { return (JSONArray) object; } - throw wrongValueFormatException(key, "JSONArray", null); + throw wrongValueFormatException(key, "JSONArray", object, null); } /** @@ -798,7 +798,7 @@ public JSONObject getJSONObject(String key) throws JSONException { if (object instanceof JSONObject) { return (JSONObject) object; } - throw wrongValueFormatException(key, "JSONObject", null); + throw wrongValueFormatException(key, "JSONObject", object, null); } /** @@ -819,7 +819,7 @@ public long getLong(String key) throws JSONException { try { return Long.parseLong(object.toString()); } catch (Exception e) { - throw wrongValueFormatException(key, "long", e); + throw wrongValueFormatException(key, "long", object, e); } } @@ -875,7 +875,7 @@ public String getString(String key) throws JSONException { if (object instanceof String) { return (String) object; } - throw wrongValueFormatException(key, "string", null); + throw wrongValueFormatException(key, "string", object, null); } /** @@ -1201,12 +1201,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea } if (exact) { return new BigDecimal(((Number)val).doubleValue()); - }else { - // use the string constructor so that we maintain "nice" values for doubles and floats - // the double constructor will translate doubles to "exact" values instead of the likely - // intended representation - return new BigDecimal(val.toString()); } + // use the string constructor so that we maintain "nice" values for doubles and floats + // the double constructor will translate doubles to "exact" values instead of the likely + // intended representation + return new BigDecimal(val.toString()); } if (val instanceof Long || val instanceof Integer || val instanceof Short || val instanceof Byte){ @@ -2021,6 +2020,7 @@ public Object optQuery(JSONPointer jsonPointer) { * A String * @return A String correctly formatted for insertion in a JSON text. */ + @SuppressWarnings("resource") public static String quote(String string) { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { @@ -2141,7 +2141,7 @@ public boolean similar(Object other) { } else if (valueThis instanceof Number && valueOther instanceof Number) { if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) { return false; - }; + } } else if (!valueThis.equals(valueOther)) { return false; } @@ -2409,6 +2409,7 @@ public String toString() { * @throws JSONException * If the object contains an invalid number. */ + @SuppressWarnings("resource") public String toString(int indentFactor) throws JSONException { StringWriter w = new StringWriter(); synchronized (w.getBuffer()) { @@ -2502,9 +2503,7 @@ private static Object wrap(Object object, Set objectsRecord) { if (objectsRecord != null) { return new JSONObject(object, objectsRecord); } - else { - return new JSONObject(object); - } + return new JSONObject(object); } catch (JSONException exception) { throw exception; @@ -2527,6 +2526,7 @@ public Writer write(Writer writer) throws JSONException { return this.write(writer, 0, 0); } + @SuppressWarnings("resource") static final Writer writeValue(Writer writer, Object value, int indentFactor, int indent) throws JSONException, IOException { if (value == null || value.equals(null)) { @@ -2604,6 +2604,7 @@ static final void indent(Writer writer, int indent) throws IOException { * @throws JSONException if a called function has an error or a write error * occurs */ + @SuppressWarnings("resource") public Writer write(Writer writer, int indentFactor, int indent) throws JSONException { try { @@ -2686,22 +2687,6 @@ public Map toMap() { return results; } - /** - * Create a new JSONException in a common format for incorrect conversions. - * @param key name of the key - * @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( - String key, - String valueType, - Throwable cause) { - return new JSONException( - "JSONObject[" + quote(key) + "] is not a " + valueType + "." - , cause); - } - /** * Create a new JSONException in a common format for incorrect conversions. * @param key name of the key @@ -2714,8 +2699,20 @@ private static JSONException wrongValueFormatException( String valueType, Object value, Throwable cause) { + if(value == null) { + + return new JSONException( + "JSONObject[" + quote(key) + "] is not a " + valueType + " (null)." + , cause); + } + // don't try to toString collections or known object types that could be large. + if(value instanceof Map || value instanceof Iterable || value instanceof JSONObject) { + return new JSONException( + "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + ")." + , cause); + } return new JSONException( - "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value + ")." + "JSONObject[" + quote(key) + "] is not a " + valueType + " (" + value.getClass() + " : " + value + ")." , cause); } diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 9b2ba8939..33838a15c 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -26,7 +26,6 @@ of this software and associated documentation files (the "Software"), to deal import java.io.Reader; import java.io.StringReader; -import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Iterator; diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 2e3263295..987891e12 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -412,7 +412,7 @@ public void failedGetArrayValues() { assertTrue("expected getBoolean to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a boolean.",e.getMessage()); + "JSONArray[4] is not a boolean (class java.lang.String : hello).",e.getMessage()); } try { jsonArray.get(-1); @@ -426,42 +426,42 @@ public void failedGetArrayValues() { assertTrue("expected getDouble to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a double.",e.getMessage()); + "JSONArray[4] is not a double (class java.lang.String : hello).",e.getMessage()); } try { jsonArray.getInt(4); assertTrue("expected getInt to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a int.",e.getMessage()); + "JSONArray[4] is not a int (class java.lang.String : hello).",e.getMessage()); } try { jsonArray.getJSONArray(4); assertTrue("expected getJSONArray to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a JSONArray.",e.getMessage()); + "JSONArray[4] is not a JSONArray (class java.lang.String : hello).",e.getMessage()); } try { jsonArray.getJSONObject(4); assertTrue("expected getJSONObject to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a JSONObject.",e.getMessage()); + "JSONArray[4] is not a JSONObject (class java.lang.String : hello).",e.getMessage()); } try { jsonArray.getLong(4); assertTrue("expected getLong to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[4] is not a long.",e.getMessage()); + "JSONArray[4] is not a long (class java.lang.String : hello).",e.getMessage()); } try { jsonArray.getString(5); assertTrue("expected getString to fail", false); } catch (JSONException e) { assertEquals("Expected an exception message", - "JSONArray[5] is not a String.",e.getMessage()); + "JSONArray[5] is not a String (class java.math.BigDecimal : 0.002345).",e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONMLTest.java b/src/test/java/org/json/junit/JSONMLTest.java index 8f3de42cf..390cbd82e 100644 --- a/src/test/java/org/json/junit/JSONMLTest.java +++ b/src/test/java/org/json/junit/JSONMLTest.java @@ -158,7 +158,7 @@ public void emptyTagException() { assertTrue("Expecting an exception", false); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONArray[0] is not a String.", + "JSONArray[0] is not a String (class org.json.JSONArray).", e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index b91fccb9b..2ba58bf6e 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1090,7 +1090,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a Boolean.", + "JSONObject[\"stringKey\"] is not a Boolean (class java.lang.String : hello world!).", e.getMessage()); } try { @@ -1106,7 +1106,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"trueKey\"] is not a string.", + "JSONObject[\"trueKey\"] is not a string (class java.lang.Boolean : true).", e.getMessage()); } try { @@ -1122,7 +1122,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a double.", + "JSONObject[\"stringKey\"] is not a double (class java.lang.String : hello world!).", e.getMessage()); } try { @@ -1138,7 +1138,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a float.", + "JSONObject[\"stringKey\"] is not a float (class java.lang.String : hello world!).", e.getMessage()); } try { @@ -1154,7 +1154,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a int.", + "JSONObject[\"stringKey\"] is not a int (class java.lang.String : hello world!).", e.getMessage()); } try { @@ -1170,7 +1170,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a long.", + "JSONObject[\"stringKey\"] is not a long (class java.lang.String : hello world!).", e.getMessage()); } try { @@ -1186,7 +1186,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONArray.", + "JSONObject[\"stringKey\"] is not a JSONArray (class java.lang.String : hello world!).", e.getMessage()); } try { @@ -1202,7 +1202,7 @@ public void jsonObjectNonAndWrongValues() { fail("Expected an exception"); } catch (JSONException e) { assertEquals("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONObject.", + "JSONObject[\"stringKey\"] is not a JSONObject (class java.lang.String : hello world!).", e.getMessage()); } }