Skip to content

Commit 5d6bf7d

Browse files
committed
support BigInteger and BigDecimal
1 parent 04eab16 commit 5d6bf7d

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

JSONObject.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.lang.reflect.Field;
3131
import java.lang.reflect.Method;
3232
import java.lang.reflect.Modifier;
33+
import java.math.*;
3334
import java.util.Collection;
3435
import java.util.Enumeration;
3536
import java.util.HashMap;
@@ -503,6 +504,46 @@ public boolean getBoolean(String key) throws JSONException {
503504
+ "] is not a Boolean.");
504505
}
505506

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+
506547
/**
507548
* Get the double value associated with a key.
508549
*
@@ -688,6 +729,10 @@ public JSONObject increment(String key) throws JSONException {
688729
Object value = this.opt(key);
689730
if (value == null) {
690731
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));
691736
} else if (value instanceof Integer) {
692737
this.put(key, (Integer) value + 1);
693738
} else if (value instanceof Long) {
@@ -843,6 +888,44 @@ public double optDouble(String key) {
843888
return this.optDouble(key, Double.NaN);
844889
}
845890

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+
846929
/**
847930
* Get an optional double associated with a key, or the defaultValue if
848931
* there is no such key or if its value is not a number. If the value is a

0 commit comments

Comments
 (0)