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

adjustments to opt methods in reference to https://github.com/stleary/JSON-java/issues/334

This commit is contained in:
John J. Aylward 2017-05-16 19:38:01 -04:00
parent cbd8b18c4a
commit a8d4e4734f

View file

@ -540,7 +540,7 @@ public class JSONObject {
return new BigInteger(object.toString()); return new BigInteger(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigInteger."); + "] could not be converted to BigInteger.", e);
} }
} }
@ -556,11 +556,14 @@ 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) {
return (BigDecimal)object;
}
try { try {
return new BigDecimal(object.toString()); return new BigDecimal(object.toString());
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) throw new JSONException("JSONObject[" + quote(key)
+ "] could not be converted to BigDecimal."); + "] could not be converted to BigDecimal.", e);
} }
} }
@ -578,10 +581,10 @@ public class JSONObject {
Object object = this.get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? ((Number) object).doubleValue() return object instanceof Number ? ((Number) object).doubleValue()
: Double.parseDouble((String) object); : new BigDecimal((String) object).doubleValue();
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) throw new JSONException("JSONObject[" + quote(key)
+ "] is not a number."); + "] is not a number.", e);
} }
} }
@ -602,7 +605,7 @@ public class JSONObject {
: Integer.parseInt((String) object); : Integer.parseInt((String) object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) throw new JSONException("JSONObject[" + quote(key)
+ "] is not an int."); + "] is not an int.", e);
} }
} }
@ -659,7 +662,7 @@ public class JSONObject {
: Long.parseLong((String) object); : Long.parseLong((String) object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) throw new JSONException("JSONObject[" + quote(key)
+ "] is not a long."); + "] is not a long.", e);
} }
} }
@ -678,7 +681,7 @@ public class JSONObject {
int i = 0; int i = 0;
while (iterator.hasNext()) { while (iterator.hasNext()) {
names[i] = iterator.next(); names[i] = iterator.next();
i += 1; i++;
} }
return names; return names;
} }
@ -933,7 +936,15 @@ public class JSONObject {
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(String key, boolean defaultValue) { public boolean optBoolean(String key, boolean defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Boolean){
return ((Boolean) val).booleanValue();
}
try { try {
// we'll use the get anyway because it does string conversion.
return this.getBoolean(key); return this.getBoolean(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
@ -965,8 +976,23 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public BigInteger optBigInteger(String key, BigInteger defaultValue) { public BigInteger optBigInteger(String key, BigInteger defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof BigInteger){
return (BigInteger) val;
}
if (val instanceof BigDecimal){
return ((BigDecimal) val).toBigInteger();
}
try { try {
return this.getBigInteger(key); // the other opt functions handle implicit conversions, i.e.
// jo.put("double",1.1d);
// jo.optInt("double"); -- will return 1, not an error
// this conversion to BigDecimal then to BigInteger is to maintain
// that type cast support that may truncate the decimal.
return new BigDecimal(val.toString()).toBigInteger();
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -984,8 +1010,25 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) { public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof BigDecimal){
return (BigDecimal) val;
}
if (val instanceof BigInteger){
return new BigDecimal((BigInteger) val);
}
if (val instanceof Double){
return new BigDecimal(((Double) val).doubleValue());
}
if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){
return new BigDecimal(((Number) val).longValue());
}
try { try {
return this.getBigDecimal(key); return new BigDecimal(val.toString());
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -1003,11 +1046,21 @@ 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) {
try { Object val = this.opt(key);
return this.getDouble(key); if (NULL.equals(val)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){
return ((Number) val).doubleValue();
}
if (val instanceof String) {
try {
return new BigDecimal((String) val).doubleValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1035,11 +1088,22 @@ 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) {
try { Object val = this.opt(key);
return this.getInt(key); if (NULL.equals(val)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){
return ((Number) val).intValue();
}
if (val instanceof String) {
try {
return new BigDecimal(val.toString()).intValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1093,11 +1157,22 @@ 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) {
try { Object val = this.opt(key);
return this.getLong(key); if (NULL.equals(val)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (val instanceof Number){
return ((Number) val).longValue();
}
if (val instanceof String) {
try {
return new BigDecimal(val.toString()).longValue();
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
} }
/** /**
@ -1583,7 +1658,7 @@ public class JSONObject {
return d; return d;
} }
} else { } else {
Long myLong = new Long(string); Long myLong = Long.valueOf(string);
if (string.equals(myLong.toString())) { if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) { if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue()); return Integer.valueOf(myLong.intValue());