@@ -78,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal
78
78
* </ul>
79
79
*
80
80
* @author JSON.org
81
- * @version 2016-07-19
81
+ * @version 2016-05-20
82
82
*/
83
83
public class JSONArray implements Iterable <Object > {
84
84
@@ -156,9 +156,9 @@ public JSONArray(String source) throws JSONException {
156
156
public JSONArray (Collection <?> collection ) {
157
157
this .myArrayList = new ArrayList <Object >();
158
158
if (collection != null ) {
159
- for (Object o : collection ) {
160
- this .myArrayList .add (JSONObject .wrap (o ));
161
- }
159
+ for (Object o : collection ){
160
+ this .myArrayList .add (JSONObject .wrap (o ));
161
+ }
162
162
}
163
163
}
164
164
@@ -241,15 +241,11 @@ public boolean getBoolean(int index) throws JSONException {
241
241
public double getDouble (int index ) throws JSONException {
242
242
Object object = this .get (index );
243
243
try {
244
- if (object instanceof Number ) {
245
- return ((Number ) object ).doubleValue ();
246
- } else if (object instanceof String ) {
247
- return Double .parseDouble ((String ) object );
248
- }
244
+ return object instanceof Number ? ((Number ) object ).doubleValue ()
245
+ : Double .parseDouble ((String ) object );
249
246
} catch (Exception e ) {
250
-
247
+ throw new JSONException ( "JSONArray[" + index + "] is not a number." );
251
248
}
252
- throw new JSONException ("JSONArray[" + index + "] is not a number." );
253
249
}
254
250
255
251
/**
@@ -329,15 +325,11 @@ public BigInteger getBigInteger (int index) throws JSONException {
329
325
public int getInt (int index ) throws JSONException {
330
326
Object object = this .get (index );
331
327
try {
332
- if (object instanceof Number ) {
333
- return ((Number ) object ).intValue ();
334
- } else if (object instanceof String ) {
335
- return Integer .parseInt ((String ) object );
336
- }
328
+ return object instanceof Number ? ((Number ) object ).intValue ()
329
+ : Integer .parseInt ((String ) object );
337
330
} catch (Exception e ) {
338
-
331
+ throw new JSONException ( "JSONArray[" + index + "] is not a number." );
339
332
}
340
- throw new JSONException ("JSONArray[" + index + "] is not a number." );
341
333
}
342
334
343
335
/**
@@ -389,15 +381,11 @@ public JSONObject getJSONObject(int index) throws JSONException {
389
381
public long getLong (int index ) throws JSONException {
390
382
Object object = this .get (index );
391
383
try {
392
- if (object instanceof Number ) {
393
- return ((Number ) object ).longValue ();
394
- } else if (object instanceof String ) {
395
- return Long .parseLong ((String ) object );
396
- }
384
+ return object instanceof Number ? ((Number ) object ).longValue ()
385
+ : Long .parseLong ((String ) object );
397
386
} catch (Exception e ) {
398
-
387
+ throw new JSONException ( "JSONArray[" + index + "] is not a number." );
399
388
}
400
- throw new JSONException ("JSONArray[" + index + "] is not a number." );
401
389
}
402
390
403
391
/**
@@ -498,20 +486,11 @@ public boolean optBoolean(int index) {
498
486
* @return The truth.
499
487
*/
500
488
public boolean optBoolean (int index , boolean defaultValue ) {
501
- Object object = this .opt (index );
502
- if (JSONObject .NULL .equals (object )) {
489
+ try {
490
+ return this .getBoolean (index );
491
+ } catch (Exception e ) {
503
492
return defaultValue ;
504
493
}
505
- if (object .equals (Boolean .FALSE )
506
- || (object instanceof String && ((String ) object )
507
- .equalsIgnoreCase ("false" ))) {
508
- return false ;
509
- } else if (object .equals (Boolean .TRUE )
510
- || (object instanceof String && ((String ) object )
511
- .equalsIgnoreCase ("true" ))) {
512
- return true ;
513
- }
514
- return defaultValue ;
515
494
}
516
495
517
496
/**
@@ -539,20 +518,11 @@ public double optDouble(int index) {
539
518
* @return The value.
540
519
*/
541
520
public double optDouble (int index , double defaultValue ) {
542
- Object object = this .opt (index );
543
- if (JSONObject .NULL .equals (object )) {
544
- return defaultValue ;
545
- }
546
521
try {
547
- if (object instanceof Number ) {
548
- return ((Number ) object ).doubleValue ();
549
- } else if (object instanceof String ) {
550
- return Double .parseDouble ((String ) object );
551
- }
522
+ return this .getDouble (index );
552
523
} catch (Exception e ) {
553
-
524
+ return defaultValue ;
554
525
}
555
- return defaultValue ;
556
526
}
557
527
558
528
/**
@@ -580,20 +550,11 @@ public int optInt(int index) {
580
550
* @return The value.
581
551
*/
582
552
public int optInt (int index , int defaultValue ) {
583
- Object object = this .opt (index );
584
- if (JSONObject .NULL .equals (object )) {
585
- return defaultValue ;
586
- }
587
553
try {
588
- if (object instanceof Number ) {
589
- return ((Number ) object ).intValue ();
590
- } else if (object instanceof String ) {
591
- return Integer .parseInt ((String ) object );
592
- }
554
+ return this .getInt (index );
593
555
} catch (Exception e ) {
594
-
556
+ return defaultValue ;
595
557
}
596
- return defaultValue ;
597
558
}
598
559
599
560
/**
@@ -654,12 +615,8 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
654
615
* @return The value.
655
616
*/
656
617
public BigInteger optBigInteger (int index , BigInteger defaultValue ) {
657
- Object object = this .opt (index );
658
- if (JSONObject .NULL .equals (object )) {
659
- return defaultValue ;
660
- }
661
618
try {
662
- return new BigInteger ( object . toString () );
619
+ return this . getBigInteger ( index );
663
620
} catch (Exception e ) {
664
621
return defaultValue ;
665
622
}
@@ -677,12 +634,8 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
677
634
* @return The value.
678
635
*/
679
636
public BigDecimal optBigDecimal (int index , BigDecimal defaultValue ) {
680
- Object object = this .opt (index );
681
- if (JSONObject .NULL .equals (object )) {
682
- return defaultValue ;
683
- }
684
637
try {
685
- return new BigDecimal ( object . toString () );
638
+ return this . getBigDecimal ( index );
686
639
} catch (Exception e ) {
687
640
return defaultValue ;
688
641
}
@@ -740,20 +693,11 @@ public long optLong(int index) {
740
693
* @return The value.
741
694
*/
742
695
public long optLong (int index , long defaultValue ) {
743
- Object object = this .opt (index );
744
- if (JSONObject .NULL .equals (object )) {
745
- return defaultValue ;
746
- }
747
696
try {
748
- if (object instanceof Number ) {
749
- return ((Number ) object ).longValue ();
750
- } else if (object instanceof String ) {
751
- return Long .parseLong ((String ) object );
752
- }
697
+ return this .getLong (index );
753
698
} catch (Exception e ) {
754
-
699
+ return defaultValue ;
755
700
}
756
- return defaultValue ;
757
701
}
758
702
759
703
/**
@@ -1017,7 +961,7 @@ public JSONArray put(int index, Object value) throws JSONException {
1017
961
}
1018
962
1019
963
/**
1020
- * Creates a JSONPointer using an initialization string and tries to
964
+ * Creates a JSONPointer using an intialization string and tries to
1021
965
* match it to an item within this JSONArray. For example, given a
1022
966
* JSONArray initialized with this document:
1023
967
* <pre>
@@ -1137,7 +1081,6 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
1137
1081
* @return a printable, displayable, transmittable representation of the
1138
1082
* array.
1139
1083
*/
1140
- @ Override
1141
1084
public String toString () {
1142
1085
try {
1143
1086
return this .toString (0 );
0 commit comments