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:
parent
abf2963bbe
commit
3890bfae52
2 changed files with 170 additions and 58 deletions
113
JSONArray.java
113
JSONArray.java
|
@ -78,7 +78,7 @@ import java.util.Map;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-05-20
|
||||
* @version 2016-07-19
|
||||
*/
|
||||
public class JSONArray implements Iterable<Object> {
|
||||
|
||||
|
@ -156,9 +156,9 @@ public class JSONArray implements Iterable<Object> {
|
|||
public JSONArray(Collection<?> collection) {
|
||||
this.myArrayList = new ArrayList<Object>();
|
||||
if (collection != null) {
|
||||
for (Object o: collection){
|
||||
this.myArrayList.add(JSONObject.wrap(o));
|
||||
}
|
||||
for (Object o : collection) {
|
||||
this.myArrayList.add(JSONObject.wrap(o));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -241,11 +241,15 @@ public class JSONArray implements Iterable<Object> {
|
|||
public double getDouble(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).doubleValue()
|
||||
: Double.parseDouble((String) object);
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
|
||||
}
|
||||
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 {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).intValue()
|
||||
: Integer.parseInt((String) object);
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
|
||||
}
|
||||
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 {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).longValue()
|
||||
: Long.parseLong((String) object);
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -486,11 +498,20 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(int index, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
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.
|
||||
*/
|
||||
public double optDouble(int index, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
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.
|
||||
*/
|
||||
public int optInt(int index, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
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.
|
||||
*/
|
||||
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return this.getBigInteger(index);
|
||||
return new BigInteger(object.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -634,8 +677,12 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return this.getBigDecimal(index);
|
||||
return new BigDecimal(object.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -693,11 +740,20 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public long optLong(int index, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
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
|
||||
* JSONArray initialized with this document:
|
||||
* <pre>
|
||||
|
@ -1081,6 +1137,7 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return a printable, displayable, transmittable representation of the
|
||||
* array.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue