-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONArray.java
More file actions
335 lines (291 loc) · 11.1 KB
/
JSONArray.java
File metadata and controls
335 lines (291 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package javaxt.json;
import javaxt.json.JSONObject.JSONTokener;
import java.io.IOException;
import java.io.Writer;
import javaxt.utils.Value;
//******************************************************************************
//** JSONArray
//******************************************************************************
/**
* A JSON array is simply an array of objects. The string representation of a
* JSON array is a widely-used standard format for exchanging data. The string
* begins with a left square bracket "[" and ends with a right square bracket
* "]". Each object in the array is separated by comma ",".
*
* @author Source adapted from json.org (2016-08-15)
*
******************************************************************************/
public class JSONArray implements Iterable<JSONValue> {
private final java.util.ArrayList<JSONValue> arr;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Used to create a new/empty array.
*/
public JSONArray() {
arr = new java.util.ArrayList<JSONValue>();
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Used to create a new array from a String (e.g. "[1,2,3]").
*/
public JSONArray(String source) throws JSONException {
this(new JSONTokener(source));
}
//**************************************************************************
//** Constructor
//**************************************************************************
protected JSONArray(JSONTokener x) throws JSONException {
this();
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
char nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
}
if (nextChar != ']') {
x.back();
for (;;) {
if (x.nextClean() == ',') {
x.back();
//arr.add(JSONObject.NULL);
} else {
x.back();
add(x.nextValue());
}
switch (x.nextClean()) {
case 0:
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
case ',':
nextChar = x.nextClean();
if (nextChar == 0) {
// array is unclosed. No ']' found, instead EOF
throw x.syntaxError("Expected a ',' or ']'");
}
if (nextChar == ']') {
return;
}
x.back();
break;
case ']':
return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
}
}
}
//**************************************************************************
//** iterator
//**************************************************************************
@Override
public java.util.Iterator<JSONValue> iterator() {
return arr.iterator();
}
//**************************************************************************
//** length
//**************************************************************************
/** Returns the number of elements in the JSONArray, included nulls.
*/
public int length() {
return arr.size();
}
//**************************************************************************
//** isEmpty
//**************************************************************************
/** Returns true if there are no entries in the array.
*/
public boolean isEmpty(){
return arr.isEmpty();
}
//**************************************************************************
//** get
//**************************************************************************
/** Returns the object value associated with an index.
*/
public JSONValue get(int index) {
return (index < 0 || index >= this.length()) ? new JSONValue(null) : arr.get(index);
}
//**************************************************************************
//** add
//**************************************************************************
/** Appends an object to the array.
*/
public void add(Object object) throws JSONException {
JSONValue v;
Object o;
if (object instanceof JSONValue){
v = (JSONValue) object;
o = v.toObject();
}
else if (object instanceof Value){
o = ((Value) object).toObject();
v = new JSONValue(object);
}
else{
o = object;
v = new JSONValue(object);
}
JSONObject.testValidity(o);
arr.add(v);
}
//**************************************************************************
//** set
//**************************************************************************
/** Updates an object to the array. Returns the original value that was
* associated with the index.
*/
public JSONValue set(int index, Object object){
JSONValue v;
Object o;
if (object instanceof JSONValue){
v = (JSONValue) object;
o = v.toObject();
}
else if (object instanceof Value){
o = ((Value) object).toObject();
v = new JSONValue(object);
}
else{
o = object;
v = new JSONValue(object);
}
JSONObject.testValidity(o);
Object obj = arr.set(index, v);
return new JSONValue(obj);
}
//**************************************************************************
//** remove
//**************************************************************************
/** Remove entry. Returns the value that was associated with the index.
*/
public JSONValue remove(int index) {
return index >= 0 && index < this.length()
? new JSONValue(arr.remove(index))
: new JSONValue(null);
}
//**************************************************************************
//** equals
//**************************************************************************
/** Returns true if the given object is a JSONArray and the JSONArray
* contains the same entries as this array. Order is important.
*/
public boolean equals(Object obj){
if (obj instanceof JSONArray){
JSONArray arr = (JSONArray) obj;
if (arr.length()==this.length()){
for (int i=0; i<this.arr.size(); i++){
Object val = this.arr.get(i);
Object val2 = arr.get(i).toObject();
if (val==null){
if (val2!=null) return false;
}
else{
if (!val.equals(val2)) return false;
}
}
return true;
}
}
return false;
}
//**************************************************************************
//** toArray
//**************************************************************************
/** Returns an array of Objects backing the JSONArray
*/
public Object[] toArray(){
Object[] out = new Object[arr.size()];
for (int i=0; i<out.length; i++){
out[i] = arr.get(i).toObject();
}
return out;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns a printable, displayable, transmittable representation of the
* array. For compactness, no unnecessary whitespace is added. If it is not
* possible to produce a syntactically correct JSON text then null will be
* returned instead.
*/
@Override
public String toString() {
try {
return this.toString(0);
}
catch (Exception e) {
return null;
}
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns a printable, displayable, transmittable representation of the
* array.
*/
public String toString(int indentFactor) {
try{
java.io.StringWriter sw = new java.io.StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
}
catch(Exception e){
return null;
}
}
//**************************************************************************
//** write
//**************************************************************************
/** Write the contents of the JSONArray as JSON text to a writer.
*/
protected Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
try {
boolean commanate = false;
int length = this.length();
writer.write('[');
if (length == 1) {
try {
JSONObject.writeValue(writer, arr.get(0).toObject(), indentFactor, indent);
}
catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: 0", e);
}
}
else if (length != 0) {
final int newindent = indent + indentFactor;
for (int i = 0; i < length; i += 1) {
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
JSONObject.indent(writer, newindent);
try {
JSONObject.writeValue(writer, arr.get(i).toObject(), indentFactor, newindent);
}
catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
}
commanate = true;
}
if (indentFactor > 0) {
writer.write('\n');
}
JSONObject.indent(writer, indent);
}
writer.write(']');
return writer;
}
catch (IOException e) {
throw new JSONException(e);
}
}
}