@@ -263,13 +263,7 @@ public boolean getBoolean(int index) throws JSONException {
263263 * to a number.
264264 */
265265 public double getDouble (int index ) throws JSONException {
266- Object object = this .get (index );
267- try {
268- return object instanceof Number ? ((Number ) object ).doubleValue ()
269- : Double .parseDouble ((String ) object );
270- } catch (Exception e ) {
271- throw new JSONException ("JSONArray[" + index + "] is not a number." , e );
272- }
266+ return this .getNumber (index ).doubleValue ();
273267 }
274268
275269 /**
@@ -283,14 +277,7 @@ public double getDouble(int index) throws JSONException {
283277 * object and cannot be converted to a number.
284278 */
285279 public float getFloat (int index ) throws JSONException {
286- Object object = this .get (index );
287- try {
288- return object instanceof Number ? ((Number ) object ).floatValue ()
289- : Float .parseFloat (object .toString ());
290- } catch (Exception e ) {
291- throw new JSONException ("JSONArray[" + index
292- + "] is not a number." , e );
293- }
280+ return this .getNumber (index ).floatValue ();
294281 }
295282
296283 /**
@@ -394,13 +381,7 @@ public BigInteger getBigInteger (int index) throws JSONException {
394381 * If the key is not found or if the value is not a number.
395382 */
396383 public int getInt (int index ) throws JSONException {
397- Object object = this .get (index );
398- try {
399- return object instanceof Number ? ((Number ) object ).intValue ()
400- : Integer .parseInt ((String ) object );
401- } catch (Exception e ) {
402- throw new JSONException ("JSONArray[" + index + "] is not a number." , e );
403- }
384+ return this .getNumber (index ).intValue ();
404385 }
405386
406387 /**
@@ -450,13 +431,7 @@ public JSONObject getJSONObject(int index) throws JSONException {
450431 * to a number.
451432 */
452433 public long getLong (int index ) throws JSONException {
453- Object object = this .get (index );
454- try {
455- return object instanceof Number ? ((Number ) object ).longValue ()
456- : Long .parseLong ((String ) object );
457- } catch (Exception e ) {
458- throw new JSONException ("JSONArray[" + index + "] is not a number." , e );
459- }
434+ return this .getNumber (index ).longValue ();
460435 }
461436
462437 /**
@@ -500,13 +475,16 @@ public boolean isNull(int index) {
500475 */
501476 public String join (String separator ) throws JSONException {
502477 int len = this .length ();
503- StringBuilder sb = new StringBuilder ();
478+ if (len == 0 ) {
479+ return "" ;
480+ }
481+
482+ StringBuilder sb = new StringBuilder (
483+ JSONObject .valueToString (this .myArrayList .get (0 )));
504484
505- for (int i = 0 ; i < len ; i += 1 ) {
506- if (i > 0 ) {
507- sb .append (separator );
508- }
509- sb .append (JSONObject .valueToString (this .myArrayList .get (i )));
485+ for (int i = 1 ; i < len ; i ++) {
486+ sb .append (separator )
487+ .append (JSONObject .valueToString (this .myArrayList .get (i )));
510488 }
511489 return sb .toString ();
512490 }
@@ -589,21 +567,15 @@ public double optDouble(int index) {
589567 * @return The value.
590568 */
591569 public double optDouble (int index , double defaultValue ) {
592- Object val = this .opt (index );
593- if (JSONObject . NULL . equals ( val ) ) {
570+ final Number val = this .optNumber (index , null );
571+ if (val == null ) {
594572 return defaultValue ;
595573 }
596- if (val instanceof Number ){
597- return ((Number ) val ).doubleValue ();
598- }
599- if (val instanceof String ) {
600- try {
601- return Double .parseDouble ((String ) val );
602- } catch (Exception e ) {
603- return defaultValue ;
604- }
605- }
606- return defaultValue ;
574+ final double doubleValue = val .doubleValue ();
575+ // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
576+ // return defaultValue;
577+ // }
578+ return doubleValue ;
607579 }
608580
609581 /**
@@ -631,21 +603,15 @@ public float optFloat(int index) {
631603 * @return The value.
632604 */
633605 public float optFloat (int index , float defaultValue ) {
634- Object val = this .opt (index );
635- if (JSONObject . NULL . equals ( val ) ) {
606+ final Number val = this .optNumber (index , null );
607+ if (val == null ) {
636608 return defaultValue ;
637609 }
638- if (val instanceof Number ){
639- return ((Number ) val ).floatValue ();
640- }
641- if (val instanceof String ) {
642- try {
643- return Float .parseFloat ((String ) val );
644- } catch (Exception e ) {
645- return defaultValue ;
646- }
647- }
648- return defaultValue ;
610+ final float floatValue = val .floatValue ();
611+ // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
612+ // return floatValue;
613+ // }
614+ return floatValue ;
649615 }
650616
651617 /**
@@ -673,22 +639,11 @@ public int optInt(int index) {
673639 * @return The value.
674640 */
675641 public int optInt (int index , int defaultValue ) {
676- Object val = this .opt (index );
677- if (JSONObject . NULL . equals ( val ) ) {
642+ final Number val = this .optNumber (index , null );
643+ if (val == null ) {
678644 return defaultValue ;
679645 }
680- if (val instanceof Number ){
681- return ((Number ) val ).intValue ();
682- }
683-
684- if (val instanceof String ) {
685- try {
686- return new BigDecimal (val .toString ()).intValue ();
687- } catch (Exception e ) {
688- return defaultValue ;
689- }
690- }
691- return defaultValue ;
646+ return val .intValue ();
692647 }
693648
694649 /**
@@ -827,22 +782,11 @@ public long optLong(int index) {
827782 * @return The value.
828783 */
829784 public long optLong (int index , long defaultValue ) {
830- Object val = this .opt (index );
831- if (JSONObject . NULL . equals ( val ) ) {
785+ final Number val = this .optNumber (index , null );
786+ if (val == null ) {
832787 return defaultValue ;
833788 }
834- if (val instanceof Number ){
835- return ((Number ) val ).longValue ();
836- }
837-
838- if (val instanceof String ) {
839- try {
840- return new BigDecimal (val .toString ()).longValue ();
841- } catch (Exception e ) {
842- return defaultValue ;
843- }
844- }
845- return defaultValue ;
789+ return val .longValue ();
846790 }
847791
848792 /**
0 commit comments