@@ -30,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
30
30
import java .lang .reflect .Field ;
31
31
import java .lang .reflect .Method ;
32
32
import java .lang .reflect .Modifier ;
33
+ import java .math .*;
33
34
import java .util .Collection ;
34
35
import java .util .Enumeration ;
35
36
import java .util .HashMap ;
@@ -91,7 +92,7 @@ of this software and associated documentation files (the "Software"), to deal
91
92
* </ul>
92
93
*
93
94
* @author JSON.org
94
- * @version 2015-05-05
95
+ * @version 2015-07-06
95
96
*/
96
97
public class JSONObject {
97
98
/**
@@ -503,6 +504,46 @@ public boolean getBoolean(String key) throws JSONException {
503
504
+ "] is not a Boolean." );
504
505
}
505
506
507
+ /**
508
+ * Get the BigInteger value associated with a key.
509
+ *
510
+ * @param key
511
+ * A key string.
512
+ * @return The numeric value.
513
+ * @throws JSONException
514
+ * if the key is not found or if the value cannot
515
+ * be converted to BigInteger.
516
+ */
517
+ public BigInteger getBigInteger (String key ) throws JSONException {
518
+ Object object = this .get (key );
519
+ try {
520
+ return new BigInteger (object .toString ());
521
+ } catch (Exception e ) {
522
+ throw new JSONException ("JSONObject[" + quote (key )
523
+ + "] could not be converted to BigInteger." );
524
+ }
525
+ }
526
+
527
+ /**
528
+ * Get the BigDecimal value associated with a key.
529
+ *
530
+ * @param key
531
+ * A key string.
532
+ * @return The numeric value.
533
+ * @throws JSONException
534
+ * if the key is not found or if the value
535
+ * cannot be converted to BigDecimal.
536
+ */
537
+ public BigDecimal getBigDecimal (String key ) throws JSONException {
538
+ Object object = this .get (key );
539
+ try {
540
+ return new BigDecimal (object .toString ());
541
+ } catch (Exception e ) {
542
+ throw new JSONException ("JSONObject[" + quote (key )
543
+ + "] could not be converted to BigDecimal." );
544
+ }
545
+ }
546
+
506
547
/**
507
548
* Get the double value associated with a key.
508
549
*
@@ -688,6 +729,10 @@ public JSONObject increment(String key) throws JSONException {
688
729
Object value = this .opt (key );
689
730
if (value == null ) {
690
731
this .put (key , 1 );
732
+ } else if (value instanceof BigInteger ) {
733
+ this .put (key , ((BigInteger )value ).add (BigInteger .ONE ));
734
+ } else if (value instanceof BigDecimal ) {
735
+ this .put (key , ((BigDecimal )value ).add (BigDecimal .ONE ));
691
736
} else if (value instanceof Integer ) {
692
737
this .put (key , (Integer ) value + 1 );
693
738
} else if (value instanceof Long ) {
@@ -843,6 +888,44 @@ public double optDouble(String key) {
843
888
return this .optDouble (key , Double .NaN );
844
889
}
845
890
891
+ /**
892
+ * Get an optional BigInteger associated with a key, or the defaultValue if
893
+ * there is no such key or if its value is not a number. If the value is a
894
+ * string, an attempt will be made to evaluate it as a number.
895
+ *
896
+ * @param key
897
+ * A key string.
898
+ * @param defaultValue
899
+ * The default.
900
+ * @return An object which is the value.
901
+ */
902
+ public BigInteger optBigInteger (String key , BigInteger defaultValue ) {
903
+ try {
904
+ return this .getBigInteger (key );
905
+ } catch (Exception e ) {
906
+ return defaultValue ;
907
+ }
908
+ }
909
+
910
+ /**
911
+ * Get an optional BigDecimal associated with a key, or the defaultValue if
912
+ * there is no such key or if its value is not a number. If the value is a
913
+ * string, an attempt will be made to evaluate it as a number.
914
+ *
915
+ * @param key
916
+ * A key string.
917
+ * @param defaultValue
918
+ * The default.
919
+ * @return An object which is the value.
920
+ */
921
+ public BigDecimal optBigDecimal (String key , BigDecimal defaultValue ) {
922
+ try {
923
+ return this .getBigDecimal (key );
924
+ } catch (Exception e ) {
925
+ return defaultValue ;
926
+ }
927
+ }
928
+
846
929
/**
847
930
* Get an optional double associated with a key, or the defaultValue if
848
931
* there is no such key or if its value is not a number. If the value is a
@@ -1550,7 +1633,8 @@ public static Object wrap(Object object) {
1550
1633
|| object instanceof Short || object instanceof Integer
1551
1634
|| object instanceof Long || object instanceof Boolean
1552
1635
|| object instanceof Float || object instanceof Double
1553
- || object instanceof String ) {
1636
+ || object instanceof String || object instanceof BigInteger
1637
+ || object instanceof BigDecimal ) {
1554
1638
return object ;
1555
1639
}
1556
1640
0 commit comments