Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduces the use of unnecessary exceptions #249

Merged
merged 3 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 82 additions & 25 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2016-05-20
* @version 2016-07-19
*/
public class JSONArray implements Iterable<Object> {

Expand Down Expand Up @@ -156,9 +156,9 @@ public JSONArray(String source) throws JSONException {
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));
}
}
}

Expand Down Expand Up @@ -241,11 +241,15 @@ public boolean getBoolean(int index) throws JSONException {
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.");
}

/**
Expand Down Expand Up @@ -325,11 +329,15 @@ public BigInteger getBigInteger (int index) throws JSONException {
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.");
}

/**
Expand Down Expand Up @@ -381,11 +389,15 @@ public JSONObject getJSONObject(int index) throws JSONException {
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.");
}

/**
Expand Down Expand Up @@ -486,11 +498,20 @@ public boolean optBoolean(int index) {
* @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;
}

/**
Expand Down Expand Up @@ -518,11 +539,20 @@ public double optDouble(int index) {
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try {
return this.getDouble(index);
if (object instanceof Number) {
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} catch (Exception e) {
return defaultValue;

}
return defaultValue;
}

/**
Expand Down Expand Up @@ -550,11 +580,20 @@ public int optInt(int index) {
* @return The value.
*/
public int optInt(int index, int defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try {
return this.getInt(index);
if (object instanceof Number) {
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) {
return defaultValue;

}
return defaultValue;
}

/**
Expand Down Expand Up @@ -615,8 +654,12 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
* @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;
}
Expand All @@ -634,8 +677,12 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
* @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;
}
Expand Down Expand Up @@ -693,11 +740,20 @@ public long optLong(int index) {
* @return The value.
*/
public long optLong(int index, long defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try {
return this.getLong(index);
if (object instanceof Number) {
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) {
return defaultValue;

}
return defaultValue;
}

/**
Expand Down Expand Up @@ -961,7 +1017,7 @@ public JSONArray put(int index, Object value) throws JSONException {
}

/**
* 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>
Expand Down Expand Up @@ -1081,6 +1137,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
* @return a printable, displayable, transmittable representation of the
* array.
*/
@Override
public String toString() {
try {
return this.toString(0);
Expand Down
Loading