@@ -22,6 +22,33 @@ public class JSONML {
2222 public JSONML () {
2323 }
2424
25+ /**
26+ * Safely cast parse result to JSONArray with proper type checking.
27+ * @param result The result from parse() method
28+ * @return JSONArray if result is a JSONArray
29+ * @throws JSONException if result is not a JSONArray
30+ */
31+ private static JSONArray toJSONArraySafe (Object result ) throws JSONException {
32+ if (result instanceof JSONArray ) {
33+ return (JSONArray ) result ;
34+ }
35+ throw new JSONException ("Expected JSONArray but got " +
36+ (result == null ? "null" : result .getClass ().getSimpleName ()));
37+ }
38+
39+ /**
40+ * Safely cast parse result to JSONObject with proper type checking.
41+ * @param result The result from parse() method
42+ * @return JSONObject if result is a JSONObject
43+ * @throws JSONException if result is not a JSONObject
44+ */
45+ private static JSONObject toJSONObjectSafe (Object result ) throws JSONException {
46+ if (result instanceof JSONObject ) {
47+ return (JSONObject ) result ;
48+ }
49+ throw new JSONException ("Expected JSONObject but got " +
50+ (result == null ? "null" : result .getClass ().getSimpleName ()));
51+ }
2552
2653 /**
2754 * Parse XML values and store them in a JSONArray.
@@ -276,7 +303,7 @@ private static Object parse(
276303 * @throws JSONException Thrown on error converting to a JSONArray
277304 */
278305 public static JSONArray toJSONArray (String string ) throws JSONException {
279- return ( JSONArray ) parse (new XMLTokener (string ), true , null , JSONMLParserConfiguration .ORIGINAL , 0 );
306+ return toJSONArraySafe ( parse (new XMLTokener (string ), true , null , JSONMLParserConfiguration .ORIGINAL , 0 ) );
280307 }
281308
282309
@@ -298,7 +325,7 @@ public static JSONArray toJSONArray(String string) throws JSONException {
298325 * @throws JSONException Thrown on error converting to a JSONArray
299326 */
300327 public static JSONArray toJSONArray (String string , boolean keepStrings ) throws JSONException {
301- return ( JSONArray ) parse (new XMLTokener (string ), true , null , keepStrings , 0 );
328+ return toJSONArraySafe ( parse (new XMLTokener (string ), true , null , keepStrings , 0 ) );
302329 }
303330
304331
@@ -323,7 +350,7 @@ public static JSONArray toJSONArray(String string, boolean keepStrings) throws J
323350 * @throws JSONException Thrown on error converting to a JSONArray
324351 */
325352 public static JSONArray toJSONArray (String string , JSONMLParserConfiguration config ) throws JSONException {
326- return ( JSONArray ) parse (new XMLTokener (string ), true , null , config , 0 );
353+ return toJSONArraySafe ( parse (new XMLTokener (string ), true , null , config , 0 ) );
327354 }
328355
329356
@@ -347,7 +374,7 @@ public static JSONArray toJSONArray(String string, JSONMLParserConfiguration con
347374 * @throws JSONException Thrown on error converting to a JSONArray
348375 */
349376 public static JSONArray toJSONArray (XMLTokener x , JSONMLParserConfiguration config ) throws JSONException {
350- return ( JSONArray ) parse (x , true , null , config , 0 );
377+ return toJSONArraySafe ( parse (x , true , null , config , 0 ) );
351378 }
352379
353380
@@ -369,7 +396,7 @@ public static JSONArray toJSONArray(XMLTokener x, JSONMLParserConfiguration conf
369396 * @throws JSONException Thrown on error converting to a JSONArray
370397 */
371398 public static JSONArray toJSONArray (XMLTokener x , boolean keepStrings ) throws JSONException {
372- return ( JSONArray ) parse (x , true , null , keepStrings , 0 );
399+ return toJSONArraySafe ( parse (x , true , null , keepStrings , 0 ) );
373400 }
374401
375402
@@ -386,7 +413,7 @@ public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings) throws JS
386413 * @throws JSONException Thrown on error converting to a JSONArray
387414 */
388415 public static JSONArray toJSONArray (XMLTokener x ) throws JSONException {
389- return ( JSONArray ) parse (x , true , null , false , 0 );
416+ return toJSONArraySafe ( parse (x , true , null , false , 0 ) );
390417 }
391418
392419
@@ -404,7 +431,7 @@ public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
404431 * @throws JSONException Thrown on error converting to a JSONObject
405432 */
406433 public static JSONObject toJSONObject (String string ) throws JSONException {
407- return ( JSONObject ) parse (new XMLTokener (string ), false , null , false , 0 );
434+ return toJSONObjectSafe ( parse (new XMLTokener (string ), false , null , false , 0 ) );
408435 }
409436
410437
@@ -424,7 +451,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
424451 * @throws JSONException Thrown on error converting to a JSONObject
425452 */
426453 public static JSONObject toJSONObject (String string , boolean keepStrings ) throws JSONException {
427- return ( JSONObject ) parse (new XMLTokener (string ), false , null , keepStrings , 0 );
454+ return toJSONObjectSafe ( parse (new XMLTokener (string ), false , null , keepStrings , 0 ) );
428455 }
429456
430457
@@ -446,7 +473,7 @@ public static JSONObject toJSONObject(String string, boolean keepStrings) throws
446473 * @throws JSONException Thrown on error converting to a JSONObject
447474 */
448475 public static JSONObject toJSONObject (String string , JSONMLParserConfiguration config ) throws JSONException {
449- return ( JSONObject ) parse (new XMLTokener (string ), false , null , config , 0 );
476+ return toJSONObjectSafe ( parse (new XMLTokener (string ), false , null , config , 0 ) );
450477 }
451478
452479
@@ -464,7 +491,7 @@ public static JSONObject toJSONObject(String string, JSONMLParserConfiguration c
464491 * @throws JSONException Thrown on error converting to a JSONObject
465492 */
466493 public static JSONObject toJSONObject (XMLTokener x ) throws JSONException {
467- return ( JSONObject ) parse (x , false , null , false , 0 );
494+ return toJSONObjectSafe ( parse (x , false , null , false , 0 ) );
468495 }
469496
470497
@@ -484,7 +511,7 @@ public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
484511 * @throws JSONException Thrown on error converting to a JSONObject
485512 */
486513 public static JSONObject toJSONObject (XMLTokener x , boolean keepStrings ) throws JSONException {
487- return ( JSONObject ) parse (x , false , null , keepStrings , 0 );
514+ return toJSONObjectSafe ( parse (x , false , null , keepStrings , 0 ) );
488515 }
489516
490517
@@ -506,7 +533,7 @@ public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings) throws
506533 * @throws JSONException Thrown on error converting to a JSONObject
507534 */
508535 public static JSONObject toJSONObject (XMLTokener x , JSONMLParserConfiguration config ) throws JSONException {
509- return ( JSONObject ) parse (x , false , null , config , 0 );
536+ return toJSONObjectSafe ( parse (x , false , null , config , 0 ) );
510537 }
511538
512539
0 commit comments