mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #135 from douglascrockford/big-numbers
Proposed BigInteger, BigDecimal support
This commit is contained in:
commit
4f8b25d527
2 changed files with 166 additions and 3 deletions
|
@ -28,6 +28,7 @@ import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
|
import java.math.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -75,7 +76,7 @@ import java.util.Map;
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2015-06-04
|
* @version 2015-07-06
|
||||||
*/
|
*/
|
||||||
public class JSONArray implements Iterable<Object> {
|
public class JSONArray implements Iterable<Object> {
|
||||||
|
|
||||||
|
@ -246,6 +247,46 @@ public class JSONArray implements Iterable<Object> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the BigDecimal value associated with an index.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* The index must be between 0 and length() - 1.
|
||||||
|
* @return The value.
|
||||||
|
* @throws JSONException
|
||||||
|
* If the key is not found or if the value cannot be converted
|
||||||
|
* to a BigDecimal.
|
||||||
|
*/
|
||||||
|
public BigDecimal getBigDecimal (int index) throws JSONException {
|
||||||
|
Object object = this.get(index);
|
||||||
|
try {
|
||||||
|
return new BigDecimal(object.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new JSONException("JSONArray[" + index +
|
||||||
|
"] could not convert to BigDecimal.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the BigInteger value associated with an index.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* The index must be between 0 and length() - 1.
|
||||||
|
* @return The value.
|
||||||
|
* @throws JSONException
|
||||||
|
* If the key is not found or if the value cannot be converted
|
||||||
|
* to a BigInteger.
|
||||||
|
*/
|
||||||
|
public BigInteger getBigInteger (int index) throws JSONException {
|
||||||
|
Object object = this.get(index);
|
||||||
|
try {
|
||||||
|
return new BigInteger(object.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new JSONException("JSONArray[" + index +
|
||||||
|
"] could not convert to BigInteger.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the int value associated with an index.
|
* Get the int value associated with an index.
|
||||||
*
|
*
|
||||||
|
@ -490,6 +531,44 @@ public class JSONArray implements Iterable<Object> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the optional BigInteger value associated with an index. The
|
||||||
|
* defaultValue is returned if there is no value for the index, or if the
|
||||||
|
* value is not a number and cannot be converted to a number.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* The index must be between 0 and length() - 1.
|
||||||
|
* @param defaultValue
|
||||||
|
* The default value.
|
||||||
|
* @return The value.
|
||||||
|
*/
|
||||||
|
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
|
||||||
|
try {
|
||||||
|
return this.getBigInteger(index);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the optional BigDecimal value associated with an index. The
|
||||||
|
* defaultValue is returned if there is no value for the index, or if the
|
||||||
|
* value is not a number and cannot be converted to a number.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* The index must be between 0 and length() - 1.
|
||||||
|
* @param defaultValue
|
||||||
|
* The default value.
|
||||||
|
* @return The value.
|
||||||
|
*/
|
||||||
|
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
|
||||||
|
try {
|
||||||
|
return this.getBigDecimal(index);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the optional JSONArray associated with an index.
|
* Get the optional JSONArray associated with an index.
|
||||||
*
|
*
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -91,7 +92,7 @@ import java.util.Set;
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2015-05-05
|
* @version 2015-07-06
|
||||||
*/
|
*/
|
||||||
public class JSONObject {
|
public class JSONObject {
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
||||||
|
@ -1550,7 +1633,8 @@ public class JSONObject {
|
||||||
|| object instanceof Short || object instanceof Integer
|
|| object instanceof Short || object instanceof Integer
|
||||||
|| object instanceof Long || object instanceof Boolean
|
|| object instanceof Long || object instanceof Boolean
|
||||||
|| object instanceof Float || object instanceof Double
|
|| object instanceof Float || object instanceof Double
|
||||||
|| object instanceof String) {
|
|| object instanceof String || object instanceof BigInteger
|
||||||
|
|| object instanceof BigDecimal) {
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue