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

reduce the use of unnecessary exceptions

This commit is contained in:
Nils Faupel 2016-07-19 19:58:10 +02:00
parent abf2963bbe
commit 3890bfae52
2 changed files with 170 additions and 58 deletions

View file

@ -78,7 +78,7 @@ import java.util.Map;
* </ul> * </ul>
* *
* @author JSON.org * @author JSON.org
* @version 2016-05-20 * @version 2016-07-19
*/ */
public class JSONArray implements Iterable<Object> { public class JSONArray implements Iterable<Object> {
@ -241,11 +241,15 @@ public class JSONArray implements Iterable<Object> {
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
return object instanceof Number ? ((Number) object).doubleValue() if (object instanceof Number) {
: Double.parseDouble((String) object); return ((Number) object).doubleValue();
} catch (Exception e) { } else if (object instanceof String) {
throw new JSONException("JSONArray[" + index + "] is not a number."); return Double.parseDouble((String) object);
} }
} catch (Exception e) {
}
throw new JSONException("JSONArray[" + index + "] is not a number.");
} }
/** /**
@ -325,11 +329,15 @@ public class JSONArray implements Iterable<Object> {
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
return object instanceof Number ? ((Number) object).intValue() if (object instanceof Number) {
: Integer.parseInt((String) object); return ((Number) object).intValue();
} catch (Exception e) { } else if (object instanceof String) {
throw new JSONException("JSONArray[" + index + "] is not a number."); return Integer.parseInt((String) object);
} }
} catch (Exception e) {
}
throw new JSONException("JSONArray[" + index + "] is not a number.");
} }
/** /**
@ -381,11 +389,15 @@ public class JSONArray implements Iterable<Object> {
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
return object instanceof Number ? ((Number) object).longValue() if (object instanceof Number) {
: Long.parseLong((String) object); return ((Number) object).longValue();
} catch (Exception e) { } else if (object instanceof String) {
throw new JSONException("JSONArray[" + index + "] is not a number."); return Long.parseLong((String) object);
} }
} catch (Exception e) {
}
throw new JSONException("JSONArray[" + index + "] is not a number.");
} }
/** /**
@ -486,11 +498,20 @@ public class JSONArray implements Iterable<Object> {
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(int index, boolean defaultValue) { public boolean optBoolean(int index, boolean defaultValue) {
try { Object object = this.opt(index);
return this.getBoolean(index); if (JSONObject.NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
return defaultValue;
} }
/** /**
@ -518,11 +539,20 @@ 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) {
try { Object object = this.opt(index);
return this.getDouble(index); if (JSONObject.NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
try {
if (object instanceof Number) {
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} catch (Exception e) {
}
return defaultValue;
} }
/** /**
@ -550,11 +580,20 @@ 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) {
try { Object object = this.opt(index);
return this.getInt(index); if (JSONObject.NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
try {
if (object instanceof Number) {
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) {
}
return defaultValue;
} }
/** /**
@ -615,8 +654,12 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public BigInteger optBigInteger(int index, BigInteger defaultValue) { public BigInteger optBigInteger(int index, BigInteger defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
return this.getBigInteger(index); return new BigInteger(object.toString());
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -634,8 +677,12 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
return this.getBigDecimal(index); return new BigDecimal(object.toString());
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -693,11 +740,20 @@ 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) {
try { Object object = this.opt(index);
return this.getLong(index); if (JSONObject.NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
try {
if (object instanceof Number) {
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) {
}
return defaultValue;
} }
/** /**
@ -961,7 +1017,7 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Creates a JSONPointer using an intialization string and tries to * Creates a JSONPointer using an initialization string and tries to
* match it to an item within 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>
@ -1081,6 +1137,7 @@ public class JSONArray implements Iterable<Object> {
* @return a printable, displayable, transmittable representation of the * @return a printable, displayable, transmittable representation of the
* array. * array.
*/ */
@Override
public String toString() { public String toString() {
try { try {
return this.toString(0); return this.toString(0);

View file

@ -93,7 +93,7 @@ import java.util.Set;
* </ul> * </ul>
* *
* @author JSON.org * @author JSON.org
* @version 2016-05-20 * @version 2016-07-19
*/ */
public class JSONObject { public class JSONObject {
/** /**
@ -132,6 +132,7 @@ public class JSONObject {
* *
* @return The string "null". * @return The string "null".
*/ */
@Override
public String toString() { public String toString() {
return "null"; return "null";
} }
@ -576,13 +577,16 @@ public class JSONObject {
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? ((Number) object).doubleValue() if (object instanceof Number) {
: Double.parseDouble((String) object); return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} 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.");
} }
}
/** /**
* Get the int value associated with a key. * Get the int value associated with a key.
@ -597,12 +601,15 @@ public class JSONObject {
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? ((Number) object).intValue() if (object instanceof Number) {
: Integer.parseInt((String) object); return ((Number) object).intValue();
} catch (Exception e) { } else if (object instanceof String) {
throw new JSONException("JSONObject[" + quote(key) return Integer.parseInt((String) object);
+ "] is not an int.");
} }
} catch (Exception e) {
}
throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
} }
/** /**
@ -654,12 +661,15 @@ public class JSONObject {
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? ((Number) object).longValue() if (object instanceof Number) {
: Long.parseLong((String) object); return ((Number) object).longValue();
} catch (Exception e) { } else if (object instanceof String) {
throw new JSONException("JSONObject[" + quote(key) return Long.parseLong((String) object);
+ "] is not a long.");
} }
} catch (Exception e) {
}
throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
} }
/** /**
@ -932,11 +942,20 @@ public class JSONObject {
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(String key, boolean defaultValue) { public boolean optBoolean(String key, boolean defaultValue) {
try { Object object = this.get(key);
return this.getBoolean(key); if (NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
return defaultValue;
} }
/** /**
@ -964,8 +983,12 @@ 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 object = this.get(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
return this.getBigInteger(key); return new BigInteger(object.toString());
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -983,8 +1006,12 @@ 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 object = this.opt(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
return this.getBigDecimal(key); return new BigDecimal(object.toString());
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -1002,11 +1029,20 @@ 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 object = this.get(key);
return this.getDouble(key); if (NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
try {
if (object instanceof Number) {
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} catch (Exception e) {
}
return defaultValue;
} }
/** /**
@ -1034,11 +1070,20 @@ 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 object = this.get(key);
return this.getInt(key); if (NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
try {
if (object instanceof Number) {
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) {
}
return defaultValue;
} }
/** /**
@ -1092,11 +1137,20 @@ 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 object = this.get(key);
return this.getLong(key); if (NULL.equals(object)) {
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
try {
if (object instanceof Number) {
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) {
}
return defaultValue;
} }
/** /**
@ -1621,6 +1675,7 @@ public class JSONObject {
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right * brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>. * brace)</small>.
*/ */
@Override
public String toString() { public String toString() {
try { try {
return this.toString(0); return this.toString(0);
@ -1880,13 +1935,13 @@ public class JSONObject {
} }
/** /**
* Returns a java.util.Map containing all of the entrys in this object. * Returns a java.util.Map containing all of the entries in this object.
* If an entry in the object is a JSONArray or JSONObject it will also * If an entry in the object is a JSONArray or JSONObject it will also
* be converted. * be converted.
* <p> * <p>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* *
* @return a java.util.Map containing the entrys of this object * @return a java.util.Map containing the entries of this object
*/ */
public Map<String, Object> toMap() { public Map<String, Object> toMap() {
Map<String, Object> results = new HashMap<String, Object>(); Map<String, Object> results = new HashMap<String, Object>();