@@ -249,7 +249,7 @@ public boolean getBoolean(int index) throws JSONException {
249
249
.equalsIgnoreCase ("true" ))) {
250
250
return true ;
251
251
}
252
- throw new JSONException ( "JSONArray[" + index + "] is not a boolean." );
252
+ throw wrongValueFormatException ( index , " boolean" , null );
253
253
}
254
254
255
255
/**
@@ -263,7 +263,15 @@ public boolean getBoolean(int index) throws JSONException {
263
263
* to a number.
264
264
*/
265
265
public double getDouble (int index ) throws JSONException {
266
- return this .getNumber (index ).doubleValue ();
266
+ final Object object = this .get (index );
267
+ if (object instanceof Number ) {
268
+ return ((Number )object ).doubleValue ();
269
+ }
270
+ try {
271
+ return Double .parseDouble (object .toString ());
272
+ } catch (Exception e ) {
273
+ throw wrongValueFormatException (index , "double" , e );
274
+ }
267
275
}
268
276
269
277
/**
@@ -277,7 +285,15 @@ public double getDouble(int index) throws JSONException {
277
285
* object and cannot be converted to a number.
278
286
*/
279
287
public float getFloat (int index ) throws JSONException {
280
- return this .getNumber (index ).floatValue ();
288
+ final Object object = this .get (index );
289
+ if (object instanceof Number ) {
290
+ return ((Float )object ).floatValue ();
291
+ }
292
+ try {
293
+ return Float .parseFloat (object .toString ());
294
+ } catch (Exception e ) {
295
+ throw wrongValueFormatException (index , "float" , e );
296
+ }
281
297
}
282
298
283
299
/**
@@ -298,7 +314,7 @@ public Number getNumber(int index) throws JSONException {
298
314
}
299
315
return JSONObject .stringToNumber (object .toString ());
300
316
} catch (Exception e ) {
301
- throw new JSONException ( "JSONArray[" + index + "] is not a number. " , e );
317
+ throw wrongValueFormatException ( index , " number" , e );
302
318
}
303
319
}
304
320
@@ -322,8 +338,8 @@ public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONExcep
322
338
// JSONException should really take a throwable argument.
323
339
// If it did, I would re-implement this with the Enum.valueOf
324
340
// method and place any thrown exception in the JSONException
325
- throw new JSONException ( "JSONArray[" + index + "] is not an enum of type "
326
- + JSONObject .quote (clazz .getSimpleName ()) + "." );
341
+ throw wrongValueFormatException ( index , " enum of type "
342
+ + JSONObject .quote (clazz .getSimpleName ()), null );
327
343
}
328
344
return val ;
329
345
}
@@ -345,8 +361,7 @@ public BigDecimal getBigDecimal (int index) throws JSONException {
345
361
Object object = this .get (index );
346
362
BigDecimal val = JSONObject .objectToBigDecimal (object , null );
347
363
if (val == null ) {
348
- throw new JSONException ("JSONArray[" + index +
349
- "] could not convert to BigDecimal (" + object + ")." );
364
+ throw wrongValueFormatException (index , "BigDecimal" , object , null );
350
365
}
351
366
return val ;
352
367
}
@@ -365,8 +380,7 @@ public BigInteger getBigInteger (int index) throws JSONException {
365
380
Object object = this .get (index );
366
381
BigInteger val = JSONObject .objectToBigInteger (object , null );
367
382
if (val == null ) {
368
- throw new JSONException ("JSONArray[" + index +
369
- "] could not convert to BigDecimal (" + object + ")." );
383
+ throw wrongValueFormatException (index , "BigInteger" , object , null );
370
384
}
371
385
return val ;
372
386
}
@@ -381,7 +395,15 @@ public BigInteger getBigInteger (int index) throws JSONException {
381
395
* If the key is not found or if the value is not a number.
382
396
*/
383
397
public int getInt (int index ) throws JSONException {
384
- return this .getNumber (index ).intValue ();
398
+ final Object object = this .get (index );
399
+ if (object instanceof Number ) {
400
+ return ((Number )object ).intValue ();
401
+ }
402
+ try {
403
+ return Integer .parseInt (object .toString ());
404
+ } catch (Exception e ) {
405
+ throw wrongValueFormatException (index , "int" , e );
406
+ }
385
407
}
386
408
387
409
/**
@@ -399,7 +421,7 @@ public JSONArray getJSONArray(int index) throws JSONException {
399
421
if (object instanceof JSONArray ) {
400
422
return (JSONArray ) object ;
401
423
}
402
- throw new JSONException ( "JSONArray[" + index + "] is not a JSONArray." );
424
+ throw wrongValueFormatException ( index , " JSONArray" , null );
403
425
}
404
426
405
427
/**
@@ -417,7 +439,7 @@ public JSONObject getJSONObject(int index) throws JSONException {
417
439
if (object instanceof JSONObject ) {
418
440
return (JSONObject ) object ;
419
441
}
420
- throw new JSONException ( "JSONArray[" + index + "] is not a JSONObject." );
442
+ throw wrongValueFormatException ( index , " JSONObject" , null );
421
443
}
422
444
423
445
/**
@@ -431,7 +453,15 @@ public JSONObject getJSONObject(int index) throws JSONException {
431
453
* to a number.
432
454
*/
433
455
public long getLong (int index ) throws JSONException {
434
- return this .getNumber (index ).longValue ();
456
+ final Object object = this .get (index );
457
+ if (object instanceof Number ) {
458
+ return ((Number )object ).longValue ();
459
+ }
460
+ try {
461
+ return Long .parseLong (object .toString ());
462
+ } catch (Exception e ) {
463
+ throw wrongValueFormatException (index , "long" , e );
464
+ }
435
465
}
436
466
437
467
/**
@@ -448,7 +478,7 @@ public String getString(int index) throws JSONException {
448
478
if (object instanceof String ) {
449
479
return (String ) object ;
450
480
}
451
- throw new JSONException ( "JSONArray[" + index + "] not a string." );
481
+ throw wrongValueFormatException ( index , "String" , null );
452
482
}
453
483
454
484
/**
@@ -1454,5 +1484,38 @@ public List<Object> toList() {
1454
1484
public boolean isEmpty () {
1455
1485
return this .myArrayList .isEmpty ();
1456
1486
}
1487
+
1488
+ /**
1489
+ * Create a new JSONException in a common format for incorrect conversions.
1490
+ * @param idx index of the item
1491
+ * @param valueType the type of value being coerced to
1492
+ * @param cause optional cause of the coercion failure
1493
+ * @return JSONException that can be thrown.
1494
+ */
1495
+ private static JSONException wrongValueFormatException (
1496
+ int idx ,
1497
+ String valueType ,
1498
+ Throwable cause ) {
1499
+ return new JSONException (
1500
+ "JSONArray[" + idx + "] is not a " + valueType + "."
1501
+ , cause );
1502
+ }
1503
+
1504
+ /**
1505
+ * Create a new JSONException in a common format for incorrect conversions.
1506
+ * @param idx index of the item
1507
+ * @param valueType the type of value being coerced to
1508
+ * @param cause optional cause of the coercion failure
1509
+ * @return JSONException that can be thrown.
1510
+ */
1511
+ private static JSONException wrongValueFormatException (
1512
+ int idx ,
1513
+ String valueType ,
1514
+ Object value ,
1515
+ Throwable cause ) {
1516
+ return new JSONException (
1517
+ "JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
1518
+ , cause );
1519
+ }
1457
1520
1458
1521
}
0 commit comments