1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

support for BigInteger and BigDecimal

This commit is contained in:
stleary 2015-06-20 13:26:55 -05:00
parent 04eab1662c
commit 8bc62cc34c

View file

@ -30,6 +30,7 @@ import java.io.Writer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.math.*;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
@ -503,6 +504,46 @@ public class JSONObject {
+ "] is not a Boolean."); + "] is not a Boolean.");
} }
/**
* Get the BigInteger value associated with a key.
*
* @param key
* A key string.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value cannot
* be converted to BigInteger.
*/
public BigInteger getBigInteger(String key) throws JSONException {
Object object = this.get(key);
try {
return new BigInteger(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigInteger.");
}
}
/**
* Get the BigDecimal value associated with a key.
*
* @param key
* A key string.
* @return The numeric value.
* @throws JSONException
* if the key is not found or if the value
* cannot be converted to BigDecimal.
*/
public BigDecimal getBigDecimal(String key) throws JSONException {
Object object = this.get(key);
try {
return new BigDecimal(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigDecimal.");
}
}
/** /**
* Get the double value associated with a key. * Get the double value associated with a key.
* *
@ -688,6 +729,10 @@ public class JSONObject {
Object value = this.opt(key); Object value = this.opt(key);
if (value == null) { if (value == null) {
this.put(key, 1); this.put(key, 1);
} else if (value instanceof BigInteger) {
this.put(key, ((BigInteger)value).add(BigInteger.ONE));
} else if (value instanceof BigDecimal) {
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
this.put(key, (Integer) value + 1); this.put(key, (Integer) value + 1);
} else if (value instanceof Long) { } else if (value instanceof Long) {
@ -843,6 +888,44 @@ public class JSONObject {
return this.optDouble(key, Double.NaN); return this.optDouble(key, Double.NaN);
} }
/**
* Get an optional BigInteger associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a
* string, an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
try {
return this.getBigInteger(key);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get an optional BigDecimal associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a
* string, an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
try {
return this.getBigDecimal(key);
} catch (Exception e) {
return defaultValue;
}
}
/** /**
* Get an optional double associated with a key, or the defaultValue if * Get an optional double associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a * there is no such key or if its value is not a number. If the value is a