@@ -540,7 +540,7 @@ public BigInteger getBigInteger(String key) throws JSONException {
540540 return new BigInteger (object .toString ());
541541 } catch (Exception e ) {
542542 throw new JSONException ("JSONObject[" + quote (key )
543- + "] could not be converted to BigInteger." );
543+ + "] could not be converted to BigInteger." , e );
544544 }
545545 }
546546
@@ -556,11 +556,14 @@ public BigInteger getBigInteger(String key) throws JSONException {
556556 */
557557 public BigDecimal getBigDecimal (String key ) throws JSONException {
558558 Object object = this .get (key );
559+ if (object instanceof BigDecimal ) {
560+ return (BigDecimal )object ;
561+ }
559562 try {
560563 return new BigDecimal (object .toString ());
561564 } catch (Exception e ) {
562565 throw new JSONException ("JSONObject[" + quote (key )
563- + "] could not be converted to BigDecimal." );
566+ + "] could not be converted to BigDecimal." , e );
564567 }
565568 }
566569
@@ -578,10 +581,10 @@ public double getDouble(String key) throws JSONException {
578581 Object object = this .get (key );
579582 try {
580583 return object instanceof Number ? ((Number ) object ).doubleValue ()
581- : Double . parseDouble ((String ) object );
584+ : new BigDecimal ((String ) object ). doubleValue ( );
582585 } catch (Exception e ) {
583586 throw new JSONException ("JSONObject[" + quote (key )
584- + "] is not a number." );
587+ + "] is not a number." , e );
585588 }
586589 }
587590
@@ -602,7 +605,7 @@ public int getInt(String key) throws JSONException {
602605 : Integer .parseInt ((String ) object );
603606 } catch (Exception e ) {
604607 throw new JSONException ("JSONObject[" + quote (key )
605- + "] is not an int." );
608+ + "] is not an int." , e );
606609 }
607610 }
608611
@@ -659,7 +662,7 @@ public long getLong(String key) throws JSONException {
659662 : Long .parseLong ((String ) object );
660663 } catch (Exception e ) {
661664 throw new JSONException ("JSONObject[" + quote (key )
662- + "] is not a long." );
665+ + "] is not a long." , e );
663666 }
664667 }
665668
@@ -678,7 +681,7 @@ public static String[] getNames(JSONObject jo) {
678681 int i = 0 ;
679682 while (iterator .hasNext ()) {
680683 names [i ] = iterator .next ();
681- i += 1 ;
684+ i ++ ;
682685 }
683686 return names ;
684687 }
@@ -933,7 +936,15 @@ public boolean optBoolean(String key) {
933936 * @return The truth.
934937 */
935938 public boolean optBoolean (String key , boolean defaultValue ) {
939+ Object val = this .opt (key );
940+ if (NULL .equals (val )) {
941+ return defaultValue ;
942+ }
943+ if (val instanceof Boolean ){
944+ return ((Boolean ) val ).booleanValue ();
945+ }
936946 try {
947+ // we'll use the get anyway because it does string conversion.
937948 return this .getBoolean (key );
938949 } catch (Exception e ) {
939950 return defaultValue ;
@@ -965,8 +976,23 @@ public double optDouble(String key) {
965976 * @return An object which is the value.
966977 */
967978 public BigInteger optBigInteger (String key , BigInteger defaultValue ) {
979+ Object val = this .opt (key );
980+ if (NULL .equals (val )) {
981+ return defaultValue ;
982+ }
983+ if (val instanceof BigInteger ){
984+ return (BigInteger ) val ;
985+ }
986+ if (val instanceof BigDecimal ){
987+ return ((BigDecimal ) val ).toBigInteger ();
988+ }
968989 try {
969- return this .getBigInteger (key );
990+ // the other opt functions handle implicit conversions, i.e.
991+ // jo.put("double",1.1d);
992+ // jo.optInt("double"); -- will return 1, not an error
993+ // this conversion to BigDecimal then to BigInteger is to maintain
994+ // that type cast support that may truncate the decimal.
995+ return new BigDecimal (val .toString ()).toBigInteger ();
970996 } catch (Exception e ) {
971997 return defaultValue ;
972998 }
@@ -984,8 +1010,25 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
9841010 * @return An object which is the value.
9851011 */
9861012 public BigDecimal optBigDecimal (String key , BigDecimal defaultValue ) {
1013+ Object val = this .opt (key );
1014+ if (NULL .equals (val )) {
1015+ return defaultValue ;
1016+ }
1017+ if (val instanceof BigDecimal ){
1018+ return (BigDecimal ) val ;
1019+ }
1020+ if (val instanceof BigInteger ){
1021+ return new BigDecimal ((BigInteger ) val );
1022+ }
1023+ if (val instanceof Double ){
1024+ return new BigDecimal (((Double ) val ).doubleValue ());
1025+ }
1026+ if (val instanceof Long || val instanceof Integer
1027+ || val instanceof Short || val instanceof Byte ){
1028+ return new BigDecimal (((Number ) val ).longValue ());
1029+ }
9871030 try {
988- return this . getBigDecimal ( key );
1031+ return new BigDecimal ( val . toString () );
9891032 } catch (Exception e ) {
9901033 return defaultValue ;
9911034 }
@@ -1003,11 +1046,21 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
10031046 * @return An object which is the value.
10041047 */
10051048 public double optDouble (String key , double defaultValue ) {
1006- try {
1007- return this .getDouble (key );
1008- } catch (Exception e ) {
1049+ Object val = this .opt (key );
1050+ if (NULL .equals (val )) {
10091051 return defaultValue ;
10101052 }
1053+ if (val instanceof Number ){
1054+ return ((Number ) val ).doubleValue ();
1055+ }
1056+ if (val instanceof String ) {
1057+ try {
1058+ return new BigDecimal ((String ) val ).doubleValue ();
1059+ } catch (Exception e ) {
1060+ return defaultValue ;
1061+ }
1062+ }
1063+ return defaultValue ;
10111064 }
10121065
10131066 /**
@@ -1035,11 +1088,22 @@ public int optInt(String key) {
10351088 * @return An object which is the value.
10361089 */
10371090 public int optInt (String key , int defaultValue ) {
1038- try {
1039- return this .getInt (key );
1040- } catch (Exception e ) {
1091+ Object val = this .opt (key );
1092+ if (NULL .equals (val )) {
10411093 return defaultValue ;
10421094 }
1095+ if (val instanceof Number ){
1096+ return ((Number ) val ).intValue ();
1097+ }
1098+
1099+ if (val instanceof String ) {
1100+ try {
1101+ return new BigDecimal (val .toString ()).intValue ();
1102+ } catch (Exception e ) {
1103+ return defaultValue ;
1104+ }
1105+ }
1106+ return defaultValue ;
10431107 }
10441108
10451109 /**
@@ -1093,11 +1157,22 @@ public long optLong(String key) {
10931157 * @return An object which is the value.
10941158 */
10951159 public long optLong (String key , long defaultValue ) {
1096- try {
1097- return this .getLong (key );
1098- } catch (Exception e ) {
1160+ Object val = this .opt (key );
1161+ if (NULL .equals (val )) {
10991162 return defaultValue ;
11001163 }
1164+ if (val instanceof Number ){
1165+ return ((Number ) val ).longValue ();
1166+ }
1167+
1168+ if (val instanceof String ) {
1169+ try {
1170+ return new BigDecimal (val .toString ()).longValue ();
1171+ } catch (Exception e ) {
1172+ return defaultValue ;
1173+ }
1174+ }
1175+ return defaultValue ;
11011176 }
11021177
11031178 /**
@@ -1583,7 +1658,7 @@ public static Object stringToValue(String string) {
15831658 return d ;
15841659 }
15851660 } else {
1586- Long myLong = new Long (string );
1661+ Long myLong = Long . valueOf (string );
15871662 if (string .equals (myLong .toString ())) {
15881663 if (myLong .longValue () == myLong .intValue ()) {
15891664 return Integer .valueOf (myLong .intValue ());
0 commit comments