Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates value error messages to be consistent. #675

Merged
merged 2 commits into from Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 24 additions & 27 deletions src/main/java/org/json/JSONArray.java
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -378,7 +378,7 @@ public <E extends Enum<E>> E getEnum(Class<E> 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;
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -1464,6 +1464,7 @@ public String toString() {
* &nbsp;<small>(right bracket)</small>.
* @throws JSONException if a called function fails
*/
@SuppressWarnings("resource")
public String toString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
69 changes: 33 additions & 36 deletions src/main/java/org/json/JSONObject.java
Expand Up @@ -609,7 +609,7 @@ public <E extends Enum<E>> E getEnum(Class<E> 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;
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -2502,9 +2503,7 @@ private static Object wrap(Object object, Set<Object> objectsRecord) {
if (objectsRecord != null) {
return new JSONObject(object, objectsRecord);
}
else {
return new JSONObject(object);
}
return new JSONObject(object);
}
catch (JSONException exception) {
throw exception;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -2686,22 +2687,6 @@ public Map<String, Object> 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
Expand All @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/json/XML.java
Expand Up @@ -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;
Expand Down