mirror of
https://github.com/stleary/JSON-java.git
synced 2026-03-17 00:03:39 -04:00
Fix ClassCastException in JSONML.toJSONArray and toJSONObject
Add type checking before casting parse() results to JSONArray/JSONObject. When parse() returns an unexpected type (e.g., String for malformed input), the code now throws a descriptive JSONException instead of ClassCastException. This prevents unchecked exceptions from propagating to callers who only expect JSONException from these methods. Fixes #1034
This commit is contained in:
@@ -22,6 +22,33 @@ public class JSONML {
|
||||
public JSONML() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely cast parse result to JSONArray with proper type checking.
|
||||
* @param result The result from parse() method
|
||||
* @return JSONArray if result is a JSONArray
|
||||
* @throws JSONException if result is not a JSONArray
|
||||
*/
|
||||
private static JSONArray toJSONArraySafe(Object result) throws JSONException {
|
||||
if (result instanceof JSONArray) {
|
||||
return (JSONArray) result;
|
||||
}
|
||||
throw new JSONException("Expected JSONArray but got " +
|
||||
(result == null ? "null" : result.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely cast parse result to JSONObject with proper type checking.
|
||||
* @param result The result from parse() method
|
||||
* @return JSONObject if result is a JSONObject
|
||||
* @throws JSONException if result is not a JSONObject
|
||||
*/
|
||||
private static JSONObject toJSONObjectSafe(Object result) throws JSONException {
|
||||
if (result instanceof JSONObject) {
|
||||
return (JSONObject) result;
|
||||
}
|
||||
throw new JSONException("Expected JSONObject but got " +
|
||||
(result == null ? "null" : result.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse XML values and store them in a JSONArray.
|
||||
@@ -276,7 +303,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, JSONMLParserConfiguration.ORIGINAL, 0);
|
||||
return toJSONArraySafe(parse(new XMLTokener(string), true, null, JSONMLParserConfiguration.ORIGINAL, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -298,7 +325,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string, boolean keepStrings) throws JSONException {
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, keepStrings, 0);
|
||||
return toJSONArraySafe(parse(new XMLTokener(string), true, null, keepStrings, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -323,7 +350,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONArray)parse(new XMLTokener(string), true, null, config, 0);
|
||||
return toJSONArraySafe(parse(new XMLTokener(string), true, null, config, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -347,7 +374,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null, config, 0);
|
||||
return toJSONArraySafe(parse(x, true, null, config, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -369,7 +396,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null, keepStrings, 0);
|
||||
return toJSONArraySafe(parse(x, true, null, keepStrings, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -386,7 +413,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONArray
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
||||
return (JSONArray)parse(x, true, null, false, 0);
|
||||
return toJSONArraySafe(parse(x, true, null, false, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -404,7 +431,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, false, 0);
|
||||
return toJSONObjectSafe(parse(new XMLTokener(string), false, null, false, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -424,7 +451,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, keepStrings, 0);
|
||||
return toJSONObjectSafe(parse(new XMLTokener(string), false, null, keepStrings, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -446,7 +473,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONObject)parse(new XMLTokener(string), false, null, config, 0);
|
||||
return toJSONObjectSafe(parse(new XMLTokener(string), false, null, config, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -464,7 +491,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null, false, 0);
|
||||
return toJSONObjectSafe(parse(x, false, null, false, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -484,7 +511,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null, keepStrings, 0);
|
||||
return toJSONObjectSafe(parse(x, false, null, keepStrings, 0));
|
||||
}
|
||||
|
||||
|
||||
@@ -506,7 +533,7 @@ public class JSONML {
|
||||
* @throws JSONException Thrown on error converting to a JSONObject
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x, JSONMLParserConfiguration config) throws JSONException {
|
||||
return (JSONObject)parse(x, false, null, config, 0);
|
||||
return toJSONObjectSafe(parse(x, false, null, config, 0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user