Skip to content

Commit e2b3573

Browse files
committed
let JSONArray and JSONObject implement Map and List
1 parent 9950350 commit e2b3573

File tree

3 files changed

+68
-99
lines changed

3 files changed

+68
-99
lines changed

JSONArray.java

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ of this software and associated documentation files (the "Software"), to deal
2828
import java.io.StringWriter;
2929
import java.io.Writer;
3030
import java.lang.reflect.Array;
31-
import java.math.*;
31+
import java.math.BigDecimal;
32+
import java.math.BigInteger;
3233
import java.util.ArrayList;
3334
import java.util.Collection;
34-
import java.util.Iterator;
3535
import java.util.Map;
3636

3737
/**
@@ -78,18 +78,13 @@ of this software and associated documentation files (the "Software"), to deal
7878
* @author JSON.org
7979
* @version 2015-10-29
8080
*/
81-
public class JSONArray implements Iterable<Object> {
82-
83-
/**
84-
* The arrayList where the JSONArray's properties are kept.
85-
*/
86-
private final ArrayList<Object> myArrayList;
81+
public class JSONArray extends ArrayList<Object> {
8782

8883
/**
8984
* Construct an empty JSONArray.
9085
*/
9186
public JSONArray() {
92-
this.myArrayList = new ArrayList<Object>();
87+
super();
9388
}
9489

9590
/**
@@ -110,10 +105,10 @@ public JSONArray(JSONTokener x) throws JSONException {
110105
for (;;) {
111106
if (x.nextClean() == ',') {
112107
x.back();
113-
this.myArrayList.add(JSONObject.NULL);
108+
super.add(JSONObject.NULL);
114109
} else {
115110
x.back();
116-
this.myArrayList.add(x.nextValue());
111+
super.add(x.nextValue());
117112
}
118113
switch (x.nextClean()) {
119114
case ',':
@@ -152,11 +147,11 @@ public JSONArray(String source) throws JSONException {
152147
* A Collection.
153148
*/
154149
public JSONArray(Collection<?> collection) {
155-
this.myArrayList = new ArrayList<Object>();
150+
this();
156151
if (collection != null) {
157-
for (Object o: collection){
158-
this.myArrayList.add(JSONObject.wrap(o));
159-
}
152+
for (Object o: collection){
153+
super.add(JSONObject.wrap(o));
154+
}
160155
}
161156
}
162157

@@ -179,11 +174,6 @@ public JSONArray(Object array) throws JSONException {
179174
}
180175
}
181176

182-
@Override
183-
public Iterator<Object> iterator() {
184-
return myArrayList.iterator();
185-
}
186-
187177
/**
188178
* Get the object value associated with an index.
189179
*
@@ -433,7 +423,7 @@ public String join(String separator) throws JSONException {
433423
if (i > 0) {
434424
sb.append(separator);
435425
}
436-
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
426+
sb.append(JSONObject.valueToString(super.get(i)));
437427
}
438428
return sb.toString();
439429
}
@@ -444,7 +434,7 @@ public String join(String separator) throws JSONException {
444434
* @return The length (or size).
445435
*/
446436
public int length() {
447-
return this.myArrayList.size();
437+
return size();
448438
}
449439

450440
/**
@@ -455,8 +445,7 @@ public int length() {
455445
* @return An object value, or null if there is no object at that index.
456446
*/
457447
public Object opt(int index) {
458-
return (index < 0 || index >= this.length()) ? null : this.myArrayList
459-
.get(index);
448+
return (index < 0 || index >= this.length()) ? null : super.get(index);
460449
}
461450

462451
/**
@@ -746,8 +735,7 @@ public JSONArray put(boolean value) {
746735
* @return this.
747736
*/
748737
public JSONArray put(Collection<?> value) {
749-
this.put(new JSONArray(value));
750-
return this;
738+
return put((Object) (value instanceof JSONArray ? value : new JSONArray(value)));
751739
}
752740

753741
/**
@@ -799,8 +787,7 @@ public JSONArray put(long value) {
799787
* @return this.
800788
*/
801789
public JSONArray put(Map<?, ?> value) {
802-
this.put(new JSONObject(value));
803-
return this;
790+
return put((Object) (value instanceof JSONObject ? value : new JSONObject(value)));
804791
}
805792

806793
/**
@@ -813,7 +800,7 @@ public JSONArray put(Map<?, ?> value) {
813800
* @return this.
814801
*/
815802
public JSONArray put(Object value) {
816-
this.myArrayList.add(value);
803+
super.add(value);
817804
return this;
818805
}
819806

@@ -848,8 +835,7 @@ public JSONArray put(int index, boolean value) throws JSONException {
848835
* If the index is negative or if the value is not finite.
849836
*/
850837
public JSONArray put(int index, Collection<?> value) throws JSONException {
851-
this.put(index, new JSONArray(value));
852-
return this;
838+
return put(index, (Object) (value instanceof JSONArray ? value : new JSONArray(value)));
853839
}
854840

855841
/**
@@ -920,8 +906,7 @@ public JSONArray put(int index, long value) throws JSONException {
920906
* number.
921907
*/
922908
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
923-
this.put(index, new JSONObject(value));
924-
return this;
909+
return put(index, (Object) (value instanceof JSONObject ? value : new JSONObject(value)));
925910
}
926911

927912
/**
@@ -946,7 +931,7 @@ public JSONArray put(int index, Object value) throws JSONException {
946931
throw new JSONException("JSONArray[" + index + "] not found.");
947932
}
948933
if (index < this.length()) {
949-
this.myArrayList.set(index, value);
934+
super.set(index, value);
950935
} else {
951936
while (index != this.length()) {
952937
this.put(JSONObject.NULL);
@@ -966,7 +951,7 @@ public JSONArray put(int index, Object value) throws JSONException {
966951
*/
967952
public Object remove(int index) {
968953
return index >= 0 && index < this.length()
969-
? this.myArrayList.remove(index)
954+
? super.remove(index)
970955
: null;
971956
}
972957

@@ -1100,7 +1085,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
11001085
writer.write('[');
11011086

11021087
if (length == 1) {
1103-
JSONObject.writeValue(writer, this.myArrayList.get(0),
1088+
JSONObject.writeValue(writer, super.get(0),
11041089
indentFactor, indent);
11051090
} else if (length != 0) {
11061091
final int newindent = indent + indentFactor;
@@ -1113,7 +1098,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
11131098
writer.write('\n');
11141099
}
11151100
JSONObject.indent(writer, newindent);
1116-
JSONObject.writeValue(writer, this.myArrayList.get(i),
1101+
JSONObject.writeValue(writer, super.get(i),
11171102
indentFactor, newindent);
11181103
commanate = true;
11191104
}

0 commit comments

Comments
 (0)