Skip to content

Commit

Permalink
Merge pull request #261 from stleary/revert-249-master
Browse files Browse the repository at this point in the history
Revert "reduces the use of unnecessary exceptions"
  • Loading branch information
stleary authored Aug 10, 2016
2 parents 8e07959 + 45a7dec commit 154cfda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 164 deletions.
107 changes: 25 additions & 82 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-07-19
* @version 2016-05-20
*/
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,15 +241,11 @@ public boolean getBoolean(int index) throws JSONException {
public double getDouble(int index) throws JSONException {
Object object = this.get(index);
try {
if (object instanceof Number) {
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
return object instanceof Number ? ((Number) object).doubleValue()
: 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 @@ -329,15 +325,11 @@ public BigInteger getBigInteger (int index) throws JSONException {
public int getInt(int index) throws JSONException {
Object object = this.get(index);
try {
if (object instanceof Number) {
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
return object instanceof Number ? ((Number) object).intValue()
: 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 @@ -389,15 +381,11 @@ public JSONObject getJSONObject(int index) throws JSONException {
public long getLong(int index) throws JSONException {
Object object = this.get(index);
try {
if (object instanceof Number) {
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
return object instanceof Number ? ((Number) object).longValue()
: 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 @@ -498,20 +486,11 @@ public boolean optBoolean(int index) {
* @return The truth.
*/
public boolean optBoolean(int index, boolean defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
try {
return this.getBoolean(index);
} catch (Exception e) {
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 @@ -539,20 +518,11 @@ 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 {
if (object instanceof Number) {
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
return this.getDouble(index);
} catch (Exception e) {

return defaultValue;
}
return defaultValue;
}

/**
Expand Down Expand Up @@ -580,20 +550,11 @@ 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 {
if (object instanceof Number) {
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
return this.getInt(index);
} catch (Exception e) {

return defaultValue;
}
return defaultValue;
}

/**
Expand Down Expand Up @@ -654,12 +615,8 @@ 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 new BigInteger(object.toString());
return this.getBigInteger(index);
} catch (Exception e) {
return defaultValue;
}
Expand All @@ -677,12 +634,8 @@ 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 new BigDecimal(object.toString());
return this.getBigDecimal(index);
} catch (Exception e) {
return defaultValue;
}
Expand Down Expand Up @@ -740,20 +693,11 @@ 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 {
if (object instanceof Number) {
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
return this.getLong(index);
} catch (Exception e) {

return defaultValue;
}
return defaultValue;
}

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

/**
* Creates a JSONPointer using an initialization string and tries to
* Creates a JSONPointer using an intialization 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 @@ -1137,7 +1081,6 @@ 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

0 comments on commit 154cfda

Please sign in to comment.