@@ -636,15 +636,11 @@ public double optDouble(int index) {
636636 * @return The value.
637637 */
638638 public double optDouble (int index , double defaultValue ) {
639- final Number val = this .optNumber (index , null );
640- if (val == null ) {
639+ try {
640+ return this .getDouble (index );
641+ } catch (Exception ex ) {
641642 return defaultValue ;
642643 }
643- final double doubleValue = val .doubleValue ();
644- // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
645- // return defaultValue;
646- // }
647- return doubleValue ;
648644 }
649645
650646 /**
@@ -672,15 +668,11 @@ public float optFloat(int index) {
672668 * @return The value.
673669 */
674670 public float optFloat (int index , float defaultValue ) {
675- final Number val = this .optNumber (index , null );
676- if (val == null ) {
671+ try {
672+ return this .getFloat (index );
673+ } catch (Exception ex ) {
677674 return defaultValue ;
678675 }
679- final float floatValue = val .floatValue ();
680- // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
681- // return floatValue;
682- // }
683- return floatValue ;
684676 }
685677
686678 /**
@@ -708,11 +700,11 @@ public int optInt(int index) {
708700 * @return The value.
709701 */
710702 public int optInt (int index , int defaultValue ) {
711- final Number val = this .optNumber (index , null );
712- if (val == null ) {
703+ try {
704+ return this .getInt (index );
705+ } catch (Exception ex ) {
713706 return defaultValue ;
714707 }
715- return val .intValue ();
716708 }
717709
718710 /**
@@ -776,8 +768,11 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
776768 * @return The value.
777769 */
778770 public BigInteger optBigInteger (int index , BigInteger defaultValue ) {
779- Object val = this .opt (index );
780- return JSONObject .objectToBigInteger (val , defaultValue );
771+ try {
772+ return this .getBigInteger (index );
773+ } catch (Exception ex ) {
774+ return defaultValue ;
775+ }
781776 }
782777
783778 /**
@@ -795,8 +790,11 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
795790 * @return The value.
796791 */
797792 public BigDecimal optBigDecimal (int index , BigDecimal defaultValue ) {
798- Object val = this .opt (index );
799- return JSONObject .objectToBigDecimal (val , defaultValue );
793+ try {
794+ return this .getBigDecimal (index );
795+ } catch (Exception ex ) {
796+ return defaultValue ;
797+ }
800798 }
801799
802800 /**
@@ -851,11 +849,11 @@ public long optLong(int index) {
851849 * @return The value.
852850 */
853851 public long optLong (int index , long defaultValue ) {
854- final Number val = this .optNumber (index , null );
855- if (val == null ) {
852+ try {
853+ return this .getLong (index );
854+ } catch (Exception ex ) {
856855 return defaultValue ;
857856 }
858- return val .longValue ();
859857 }
860858
861859 /**
@@ -885,22 +883,11 @@ public Number optNumber(int index) {
885883 * @return An object which is the value.
886884 */
887885 public Number optNumber (int index , Number defaultValue ) {
888- Object val = this .opt (index );
889- if (JSONObject .NULL .equals (val )) {
886+ try {
887+ return this .getNumber (index );
888+ } catch (Exception ex ) {
890889 return defaultValue ;
891890 }
892- if (val instanceof Number ){
893- return (Number ) val ;
894- }
895-
896- if (val instanceof String ) {
897- try {
898- return JSONObject .stringToNumber ((String ) val );
899- } catch (Exception e ) {
900- return defaultValue ;
901- }
902- }
903- return defaultValue ;
904891 }
905892
906893 /**
@@ -932,6 +919,30 @@ public String optString(int index, String defaultValue) {
932919 .toString ();
933920 }
934921
922+ public Integer getInteger (int index ) {
923+ final Object object = this .get (index );
924+ if (object instanceof Number ) {
925+ return ((Number ) object ).intValue ();
926+ }
927+ try {
928+ return Integer .parseInt (object .toString ());
929+ } catch (Exception e ) {
930+ throw wrongValueFormatException (index , "integer" , e );
931+ }
932+ }
933+
934+ public Integer optInteger (int index ) {
935+ return this .optInteger (index , null );
936+ }
937+
938+ public Integer optInteger (int index , Integer defaultValue ) {
939+ try {
940+ return this .getInteger (index );
941+ } catch (Exception ex ) {
942+ return defaultValue ;
943+ }
944+ }
945+
935946 /**
936947 * Append a boolean value. This increases the array's length by one.
937948 *
@@ -1700,16 +1711,4 @@ private static JSONException wrongValueFormatException(
17001711 "JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
17011712 , cause );
17021713 }
1703-
1704- public Integer optInteger (int index ) {
1705- return this .optInteger (index , null );
1706- }
1707-
1708- public Integer optInteger (int index , Integer defaultValue ) {
1709- final Number val = this .optNumber (index , null );
1710- if (val == null ) {
1711- return defaultValue ;
1712- }
1713- return val .intValue ();
1714- }
17151714}
0 commit comments