-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonArray.java
More file actions
472 lines (376 loc) · 12.4 KB
/
JsonArray.java
File metadata and controls
472 lines (376 loc) · 12.4 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.lupcode.JSON;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import com.lupcode.JSON.exceptions.JsonParseException;
import com.lupcode.JSON.exceptions.JsonParseUnfinishedException;
import com.lupcode.JSON.utils.LineColumnTracker;
import com.lupcode.Utilities.streams.UTF8CharInputStream;
/** Represents a JSON array
* @author LupCode.com (Luca Vogels)
* @since 2020-12-23
*/
public class JsonArray extends JSON<JsonArray> implements List<JSON<?>> {
private ArrayList<JSON<?>> values = new ArrayList<>();
public JsonArray() {
}
public JsonArray(JSON<?>... values) {
addAll(values);
}
public JsonArray(Collection<JSON<?>> values) {
addAll(values);
}
JsonArray(Set<String> values){
if(values!=null){
for(String v : values){
this.values.add(v!=null ? new JsonString(v) : new JsonNull());
}
}
}
public int size(){
return values.size();
}
public JSON<?> get(int index){
return values.get(index);
}
public JsonObject getAsJsonObject(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonObject)obj : null;
}
public JsonArray getAsJsonArray(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonArray)obj : null;
}
public String getAsString(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonString)obj).getValue() : null;
}
public Byte getAsByte(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsByte() : null;
}
public Short getAsShort(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsShort() : null;
}
public Integer getAsInt(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsInt() : null;
}
public Long getAsLong(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsLong() : null;
}
public Float getAsFloat(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsFloat() : null;
}
public Double getAsDouble(int index) {
JSON<?> obj = values.get(index);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsDouble() : null;
}
public JsonArray insert(int index, JSON<?> value){
this.values.add(index, value!=null ? value : new JsonNull()); return this;
}
public JsonArray insert(int index, String value){
return insert(index, new JsonString(value));
}
public JsonArray insert(int index, boolean value){
return insert(index, new JsonBoolean(value));
}
public JsonArray insert(int index, double value){
return insert(index, new JsonNumber(value));
}
public JsonArray insert(int index, float value){
return insert(index, new JsonNumber(value));
}
public JsonArray insert(int index, int value){
return insert(index, new JsonNumber(value));
}
public JsonArray insert(int index, long value){
return insert(index, new JsonNumber(value));
}
public JsonArray insert(int index, short value){
return insert(index, new JsonNumber(value));
}
public JsonArray insert(int index, byte value){
return insert(index, new JsonNumber(value));
}
public JsonArray set(int index, JSON<?> value){
this.values.set(index, value!=null ? value : new JsonNull()); return this;
}
public JsonArray set(int index, String value){
return set(index, new JsonString(value));
}
public JsonArray set(int index, boolean value){
return set(index, new JsonBoolean(value));
}
public JsonArray set(int index, double value){
return set(index, new JsonNumber(value));
}
public JsonArray set(int index, float value){
return set(index, new JsonNumber(value));
}
public JsonArray set(int index, int value){
return set(index, new JsonNumber(value));
}
public JsonArray set(int index, long value){
return set(index, new JsonNumber(value));
}
public JsonArray set(int index, short value){
return set(index, new JsonNumber(value));
}
public JsonArray set(int index, byte value){
return set(index, new JsonNumber(value));
}
public boolean add(String value){
return add(value!=null ? new JsonString(value) : new JsonNull());
}
public boolean add(boolean value){
return add(new JsonBoolean(value));
}
public boolean add(double value){
return add(new JsonNumber(value));
}
public boolean add(float value){
return add(new JsonNumber(value));
}
public boolean add(int value){
return add(new JsonNumber(value));
}
public boolean add(long value){
return add(new JsonNumber(value));
}
public boolean add(short value){
return add(new JsonNumber(value));
}
public boolean add(byte value){
return add(new JsonNumber(value));
}
@Override
public boolean add(JSON<?> e) {
return values.add(e!=null ? e : new JsonNull());
}
public boolean addArray(JSON<?>... values){
return add((JSON<?>)new JsonArray(values));
}
public boolean addArray(Collection<JSON<?>> values){
return add((JSON<?>)new JsonArray(values));
}
public JsonArray addAll(JSON<?>... values){
if(values!=null){
for(JSON<?> json : values){
this.values.add(json!=null ? json : new JsonNull());
}
} return this;
}
public JSON<?> remove(int index){
return values.remove(index);
}
public boolean remove(JSON<?> object){
return values.remove(object);
}
public boolean contains(JSON<?> object){
return values.contains(object);
}
public List<JSON<?>> toList(int fromIndex, int toIndex){
return new ArrayList<>(values.subList(fromIndex, toIndex));
}
public List<JSON<?>> toList(){
return new ArrayList<>(values);
}
public JsonObject toJsonObject(){
return new JsonObject(values);
}
@Override
protected void toJSON(OutputStream output, boolean prettyPrint, String whitespace) throws IOException {
output.write("[".getBytes(StandardCharsets.UTF_8));
if(!values.isEmpty()){
if(prettyPrint){
boolean notFirst = false;
String ws = (whitespace + SPACER);
byte[] wsArr = (whitespace + SPACER).getBytes(StandardCharsets.UTF_8);
for(JSON<?> obj : values){
if(notFirst) output.write(",".getBytes(StandardCharsets.UTF_8)); else notFirst = true;
output.write(LINE_BREAKER.getBytes());
output.write(wsArr);
obj.toJSON(output, prettyPrint, ws);
}
output.write(LINE_BREAKER.getBytes());
output.write(whitespace.getBytes(StandardCharsets.UTF_8));
} else {
boolean notFirst = false;
for(JSON<?> obj : values){
if(notFirst) output.write(",".getBytes(StandardCharsets.UTF_8)); else notFirst = true;
obj.toJSON(output, prettyPrint, whitespace);
}
}
}
output.write("]".getBytes(StandardCharsets.UTF_8));
}
@Override
public boolean isEmpty() {
return values.isEmpty();
}
@Override
public boolean contains(Object o) {
return values.contains(o);
}
@Override
public Iterator<JSON<?>> iterator() {
return values.iterator();
}
@Override
public Object[] toArray() {
return values.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return values.toArray(a);
}
@Override
public boolean remove(Object o) {
return values.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return values.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends JSON<?>> c) {
boolean v = true;
for(JSON<?> o : c) {
v &= values.add(o!=null ? o : new JsonNull());
} return v;
}
@Override
public boolean addAll(int index, Collection<? extends JSON<?>> c) {
for(JSON<?> o : c) {
values.add(index++, o!=null ? o : new JsonNull());
} return true;
}
@Override
public boolean removeAll(Collection<?> c) {
return values.removeAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return values.retainAll(c);
}
@Override
public void clear() {
values.clear();
}
@Override
public void add(int index, JSON<?> element) {
values.add(index, element!=null ? element : new JsonNull());
}
@Override
public int indexOf(Object o) {
return values.indexOf(o!=null ? o : new JsonNull());
}
@Override
public int lastIndexOf(Object o) {
return values.lastIndexOf(o!=null ? o : new JsonNull());
}
@Override
public ListIterator<JSON<?>> listIterator() {
return values.listIterator();
}
@Override
public ListIterator<JSON<?>> listIterator(int index) {
return values.listIterator(index);
}
@Override
public List<JSON<?>> subList(int fromIndex, int toIndex) {
return values.subList(fromIndex, toIndex);
}
@Override
protected JsonArray parseJSON(UTF8CharInputStream input, LineColumnTracker lct) throws JsonParseException, IOException {
String c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('[', lct); }
if(!c.equals("[")){ throw new JsonParseException('[', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException(lct); }
if(c.equals("]")){ lct.increaseColumn(); this.values.clear(); return this; }
input.insertReadAgainAtBeginning(c);
boolean nextEntry = false;
ArrayList<JSON<?>> list = new ArrayList<>();
do {
if(nextEntry) lct.increaseColumn(); // increment for ','
// check if empty ','
c = skipIgnorers(input, lct);
if(c!=null && c.equals("]")) break;
input.insertReadAgainAtBeginning(c);
// read entry as JSON<?>
list.add(parseAutoJSON(input, lct));
c = skipIgnorers(input, lct);
} while((nextEntry = (c!=null && c.equals(","))));
if(c==null){ throw new JsonParseUnfinishedException(lct); }
if(!c.equals("]")){ throw new JsonParseException(']', c, lct); }
lct.increaseColumn();
this.values = list;
return this;
}
/** Tries to parse a given JSON string as a {@link JsonArray}
* @param json String that should be parsed
* @return Parsed JSON array
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonArray parse(String json) throws JsonParseException, NullPointerException {
return new JsonArray().parseJSON(json);
}
/** Tries to parse a given JSON string as a {@link JsonArray}
* @param json String that should be parsed
* @param offset Offset where to start reading the JSON string
* @return Parse JSON array
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonArray parse(String json, int offset) throws JsonParseException, NullPointerException {
return new JsonArray().parseJSON(json, offset);
}
/** Tries to parse a {@link JsonArray} from a given {@link InputStream} in UTF-8
* @param json Stream in UTF-8 that should be parsed
* @return Parse JSON array
* @throws JsonParseException if input could not be parsed correctly
* @throws IOException if an error while reading {@link InputStream} occurred
* @throws NullPointerException if input is null
*/
public static JsonArray parse(UTF8CharInputStream input) throws JsonParseException, IOException, NullPointerException {
return new JsonArray().parseJSON(input);
}
/**
* Tries to parse a {@link JsonArray} from a given {@link File}
* @param file File the JSON data should be read from
* @return Parsed JSON array
* @throws JsonParseException if file could not be parsed correctly
* @throws NullPointerException if file is null
* @throws IOException if an error occurs while reading the {@link File}
*/
public static JsonArray parse(File file) throws JsonParseException, NullPointerException, IOException{
return new JsonArray().parseJSON(file);
}
/**
* Tries to parse a {@link JsonArray} from a given {@link URL}
* @param url URL the JSON data should be read from
* @return Parsed JSON array
* @throws JsonParseException if {@link URL} could not be parsed correctly
* @throws NullPointerException if {@link URL} is null
* @throws IOException if an error occurs while reading the from the {@link URL}
*/
public static JsonArray parse(URL url) throws JsonParseException, NullPointerException, IOException{
return new JsonArray().parseJSON(url);
}
}