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

Merge pull request #440 from johnjaylward/FixForBigDecimal

Fixes #438 - Corrections to BigDecimal consistency
This commit is contained in:
Sean Leary 2018-10-06 14:51:57 -05:00 committed by GitHub
commit 1a811f1ada
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 215 additions and 315 deletions

View file

@ -180,10 +180,16 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Construct a JSONArray from an array * Construct a JSONArray from an array.
*
* @param array
* Array. If the parameter passed is null, or not an array, an
* exception will be thrown.
* *
* @throws JSONException * @throws JSONException
* If not an array or if an array value is non-finite number. * If not an array or if an array value is non-finite number.
* @throws NullPointerException
* Thrown if the array parameter is null.
*/ */
public JSONArray(Object array) throws JSONException { public JSONArray(Object array) throws JSONException {
this(); this();
@ -257,13 +263,7 @@ public class JSONArray implements Iterable<Object> {
* to a number. * to a number.
*/ */
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
Object object = this.get(index); return this.getNumber(index).doubleValue();
try {
return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble((String) object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
}
} }
/** /**
@ -277,14 +277,7 @@ public class JSONArray implements Iterable<Object> {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public float getFloat(int index) throws JSONException { public float getFloat(int index) throws JSONException {
Object object = this.get(index); return this.getNumber(index).floatValue();
try {
return object instanceof Number ? ((Number) object).floatValue()
: Float.parseFloat(object.toString());
} catch (Exception e) {
throw new JSONException("JSONArray[" + index
+ "] is not a number.", e);
}
} }
/** /**
@ -310,17 +303,19 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Get the enum value associated with an index. * Get the enum value associated with an index.
* *
* @param clazz * @param <E>
* The type of enum to retrieve. * Enum Type
* @param index * @param clazz
* The index must be between 0 and length() - 1. * The type of enum to retrieve.
* @return The enum value at the index location * @param index
* @throws JSONException * The index must be between 0 and length() - 1.
* if the key is not found or if the value cannot be converted * @return The enum value at the index location
* to an enum. * @throws JSONException
*/ * if the key is not found or if the value cannot be converted
* to an enum.
*/
public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException { public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
E val = optEnum(clazz, index); E val = optEnum(clazz, index);
if(val==null) { if(val==null) {
@ -334,7 +329,10 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Get the BigDecimal value associated with an index. * Get the BigDecimal value associated with an index. If the value is float
* or double, the the {@link BigDecimal#BigDecimal(double)} constructor
* will be used. See notes on the constructor for conversion issues that
* may arise.
* *
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1.
@ -345,12 +343,12 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigDecimal getBigDecimal (int index) throws JSONException { public BigDecimal getBigDecimal (int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { BigDecimal val = JSONObject.objectToBigDecimal(object, null);
return new BigDecimal(object.toString()); if(val == null) {
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index +
"] could not convert to BigDecimal.", e); "] could not convert to BigDecimal ("+ object + ").");
} }
return val;
} }
/** /**
@ -365,12 +363,12 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigInteger getBigInteger (int index) throws JSONException { public BigInteger getBigInteger (int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { BigInteger val = JSONObject.objectToBigInteger(object, null);
return new BigInteger(object.toString()); if(val == null) {
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index +
"] could not convert to BigInteger.", e); "] could not convert to BigDecimal ("+ object + ").");
} }
return val;
} }
/** /**
@ -383,13 +381,7 @@ public class JSONArray implements Iterable<Object> {
* If the key is not found or if the value is not a number. * If the key is not found or if the value is not a number.
*/ */
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
Object object = this.get(index); return this.getNumber(index).intValue();
try {
return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
}
} }
/** /**
@ -439,13 +431,7 @@ public class JSONArray implements Iterable<Object> {
* to a number. * to a number.
*/ */
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
Object object = this.get(index); return this.getNumber(index).longValue();
try {
return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
}
} }
/** /**
@ -489,13 +475,16 @@ public class JSONArray implements Iterable<Object> {
*/ */
public String join(String separator) throws JSONException { public String join(String separator) throws JSONException {
int len = this.length(); int len = this.length();
StringBuilder sb = new StringBuilder(); if (len == 0) {
return "";
}
StringBuilder sb = new StringBuilder(
JSONObject.valueToString(this.myArrayList.get(0)));
for (int i = 0; i < len; i += 1) { for (int i = 1; i < len; i++) {
if (i > 0) { sb.append(separator)
sb.append(separator); .append(JSONObject.valueToString(this.myArrayList.get(i)));
}
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
} }
return sb.toString(); return sb.toString();
} }
@ -578,21 +567,15 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public double optDouble(int index, double defaultValue) { public double optDouble(int index, double defaultValue) {
Object val = this.opt(index); final Number val = this.optNumber(index, null);
if (JSONObject.NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ final double doubleValue = val.doubleValue();
return ((Number) val).doubleValue(); // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
} // return defaultValue;
if (val instanceof String) { // }
try { return doubleValue;
return Double.parseDouble((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -620,21 +603,15 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public float optFloat(int index, float defaultValue) { public float optFloat(int index, float defaultValue) {
Object val = this.opt(index); final Number val = this.optNumber(index, null);
if (JSONObject.NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ final float floatValue = val.floatValue();
return ((Number) val).floatValue(); // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
} // return floatValue;
if (val instanceof String) { // }
try { return floatValue;
return Float.parseFloat((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -662,27 +639,18 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public int optInt(int index, int defaultValue) { public int optInt(int index, int defaultValue) {
Object val = this.opt(index); final Number val = this.optNumber(index, null);
if (JSONObject.NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ return val.intValue();
return ((Number) val).intValue();
}
if (val instanceof String) {
try {
return new BigDecimal(val.toString()).intValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param index * @param index
@ -696,6 +664,8 @@ public class JSONArray implements Iterable<Object> {
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param index * @param index
@ -725,7 +695,6 @@ public class JSONArray implements Iterable<Object> {
} }
} }
/** /**
* Get the optional BigInteger value associated with an index. The * Get the optional BigInteger value associated with an index. The
* defaultValue is returned if there is no value for the index, or if the * defaultValue is returned if there is no value for the index, or if the
@ -739,37 +708,16 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigInteger optBigInteger(int index, BigInteger defaultValue) { public BigInteger optBigInteger(int index, BigInteger defaultValue) {
Object val = this.opt(index); Object val = this.opt(index);
if (JSONObject.NULL.equals(val)) { return JSONObject.objectToBigInteger(val, defaultValue);
return defaultValue;
}
if (val instanceof BigInteger){
return (BigInteger) val;
}
if (val instanceof BigDecimal){
return ((BigDecimal) val).toBigInteger();
}
if (val instanceof Double || val instanceof Float){
return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
}
if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){
return BigInteger.valueOf(((Number) val).longValue());
}
try {
final String valStr = val.toString();
if(JSONObject.isDecimalNotation(valStr)) {
return new BigDecimal(valStr).toBigInteger();
}
return new BigInteger(valStr);
} catch (Exception e) {
return defaultValue;
}
} }
/** /**
* Get the optional BigDecimal value associated with an index. The * Get the optional BigDecimal value associated with an index. The
* defaultValue is returned if there is no value for the index, or if 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. * value is not a number and cannot be converted to a number. If the value
* is float or double, the the {@link BigDecimal#BigDecimal(double)}
* constructor will be used. See notes on the constructor for conversion
* issues that may arise.
* *
* @param index * @param index
* The index must be between 0 and length() - 1. * The index must be between 0 and length() - 1.
@ -779,27 +727,7 @@ public class JSONArray implements Iterable<Object> {
*/ */
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
Object val = this.opt(index); Object val = this.opt(index);
if (JSONObject.NULL.equals(val)) { return JSONObject.objectToBigDecimal(val, defaultValue);
return defaultValue;
}
if (val instanceof BigDecimal){
return (BigDecimal) val;
}
if (val instanceof BigInteger){
return new BigDecimal((BigInteger) val);
}
if (val instanceof Double || val instanceof Float){
return new BigDecimal(((Number) val).doubleValue());
}
if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){
return new BigDecimal(((Number) val).longValue());
}
try {
return new BigDecimal(val.toString());
} catch (Exception e) {
return defaultValue;
}
} }
/** /**
@ -854,22 +782,11 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public long optLong(int index, long defaultValue) { public long optLong(int index, long defaultValue) {
Object val = this.opt(index); final Number val = this.optNumber(index, null);
if (JSONObject.NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ return val.longValue();
return ((Number) val).longValue();
}
if (val instanceof String) {
try {
return new BigDecimal(val.toString()).longValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1236,8 +1153,8 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Uses a uaer initialized JSONPointer and tries to * Uses a user initialized JSONPointer and tries to
* match it to an item whithin this JSONArray. For example, given a * match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document: * JSONArray initialized with this document:
* <pre> * <pre>
* [ * [
@ -1535,7 +1452,7 @@ public class JSONArray implements Iterable<Object> {
* @return true if JSONArray is empty, otherwise false. * @return true if JSONArray is empty, otherwise false.
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return myArrayList.isEmpty(); return this.myArrayList.isEmpty();
} }
} }

View file

@ -45,6 +45,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
/** /**
* A JSONObject is an unordered collection of name/value pairs. Its external * A JSONObject is an unordered collection of name/value pairs. Its external
@ -150,6 +151,12 @@ public class JSONObject {
return "null"; return "null";
} }
} }
/**
* Regular Expression Pattern that matches JSON Numbers. This is primarily used for
* output to guarantee that we are always writing valid JSON.
*/
static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?");
/** /**
* The map where the JSONObject's properties are kept. * The map where the JSONObject's properties are kept.
@ -569,17 +576,19 @@ public class JSONObject {
} }
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param clazz * @param <E>
* The type of enum to retrieve. * Enum Type
* @param key * @param clazz
* A key string. * The type of enum to retrieve.
* @return The enum value associated with the key * @param key
* @throws JSONException * A key string.
* if the key is not found or if the value cannot be converted * @return The enum value associated with the key
* to an enum. * @throws JSONException
*/ * if the key is not found or if the value cannot be converted
* to an enum.
*/
public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException { public <E extends Enum<E>> E getEnum(Class<E> clazz, String key) throws JSONException {
E val = optEnum(clazz, key); E val = optEnum(clazz, key);
if(val==null) { if(val==null) {
@ -630,16 +639,19 @@ public class JSONObject {
*/ */
public BigInteger getBigInteger(String key) throws JSONException { public BigInteger getBigInteger(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { BigInteger ret = objectToBigInteger(object, null);
return new BigInteger(object.toString()); if (ret != null) {
} catch (Exception e) { return ret;
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigInteger.", e);
} }
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigInteger (" + object + ").");
} }
/** /**
* Get the BigDecimal value associated with a key. * Get the BigDecimal value associated with a key. If the value is float or
* double, the the {@link BigDecimal#BigDecimal(double)} constructor will
* be used. See notes on the constructor for conversion issues that may
* arise.
* *
* @param key * @param key
* A key string. * A key string.
@ -650,15 +662,12 @@ public class JSONObject {
*/ */
public BigDecimal getBigDecimal(String key) throws JSONException { public BigDecimal getBigDecimal(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
if (object instanceof BigDecimal) { BigDecimal ret = objectToBigDecimal(object, null);
return (BigDecimal)object; if (ret != null) {
} return ret;
try {
return new BigDecimal(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigDecimal.", e);
} }
throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigDecimal (" + object + ").");
} }
/** /**
@ -672,14 +681,7 @@ public class JSONObject {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
Object object = this.get(key); return this.getNumber(key).doubleValue();
try {
return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
}
} }
/** /**
@ -693,14 +695,7 @@ public class JSONObject {
* object and cannot be converted to a number. * object and cannot be converted to a number.
*/ */
public float getFloat(String key) throws JSONException { public float getFloat(String key) throws JSONException {
Object object = this.get(key); return this.getNumber(key).floatValue();
try {
return object instanceof Number ? ((Number) object).floatValue()
: Float.parseFloat(object.toString());
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number.", e);
}
} }
/** /**
@ -737,14 +732,7 @@ public class JSONObject {
* to an integer. * to an integer.
*/ */
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
Object object = this.get(key); return this.getNumber(key).intValue();
try {
return object instanceof Number ? ((Number) object).intValue()
: Integer.parseInt((String) object);
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not an int.", e);
}
} }
/** /**
@ -794,19 +782,14 @@ public class JSONObject {
* to a long. * to a long.
*/ */
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
Object object = this.get(key); return this.getNumber(key).longValue();
try {
return object instanceof Number ? ((Number) object).longValue()
: Long.parseLong((String) object);
} catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a long.", e);
}
} }
/** /**
* Get an array of field names from a JSONObject. * Get an array of field names from a JSONObject.
* *
* @param jo
* JSON object
* @return An array of field names, or null if there are no names. * @return An array of field names, or null if there are no names.
*/ */
public static String[] getNames(JSONObject jo) { public static String[] getNames(JSONObject jo) {
@ -817,8 +800,10 @@ public class JSONObject {
} }
/** /**
* Get an array of field names from an Object. * Get an array of public field names from an Object.
* *
* @param object
* object to read
* @return An array of field names, or null if there are no names. * @return An array of field names, or null if there are no names.
*/ */
public static String[] getNames(Object object) { public static String[] getNames(Object object) {
@ -968,7 +953,7 @@ public class JSONObject {
* @return true if JSONObject is empty, otherwise false. * @return true if JSONObject is empty, otherwise false.
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return map.isEmpty(); return this.map.isEmpty();
} }
/** /**
@ -1029,6 +1014,8 @@ public class JSONObject {
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param key * @param key
@ -1042,6 +1029,8 @@ public class JSONObject {
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
* *
* @param <E>
* Enum Type
* @param clazz * @param clazz
* The type of enum to retrieve. * The type of enum to retrieve.
* @param key * @param key
@ -1113,7 +1102,10 @@ public class JSONObject {
/** /**
* Get an optional BigDecimal associated with a key, or the defaultValue if * 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 * 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. * string, an attempt will be made to evaluate it as a number. If the value
* is float or double, then the {@link BigDecimal#BigDecimal(double)}
* constructor will be used. See notes on the constructor for conversion
* issues that may arise.
* *
* @param key * @param key
* A key string. * A key string.
@ -1123,6 +1115,16 @@ public class JSONObject {
*/ */
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) { public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
Object val = this.opt(key); Object val = this.opt(key);
return objectToBigDecimal(val, defaultValue);
}
/**
* @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.
* @return BigDecimal conversion of the original value, or the defaultValue if unable
* to convert.
*/
static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
if (NULL.equals(val)) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
@ -1133,6 +1135,10 @@ public class JSONObject {
return new BigDecimal((BigInteger) val); return new BigDecimal((BigInteger) val);
} }
if (val instanceof Double || val instanceof Float){ if (val instanceof Double || val instanceof Float){
final double d = ((Number) val).doubleValue();
if(Double.isNaN(d)) {
return defaultValue;
}
return new BigDecimal(((Number) val).doubleValue()); return new BigDecimal(((Number) val).doubleValue());
} }
if (val instanceof Long || val instanceof Integer if (val instanceof Long || val instanceof Integer
@ -1160,6 +1166,16 @@ public class JSONObject {
*/ */
public BigInteger optBigInteger(String key, BigInteger defaultValue) { public BigInteger optBigInteger(String key, BigInteger defaultValue) {
Object val = this.opt(key); Object val = this.opt(key);
return objectToBigInteger(val, defaultValue);
}
/**
* @param val value to convert
* @param defaultValue default value to return is the conversion doesn't work or is null.
* @return BigInteger conversion of the original value, or the defaultValue if unable
* to convert.
*/
static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
if (NULL.equals(val)) { if (NULL.equals(val)) {
return defaultValue; return defaultValue;
} }
@ -1170,7 +1186,11 @@ public class JSONObject {
return ((BigDecimal) val).toBigInteger(); return ((BigDecimal) val).toBigInteger();
} }
if (val instanceof Double || val instanceof Float){ if (val instanceof Double || val instanceof Float){
return new BigDecimal(((Number) val).doubleValue()).toBigInteger(); final double d = ((Number) val).doubleValue();
if(Double.isNaN(d)) {
return defaultValue;
}
return new BigDecimal(d).toBigInteger();
} }
if (val instanceof Long || val instanceof Integer if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){ || val instanceof Short || val instanceof Byte){
@ -1218,21 +1238,15 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public double optDouble(String key, double defaultValue) { public double optDouble(String key, double defaultValue) {
Object val = this.opt(key); Number val = this.optNumber(key);
if (NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ final double doubleValue = val.doubleValue();
return ((Number) val).doubleValue(); // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
} // return defaultValue;
if (val instanceof String) { // }
try { return doubleValue;
return Double.parseDouble((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1260,21 +1274,15 @@ public class JSONObject {
* @return The value. * @return The value.
*/ */
public float optFloat(String key, float defaultValue) { public float optFloat(String key, float defaultValue) {
Object val = this.opt(key); Number val = this.optNumber(key);
if (JSONObject.NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ final float floatValue = val.floatValue();
return ((Number) val).floatValue(); // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
} // return defaultValue;
if (val instanceof String) { // }
try { return floatValue;
return Float.parseFloat((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1302,22 +1310,11 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public int optInt(String key, int defaultValue) { public int optInt(String key, int defaultValue) {
Object val = this.opt(key); final Number val = this.optNumber(key, null);
if (NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){ return val.intValue();
return ((Number) val).intValue();
}
if (val instanceof String) {
try {
return new BigDecimal((String) val).intValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1371,22 +1368,12 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public long optLong(String key, long defaultValue) { public long optLong(String key, long defaultValue) {
Object val = this.opt(key); final Number val = this.optNumber(key, null);
if (NULL.equals(val)) { if (val == null) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){
return ((Number) val).longValue();
}
if (val instanceof String) { return val.longValue();
try {
return new BigDecimal((String) val).longValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1424,14 +1411,11 @@ public class JSONObject {
return (Number) val; return (Number) val;
} }
if (val instanceof String) { try {
try { return stringToNumber(val.toString());
return stringToNumber((String) val); } catch (Exception e) {
} catch (Exception e) { return defaultValue;
return defaultValue;
}
} }
return defaultValue;
} }
/** /**
@ -1823,8 +1807,10 @@ public class JSONObject {
* are both non-null, and only if there is not already a member with that * are both non-null, and only if there is not already a member with that
* name. * name.
* *
* @param key string * @param key
* @param value object * key to insert into
* @param value
* value to insert
* @return this. * @return this.
* @throws JSONException * @throws JSONException
* if the key is a duplicate * if the key is a duplicate
@ -1935,9 +1921,10 @@ public class JSONObject {
/** /**
* Produce a string in double quotes with backslash sequences in all the * Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/, * right places. A backslash will be inserted within &lt;/, producing
* allowing JSON text to be delivered in HTML. In JSON text, a string cannot * &lt;\/, allowing JSON text to be delivered in HTML. In JSON text, a
* contain a control character or an unescaped quote or backslash. * string cannot contain a control character or an unescaped quote or
* backslash.
* *
* @param string * @param string
* A String * A String
@ -2150,22 +2137,26 @@ public class JSONObject {
* can't be converted, return the string. * can't be converted, return the string.
* *
* @param string * @param string
* A String. * A String. can not be null.
* @return A simple JSON value. * @return A simple JSON value.
* @throws NullPointerException
* Thrown if the string is null.
*/ */
// Changes to this method must be copied to the corresponding method in // Changes to this method must be copied to the corresponding method in
// the XML class to keep full support for Android // the XML class to keep full support for Android
public static Object stringToValue(String string) { public static Object stringToValue(String string) {
if (string.equals("")) { if ("".equals(string)) {
return string; return string;
} }
if (string.equalsIgnoreCase("true")) {
// check JSON key words true/false/null
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if (string.equalsIgnoreCase("false")) { if ("false".equalsIgnoreCase(string)) {
return Boolean.FALSE; return Boolean.FALSE;
} }
if (string.equalsIgnoreCase("null")) { if ("null".equalsIgnoreCase(string)) {
return JSONObject.NULL; return JSONObject.NULL;
} }
@ -2177,7 +2168,8 @@ public class JSONObject {
char initial = string.charAt(0); char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
// if we want full Big Number support this block can be replaced with: // if we want full Big Number support the contents of this
// `try` block can be replaced with:
// return stringToNumber(string); // return stringToNumber(string);
if (isDecimalNotation(string)) { if (isDecimalNotation(string)) {
Double d = Double.valueOf(string); Double d = Double.valueOf(string);
@ -2414,13 +2406,9 @@ public class JSONObject {
} else if (value instanceof Number) { } else if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
final String numberAsString = numberToString((Number) value); final String numberAsString = numberToString((Number) value);
try { if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
// Use the BigDecimal constructor for its parser to validate the format.
@SuppressWarnings("unused")
BigDecimal testNum = new BigDecimal(numberAsString);
// Close enough to a JSON number that we will use it unquoted
writer.write(numberAsString); writer.write(numberAsString);
} catch (NumberFormatException ex){ } else {
// The Number value is not a valid JSON number. // The Number value is not a valid JSON number.
// Instead we will quote it as a string // Instead we will quote it as a string
quote(numberAsString, writer); quote(numberAsString, writer);

View file

@ -233,8 +233,8 @@ public class JSONPointer {
int index = Integer.parseInt(indexToken); int index = Integer.parseInt(indexToken);
JSONArray currentArr = (JSONArray) current; JSONArray currentArr = (JSONArray) current;
if (index >= currentArr.length()) { if (index >= currentArr.length()) {
throw new JSONPointerException(format("index %d is out of bounds - the array has %d elements", index, throw new JSONPointerException(format("index %s is out of bounds - the array has %d elements", indexToken,
currentArr.length())); Integer.valueOf(currentArr.length())));
} }
try { try {
return currentArr.get(index); return currentArr.get(index);

View file

@ -1,7 +1,6 @@
package org.json; package org.json;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
@ -326,31 +325,27 @@ public class JSONWriter {
return "null"; return "null";
} }
if (value instanceof JSONString) { if (value instanceof JSONString) {
Object object; String object;
try { try {
object = ((JSONString) value).toJSONString(); object = ((JSONString) value).toJSONString();
} catch (Exception e) { } catch (Exception e) {
throw new JSONException(e); throw new JSONException(e);
} }
if (object instanceof String) { if (object != null) {
return (String) object; return object;
} }
throw new JSONException("Bad value from toJSONString: " + object); throw new JSONException("Bad value from toJSONString: " + object);
} }
if (value instanceof Number) { if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
final String numberAsString = JSONObject.numberToString((Number) value); final String numberAsString = JSONObject.numberToString((Number) value);
try { if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) {
// Use the BigDecimal constructor for it's parser to validate the format.
@SuppressWarnings("unused")
BigDecimal unused = new BigDecimal(numberAsString);
// Close enough to a JSON number that we will return it unquoted // Close enough to a JSON number that we will return it unquoted
return numberAsString; return numberAsString;
} catch (NumberFormatException ex){
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
return JSONObject.quote(numberAsString);
} }
// The Number value is not a valid JSON number.
// Instead we will quote it as a string
return JSONObject.quote(numberAsString);
} }
if (value instanceof Boolean || value instanceof JSONObject if (value instanceof Boolean || value instanceof JSONObject
|| value instanceof JSONArray) { || value instanceof JSONArray) {

View file

@ -135,7 +135,7 @@ public class XMLTokener extends JSONTokener {
* @return A Character or an entity String if the entity is not recognized. * @return A Character or an entity String if the entity is not recognized.
* @throws JSONException If missing ';' in XML entity. * @throws JSONException If missing ';' in XML entity.
*/ */
public Object nextEntity(char ampersand) throws JSONException { public Object nextEntity(@SuppressWarnings("unused") char ampersand) throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (;;) { for (;;) {
char c = next(); char c = next();