Skip to content

Commit 4a38c80

Browse files
committed
Merge from upstream.
2 parents 2917cc5 + 2c228ec commit 4a38c80

4 files changed

Lines changed: 193 additions & 52 deletions

File tree

JSONArray.java

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

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

@@ -240,11 +240,15 @@ public boolean getBoolean(int index) throws JSONException {
240240
public double getDouble(int index) throws JSONException {
241241
Object object = this.get(index);
242242
try {
243-
return object instanceof Number ? ((Number) object).doubleValue()
244-
: Double.parseDouble((String) object);
243+
if (object instanceof Number) {
244+
return ((Number) object).doubleValue();
245+
} else if (object instanceof String) {
246+
return Double.parseDouble((String) object);
247+
}
245248
} catch (Exception e) {
246-
throw new JSONException("JSONArray[" + index + "] is not a number.");
249+
247250
}
251+
throw new JSONException("JSONArray[" + index + "] is not a number.");
248252
}
249253

250254
/**
@@ -324,11 +328,15 @@ public BigInteger getBigInteger (int index) throws JSONException {
324328
public int getInt(int index) throws JSONException {
325329
Object object = this.get(index);
326330
try {
327-
return object instanceof Number ? ((Number) object).intValue()
328-
: Integer.parseInt((String) object);
331+
if (object instanceof Number) {
332+
return ((Number) object).intValue();
333+
} else if (object instanceof String) {
334+
return Integer.parseInt((String) object);
335+
}
329336
} catch (Exception e) {
330-
throw new JSONException("JSONArray[" + index + "] is not a number.");
337+
331338
}
339+
throw new JSONException("JSONArray[" + index + "] is not a number.");
332340
}
333341

334342
/**
@@ -380,11 +388,15 @@ public JSONObject getJSONObject(int index) throws JSONException {
380388
public long getLong(int index) throws JSONException {
381389
Object object = this.get(index);
382390
try {
383-
return object instanceof Number ? ((Number) object).longValue()
384-
: Long.parseLong((String) object);
391+
if (object instanceof Number) {
392+
return ((Number) object).longValue();
393+
} else if (object instanceof String) {
394+
return Long.parseLong((String) object);
395+
}
385396
} catch (Exception e) {
386-
throw new JSONException("JSONArray[" + index + "] is not a number.");
397+
387398
}
399+
throw new JSONException("JSONArray[" + index + "] is not a number.");
388400
}
389401

390402
/**
@@ -485,11 +497,20 @@ public boolean optBoolean(int index) {
485497
* @return The truth.
486498
*/
487499
public boolean optBoolean(int index, boolean defaultValue) {
488-
try {
489-
return this.getBoolean(index);
490-
} catch (Exception e) {
500+
Object object = this.opt(index);
501+
if (JSONObject.NULL.equals(object)) {
491502
return defaultValue;
492503
}
504+
if (object.equals(Boolean.FALSE)
505+
|| (object instanceof String && ((String) object)
506+
.equalsIgnoreCase("false"))) {
507+
return false;
508+
} else if (object.equals(Boolean.TRUE)
509+
|| (object instanceof String && ((String) object)
510+
.equalsIgnoreCase("true"))) {
511+
return true;
512+
}
513+
return defaultValue;
493514
}
494515

495516
/**
@@ -517,11 +538,20 @@ public double optDouble(int index) {
517538
* @return The value.
518539
*/
519540
public double optDouble(int index, double defaultValue) {
541+
Object object = this.opt(index);
542+
if (JSONObject.NULL.equals(object)) {
543+
return defaultValue;
544+
}
520545
try {
521-
return this.getDouble(index);
546+
if (object instanceof Number) {
547+
return ((Number) object).doubleValue();
548+
} else if (object instanceof String) {
549+
return Double.parseDouble((String) object);
550+
}
522551
} catch (Exception e) {
523-
return defaultValue;
552+
524553
}
554+
return defaultValue;
525555
}
526556

527557
/**
@@ -549,11 +579,20 @@ public int optInt(int index) {
549579
* @return The value.
550580
*/
551581
public int optInt(int index, int defaultValue) {
582+
Object object = this.opt(index);
583+
if (JSONObject.NULL.equals(object)) {
584+
return defaultValue;
585+
}
552586
try {
553-
return this.getInt(index);
587+
if (object instanceof Number) {
588+
return ((Number) object).intValue();
589+
} else if (object instanceof String) {
590+
return Integer.parseInt((String) object);
591+
}
554592
} catch (Exception e) {
555-
return defaultValue;
593+
556594
}
595+
return defaultValue;
557596
}
558597

559598
/**
@@ -614,8 +653,12 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
614653
* @return The value.
615654
*/
616655
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
656+
Object object = this.opt(index);
657+
if (JSONObject.NULL.equals(object)) {
658+
return defaultValue;
659+
}
617660
try {
618-
return this.getBigInteger(index);
661+
return new BigInteger(object.toString());
619662
} catch (Exception e) {
620663
return defaultValue;
621664
}
@@ -633,8 +676,12 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
633676
* @return The value.
634677
*/
635678
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
679+
Object object = this.opt(index);
680+
if (JSONObject.NULL.equals(object)) {
681+
return defaultValue;
682+
}
636683
try {
637-
return this.getBigDecimal(index);
684+
return new BigDecimal(object.toString());
638685
} catch (Exception e) {
639686
return defaultValue;
640687
}
@@ -692,11 +739,20 @@ public long optLong(int index) {
692739
* @return The value.
693740
*/
694741
public long optLong(int index, long defaultValue) {
742+
Object object = this.opt(index);
743+
if (JSONObject.NULL.equals(object)) {
744+
return defaultValue;
745+
}
695746
try {
696-
return this.getLong(index);
747+
if (object instanceof Number) {
748+
return ((Number) object).longValue();
749+
} else if (object instanceof String) {
750+
return Long.parseLong((String) object);
751+
}
697752
} catch (Exception e) {
698-
return defaultValue;
753+
699754
}
755+
return defaultValue;
700756
}
701757

702758
/**
@@ -960,7 +1016,7 @@ public JSONArray put(int index, Object value) throws JSONException {
9601016
}
9611017

9621018
/**
963-
* Creates a JSONPointer using an intialization string and tries to
1019+
* Creates a JSONPointer using an initialization string and tries to
9641020
* match it to an item within this JSONArray. For example, given a
9651021
* JSONArray initialized with this document:
9661022
* <pre>
@@ -1080,6 +1136,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
10801136
* @return a printable, displayable, transmittable representation of the
10811137
* array.
10821138
*/
1139+
@Override
10831140
public String toString() {
10841141
try {
10851142
return this.toString(0);

0 commit comments

Comments
 (0)