Skip to content

Commit 154cfda

Browse files
authored
Merge pull request stleary#261 from stleary/revert-249-master
Revert "reduces the use of unnecessary exceptions"
2 parents 8e07959 + 45a7dec commit 154cfda

File tree

2 files changed

+52
-164
lines changed

2 files changed

+52
-164
lines changed

JSONArray.java

Lines changed: 25 additions & 82 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-07-19
81+
* @version 2016-05-20
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,15 +241,11 @@ public boolean getBoolean(int index) throws JSONException {
241241
public double getDouble(int index) throws JSONException {
242242
Object object = this.get(index);
243243
try {
244-
if (object instanceof Number) {
245-
return ((Number) object).doubleValue();
246-
} else if (object instanceof String) {
247-
return Double.parseDouble((String) object);
248-
}
244+
return object instanceof Number ? ((Number) object).doubleValue()
245+
: Double.parseDouble((String) object);
249246
} catch (Exception e) {
250-
247+
throw new JSONException("JSONArray[" + index + "] is not a number.");
251248
}
252-
throw new JSONException("JSONArray[" + index + "] is not a number.");
253249
}
254250

255251
/**
@@ -329,15 +325,11 @@ public BigInteger getBigInteger (int index) throws JSONException {
329325
public int getInt(int index) throws JSONException {
330326
Object object = this.get(index);
331327
try {
332-
if (object instanceof Number) {
333-
return ((Number) object).intValue();
334-
} else if (object instanceof String) {
335-
return Integer.parseInt((String) object);
336-
}
328+
return object instanceof Number ? ((Number) object).intValue()
329+
: Integer.parseInt((String) object);
337330
} catch (Exception e) {
338-
331+
throw new JSONException("JSONArray[" + index + "] is not a number.");
339332
}
340-
throw new JSONException("JSONArray[" + index + "] is not a number.");
341333
}
342334

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

403391
/**
@@ -498,20 +486,11 @@ public boolean optBoolean(int index) {
498486
* @return The truth.
499487
*/
500488
public boolean optBoolean(int index, boolean defaultValue) {
501-
Object object = this.opt(index);
502-
if (JSONObject.NULL.equals(object)) {
489+
try {
490+
return this.getBoolean(index);
491+
} catch (Exception e) {
503492
return defaultValue;
504493
}
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;
515494
}
516495

517496
/**
@@ -539,20 +518,11 @@ public double optDouble(int index) {
539518
* @return The value.
540519
*/
541520
public double optDouble(int index, double defaultValue) {
542-
Object object = this.opt(index);
543-
if (JSONObject.NULL.equals(object)) {
544-
return defaultValue;
545-
}
546521
try {
547-
if (object instanceof Number) {
548-
return ((Number) object).doubleValue();
549-
} else if (object instanceof String) {
550-
return Double.parseDouble((String) object);
551-
}
522+
return this.getDouble(index);
552523
} catch (Exception e) {
553-
524+
return defaultValue;
554525
}
555-
return defaultValue;
556526
}
557527

558528
/**
@@ -580,20 +550,11 @@ public int optInt(int index) {
580550
* @return The value.
581551
*/
582552
public int optInt(int index, int defaultValue) {
583-
Object object = this.opt(index);
584-
if (JSONObject.NULL.equals(object)) {
585-
return defaultValue;
586-
}
587553
try {
588-
if (object instanceof Number) {
589-
return ((Number) object).intValue();
590-
} else if (object instanceof String) {
591-
return Integer.parseInt((String) object);
592-
}
554+
return this.getInt(index);
593555
} catch (Exception e) {
594-
556+
return defaultValue;
595557
}
596-
return defaultValue;
597558
}
598559

599560
/**
@@ -654,12 +615,8 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
654615
* @return The value.
655616
*/
656617
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
657-
Object object = this.opt(index);
658-
if (JSONObject.NULL.equals(object)) {
659-
return defaultValue;
660-
}
661618
try {
662-
return new BigInteger(object.toString());
619+
return this.getBigInteger(index);
663620
} catch (Exception e) {
664621
return defaultValue;
665622
}
@@ -677,12 +634,8 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
677634
* @return The value.
678635
*/
679636
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
680-
Object object = this.opt(index);
681-
if (JSONObject.NULL.equals(object)) {
682-
return defaultValue;
683-
}
684637
try {
685-
return new BigDecimal(object.toString());
638+
return this.getBigDecimal(index);
686639
} catch (Exception e) {
687640
return defaultValue;
688641
}
@@ -740,20 +693,11 @@ public long optLong(int index) {
740693
* @return The value.
741694
*/
742695
public long optLong(int index, long defaultValue) {
743-
Object object = this.opt(index);
744-
if (JSONObject.NULL.equals(object)) {
745-
return defaultValue;
746-
}
747696
try {
748-
if (object instanceof Number) {
749-
return ((Number) object).longValue();
750-
} else if (object instanceof String) {
751-
return Long.parseLong((String) object);
752-
}
697+
return this.getLong(index);
753698
} catch (Exception e) {
754-
699+
return defaultValue;
755700
}
756-
return defaultValue;
757701
}
758702

759703
/**
@@ -1017,7 +961,7 @@ public JSONArray put(int index, Object value) throws JSONException {
1017961
}
1018962

1019963
/**
1020-
* Creates a JSONPointer using an initialization string and tries to
964+
* Creates a JSONPointer using an intialization string and tries to
1021965
* match it to an item within this JSONArray. For example, given a
1022966
* JSONArray initialized with this document:
1023967
* <pre>
@@ -1137,7 +1081,6 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
11371081
* @return a printable, displayable, transmittable representation of the
11381082
* array.
11391083
*/
1140-
@Override
11411084
public String toString() {
11421085
try {
11431086
return this.toString(0);

0 commit comments

Comments
 (0)