Skip to content

Commit 3890bfa

Browse files
author
Nils Faupel
committed
reduce the use of unnecessary exceptions
1 parent abf2963 commit 3890bfa

File tree

2 files changed

+164
-52
lines changed

2 files changed

+164
-52
lines changed

JSONArray.java

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ of this software and associated documentation files (the "Software"), to deal
7878
* </ul>
7979
*
8080
* @author JSON.org
81-
* @version 2016-05-20
81+
* @version 2016-07-19
8282
*/
8383
public class JSONArray implements Iterable<Object> {
8484

@@ -156,9 +156,9 @@ public JSONArray(String source) throws JSONException {
156156
public JSONArray(Collection<?> collection) {
157157
this.myArrayList = new ArrayList<Object>();
158158
if (collection != null) {
159-
for (Object o: collection){
160-
this.myArrayList.add(JSONObject.wrap(o));
161-
}
159+
for (Object o : collection) {
160+
this.myArrayList.add(JSONObject.wrap(o));
161+
}
162162
}
163163
}
164164

@@ -241,11 +241,15 @@ public boolean getBoolean(int index) throws JSONException {
241241
public double getDouble(int index) throws JSONException {
242242
Object object = this.get(index);
243243
try {
244-
return object instanceof Number ? ((Number) object).doubleValue()
245-
: Double.parseDouble((String) object);
244+
if (object instanceof Number) {
245+
return ((Number) object).doubleValue();
246+
} else if (object instanceof String) {
247+
return Double.parseDouble((String) object);
248+
}
246249
} catch (Exception e) {
247-
throw new JSONException("JSONArray[" + index + "] is not a number.");
250+
248251
}
252+
throw new JSONException("JSONArray[" + index + "] is not a number.");
249253
}
250254

251255
/**
@@ -325,11 +329,15 @@ public BigInteger getBigInteger (int index) throws JSONException {
325329
public int getInt(int index) throws JSONException {
326330
Object object = this.get(index);
327331
try {
328-
return object instanceof Number ? ((Number) object).intValue()
329-
: Integer.parseInt((String) object);
332+
if (object instanceof Number) {
333+
return ((Number) object).intValue();
334+
} else if (object instanceof String) {
335+
return Integer.parseInt((String) object);
336+
}
330337
} catch (Exception e) {
331-
throw new JSONException("JSONArray[" + index + "] is not a number.");
338+
332339
}
340+
throw new JSONException("JSONArray[" + index + "] is not a number.");
333341
}
334342

335343
/**
@@ -381,11 +389,15 @@ public JSONObject getJSONObject(int index) throws JSONException {
381389
public long getLong(int index) throws JSONException {
382390
Object object = this.get(index);
383391
try {
384-
return object instanceof Number ? ((Number) object).longValue()
385-
: Long.parseLong((String) object);
392+
if (object instanceof Number) {
393+
return ((Number) object).longValue();
394+
} else if (object instanceof String) {
395+
return Long.parseLong((String) object);
396+
}
386397
} catch (Exception e) {
387-
throw new JSONException("JSONArray[" + index + "] is not a number.");
398+
388399
}
400+
throw new JSONException("JSONArray[" + index + "] is not a number.");
389401
}
390402

391403
/**
@@ -486,11 +498,20 @@ public boolean optBoolean(int index) {
486498
* @return The truth.
487499
*/
488500
public boolean optBoolean(int index, boolean defaultValue) {
489-
try {
490-
return this.getBoolean(index);
491-
} catch (Exception e) {
501+
Object object = this.opt(index);
502+
if (JSONObject.NULL.equals(object)) {
492503
return defaultValue;
493504
}
505+
if (object.equals(Boolean.FALSE)
506+
|| (object instanceof String && ((String) object)
507+
.equalsIgnoreCase("false"))) {
508+
return false;
509+
} else if (object.equals(Boolean.TRUE)
510+
|| (object instanceof String && ((String) object)
511+
.equalsIgnoreCase("true"))) {
512+
return true;
513+
}
514+
return defaultValue;
494515
}
495516

496517
/**
@@ -518,11 +539,20 @@ public double optDouble(int index) {
518539
* @return The value.
519540
*/
520541
public double optDouble(int index, double defaultValue) {
542+
Object object = this.opt(index);
543+
if (JSONObject.NULL.equals(object)) {
544+
return defaultValue;
545+
}
521546
try {
522-
return this.getDouble(index);
547+
if (object instanceof Number) {
548+
return ((Number) object).doubleValue();
549+
} else if (object instanceof String) {
550+
return Double.parseDouble((String) object);
551+
}
523552
} catch (Exception e) {
524-
return defaultValue;
553+
525554
}
555+
return defaultValue;
526556
}
527557

528558
/**
@@ -550,11 +580,20 @@ public int optInt(int index) {
550580
* @return The value.
551581
*/
552582
public int optInt(int index, int defaultValue) {
583+
Object object = this.opt(index);
584+
if (JSONObject.NULL.equals(object)) {
585+
return defaultValue;
586+
}
553587
try {
554-
return this.getInt(index);
588+
if (object instanceof Number) {
589+
return ((Number) object).intValue();
590+
} else if (object instanceof String) {
591+
return Integer.parseInt((String) object);
592+
}
555593
} catch (Exception e) {
556-
return defaultValue;
594+
557595
}
596+
return defaultValue;
558597
}
559598

560599
/**
@@ -615,8 +654,12 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
615654
* @return The value.
616655
*/
617656
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
657+
Object object = this.opt(index);
658+
if (JSONObject.NULL.equals(object)) {
659+
return defaultValue;
660+
}
618661
try {
619-
return this.getBigInteger(index);
662+
return new BigInteger(object.toString());
620663
} catch (Exception e) {
621664
return defaultValue;
622665
}
@@ -634,8 +677,12 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
634677
* @return The value.
635678
*/
636679
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
680+
Object object = this.opt(index);
681+
if (JSONObject.NULL.equals(object)) {
682+
return defaultValue;
683+
}
637684
try {
638-
return this.getBigDecimal(index);
685+
return new BigDecimal(object.toString());
639686
} catch (Exception e) {
640687
return defaultValue;
641688
}
@@ -693,11 +740,20 @@ public long optLong(int index) {
693740
* @return The value.
694741
*/
695742
public long optLong(int index, long defaultValue) {
743+
Object object = this.opt(index);
744+
if (JSONObject.NULL.equals(object)) {
745+
return defaultValue;
746+
}
696747
try {
697-
return this.getLong(index);
748+
if (object instanceof Number) {
749+
return ((Number) object).longValue();
750+
} else if (object instanceof String) {
751+
return Long.parseLong((String) object);
752+
}
698753
} catch (Exception e) {
699-
return defaultValue;
754+
700755
}
756+
return defaultValue;
701757
}
702758

703759
/**
@@ -961,7 +1017,7 @@ public JSONArray put(int index, Object value) throws JSONException {
9611017
}
9621018

9631019
/**
964-
* Creates a JSONPointer using an intialization string and tries to
1020+
* Creates a JSONPointer using an initialization string and tries to
9651021
* match it to an item within this JSONArray. For example, given a
9661022
* JSONArray initialized with this document:
9671023
* <pre>
@@ -1081,6 +1137,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
10811137
* @return a printable, displayable, transmittable representation of the
10821138
* array.
10831139
*/
1140+
@Override
10841141
public String toString() {
10851142
try {
10861143
return this.toString(0);

0 commit comments

Comments
 (0)