forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONWriter.java
More file actions
723 lines (676 loc) · 25.9 KB
/
JSONWriter.java
File metadata and controls
723 lines (676 loc) · 25.9 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package org.json;
/*
Copyright (c) 2006 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import org.json.util.ALStack;
import org.json.util.JSONPointerUtils;
import org.json.write.CompactWriterFactory;
import org.json.write.NullStructureWriter;
import org.json.write.PrettyWriterFactory;
import org.json.write.SimpleStructureWriter;
import org.json.write.StructureWriter;
import org.json.write.StructureWriterFactory;
import org.json.write.WriterUtil;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* JSONWriter provides a quick and convenient way of producing JSON text.
* The texts produced strictly conform to JSON syntax rules. By default,
* no whitespace is added, so the results are ready for transmission or storage.
* Each instance of JSONWriter can produce one JSON text.
* <p>
* A JSONWriter instance provides a <code>value</code> method for appending
* values to the
* text, and a <code>key</code>
* method for adding keys before values in objects. There are <code>array</code>
* and <code>endArray</code> methods that make and bound array values, and
* <code>object</code> and <code>endObject</code> methods which make and bound
* object values. All of these methods return the JSONWriter instance,
* permitting a cascade style. For example, <pre>
* new JSONWriter(myWriter)
* .object()
* .key("JSON")
* .value("Hello, World!")
* .endObject();</pre> which writes <pre>
* {"JSON":"Hello, World!"}</pre>
* <p>
* The first method called must be <code>array</code> or <code>object</code>,
* unless this object has been constructed with {@code allowSimpleValues}
* set to true.
* <p>
* There are no methods for adding commas or colons. JSONWriter adds them for
* you. Objects and arrays can be nested arbitrarily deep.
* <p>
* This can be less memory intensive than using a JSONObject to build a string.
*
* @author JSON.org
* @version 2016-09-18
*/
public class JSONWriter implements Closeable {
private static final int initdepth = 16;
/**
* Factory for creating strategy objects for writing a particular structure.
*/
private final StructureWriterFactory factory;
/**
* Strategy for writing JSON structures, parameterizes writing of
* JSON Objects and JSON Arrays, as well as pretty printing or compact.
*/
private StructureWriter currentStructure;
/**
* Indicate when writing has finished.
*/
protected boolean done;
/**
* The object/array stack.
*/
private final ALStack<StructureWriter> stack;
/**
* The writer that will receive the output.
*/
protected final Appendable writer;
/**
* Make a fresh JSONWriter. It can be used to build one JSON text.
*
* @param w the Writer to which JSON content will be written
*/
public JSONWriter(Appendable w) {
this.done = false;
this.stack = new ALStack<StructureWriter>(initdepth);
this.writer = w;
this.factory = CompactWriterFactory.INSTANCE;
this.currentStructure = NullStructureWriter.getInstance();
}
/**
* Make a fresh JSONWriter. It can be used to build one JSON text.
*
* @param w the Writer to which JSON content will be written
* @param allowSimpleValues {@code true} to allow the root value to be
* a simple value, otherwise {@code false} to only allow objects and arrays
* at the root level
*/
public JSONWriter(Appendable w, boolean allowSimpleValues) {
this.done = false;
this.stack = new ALStack<StructureWriter>(initdepth);
this.writer = w;
this.factory = CompactWriterFactory.INSTANCE;
this.currentStructure = allowSimpleValues ?
SimpleStructureWriter.getInstance(0) :
NullStructureWriter.getInstance();
}
/**
* Make a fresh JSONWriter with the given indent factor.
* It can be used to build one JSON text with pretty-printing.
*
* @param w the Writer to which JSON content will be written
* @param indentFactor indent level, or 0 for compact output
*/
public JSONWriter(Appendable w, int indentFactor) {
this.done = false;
this.stack = new ALStack<StructureWriter>(initdepth);
this.writer = w;
this.factory = (indentFactor > 0)
? new PrettyWriterFactory(indentFactor)
: CompactWriterFactory.INSTANCE;
this.currentStructure = NullStructureWriter.getInstance();
}
/**
* Make a fresh JSONWriter with the given indent factor and initial
* indentation. It can be used to build one JSON text with pretty-printing.
*
* @param w the Writer to which JSON content will be written
* @param indentFactor indent level, or 0 for compact output
* @param indent initial indent
*/
public JSONWriter(Appendable w, int indentFactor, int indent) {
this.done = false;
this.stack = new ALStack<StructureWriter>(initdepth);
this.writer = w;
this.factory = (indentFactor > 0)
? new PrettyWriterFactory(indentFactor, indent)
: CompactWriterFactory.INSTANCE;
this.currentStructure = NullStructureWriter.getInstance();
}
/**
* Make a fresh JSONWriter with the given indent factor and initial
* indentation. It can be used to build one JSON text with pretty-printing.
*
* @param w the Writer to which JSON content will be written
* @param indentFactor indent level, or 0 for compact output
* @param indent initial indent
* @param allowSimpleValues {@code true} to allow the root value to be
* a simple value, otherwise {@code false} to only allow objects and arrays
* at the root level
*/
public JSONWriter(Appendable w, int indentFactor, int indent,
boolean allowSimpleValues) {
this.done = false;
this.stack = new ALStack<StructureWriter>(initdepth);
this.writer = w;
this.factory = (indentFactor > 0)
? new PrettyWriterFactory(indentFactor, indent)
: CompactWriterFactory.INSTANCE;
this.currentStructure = allowSimpleValues ?
SimpleStructureWriter.getInstance(indent) :
NullStructureWriter.getInstance();
}
private String getLocation() {
return JSONPointerUtils.toJSONPointer(stack);
}
/**
* Begin appending a new array. All values until the balancing
* <code>endArray</code> will be appended to this array. The
* <code>endArray</code> method must be called to mark the array's end.
* @return this
* @throws JSONException If the nesting is too deep, or if the object is
* started in the wrong place (for example as a key or after the end of the
* outermost array or object).
*/
public JSONWriter array() throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced array.", getLocation());
}
currentStructure.subValue(writer);
currentStructure = factory.newArrayWriter(currentStructure);
stack.push(currentStructure);
currentStructure.beginStructure(writer);
return this;
}
/**
* End an array. This method most be called to balance calls to
* <code>array</code>.
* @return this
* @throws JSONException If incorrectly nested.
*/
public JSONWriter endArray() throws JSONException {
if(done || currentStructure.getStructureType() != 'a') {
throw new JSONWriterException("Misplaced endArray.", getLocation());
}
try {
done = currentStructure.endStructure(writer);
stack.pop();
if (stack.isEmpty()) {
currentStructure = null;
done = true;
} else {
currentStructure = stack.peek();
}
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
} catch (EmptyStackException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* End an object. This method most be called to balance calls to
* <code>object</code>.
* @return this
* @throws JSONException If incorrectly nested.
*/
public JSONWriter endObject() throws JSONException {
if(done || currentStructure.getStructureType() != 'o') {
throw new JSONWriterException("Misplaced endObject.", getLocation());
}
try {
done = currentStructure.endStructure(writer);
stack.pop();
if (stack.isEmpty()) {
currentStructure = null;
done = true;
} else {
currentStructure = stack.peek();
}
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
} catch (EmptyStackException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Append a key. The key will be associated with the next value. In an
* object, every value must be preceded by a key.
* @param string A key string.
* @return this
* @throws JSONException If the key is out of place. For example, keys
* do not belong in arrays or if the key is null.
*/
public JSONWriter key(String string) throws JSONException {
if(done) {
throw new JSONWriterException("Misplaced key.", getLocation());
}
try {
currentStructure.key(string, writer);
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Begin appending a new object. All keys and values until the balancing
* <code>endObject</code> will be appended to this object. The
* <code>endObject</code> method must be called to mark the object's end.
* @return this
* @throws JSONException If the nesting is too deep, or if the object is
* started in the wrong place (for example as a key or after the end of the
* outermost array or object).
*/
public JSONWriter object() throws JSONException {
if(done) {
throw new JSONWriterException("Misplaced object.", getLocation());
}
try {
currentStructure.subValue(writer);
currentStructure = factory.newObjectWriter(currentStructure);
stack.push(currentStructure);
currentStructure.beginStructure(writer);
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Append a <code>null</code> value.
* @return this
* @throws JSONException there was a problem writing the null value
*/
public JSONWriter nullValue() throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
done = currentStructure.nullValue(writer);
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Append either the value <code>true</code> or the value
* <code>false</code>.
* @param b A boolean.
* @return this
* @throws JSONException there was a problem writing the boolean value
*/
public JSONWriter value(boolean b) throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
done = currentStructure.booleanValue(b, writer);
} catch (JSONException e) {
throw new JSONWriterException(e.getMessage(), e, getLocation());
}
return this;
}
/**
* Append a double value.
* @param d A double.
* @return this
* @throws JSONException If the number is not finite.
*/
public JSONWriter value(double d) throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
done = currentStructure.doubleValue(d, writer);
} catch (JSONException e) {
throw new JSONWriterException(e.getMessage(), e, getLocation());
}
return this;
}
/**
* Append a long value.
* @param l A long.
* @return this
* @throws JSONException there was a problem writing the long value
*/
public JSONWriter value(long l) throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
done = currentStructure.longValue(l, writer);
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Append an object value.
* @param object The object to appendString. It can be null, or a Boolean, Number,
* String, JSONObject, or JSONArray, or an object that implements JSONString.
* @return this
* @throws JSONException If the value is out of sequence.
*/
public JSONWriter value(Object object) throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
done = writeValue(object);
} catch (JSONWriterException e) {
throw e; // propagate
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Append a sequence of values into an array.
*
* @param values The objects to appendString. They can be null, or a Boolean, Number,
* String, JSONObject, or JSONArray, or an object that implements JSONString.
* @return this
* @throws JSONException If a value is out of place. For example, a value
* occurs where a key is expected.
*/
public JSONWriter values(Iterable<?> values) throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
for(Object obj : values) {
writeValue(obj);
}
} catch (JSONWriterException e) {
throw e; // propagate
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Append a sequence of keys and values in an object.
* The key will be associated with the corresponding value. In an
* object, every value must be associated with a key.
*
* @param kvPairs The objects to appendString. The values can be null, or a Boolean,
* Number, String, JSONObject, or JSONArray, or an object that implements
* JSONString.
* @return this
* @throws JSONException If a key or value is out of place. For example, keys
* do not belong in arrays or if the key is null.
*/
public JSONWriter entries(Map<?, ?> kvPairs) throws JSONException {
if (done) {
throw new JSONWriterException("Misplaced value.", getLocation());
}
try {
for(Entry<?, ?> entry : kvPairs.entrySet()) {
currentStructure.key(String.valueOf(entry.getKey()), writer);
writeValue(entry.getValue());
}
} catch (JSONWriterException e) {
throw e; // propagate
} catch (JSONException e) {
throw new JSONWriterException(e, getLocation());
}
return this;
}
/**
* Asserts the JSON writer is finished, and close any underlying
* {@code Closeable} writer.
*
* @throws IOException the writer cannot be closed
*/
@Override
public void close() throws IOException {
if(!done) {
throw new IOException("JSON stack is not empty");
}
if(writer instanceof Closeable) {
((Closeable)writer).close();
}
}
/**
* Write the contents of the Object as JSON text to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The value to be written
* @throws JSONException there was a problem writing the Object
*/
private boolean writeValue(Object value) throws JSONException {
if(WriterUtil.isSimpleValue(value)) {
return currentStructure.simpleValue(value, writer);
}
if (value instanceof JSONAppendable) {
return jsonAppendableValue((JSONAppendable) value);
}
if (value instanceof JSONString) {
return jsonStringValue((JSONString) value);
}
if (value instanceof JSONObject) {
return jsonObjectValue((JSONObject) value);
}
if (value instanceof JSONArray) {
return jsonArrayValue((JSONArray) value);
}
if (value instanceof Map) {
return mapValue((Map<?, ?>) value);
}
if (value instanceof Iterable) {
return iterableValue((Iterable<?>) value);
}
if (value.getClass().isArray()) {
return arrayValue(value);
}
if (JSONObject.objectIsBean(value)) {
return beanValue(value);
}
return currentStructure.simpleValue(value.toString(), writer);
}
/**
* Write the JSONAppendable as a JSON value to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The JSONString to be written
* @throws JSONException there was a problem writing the JSONAppendable
*/
private boolean jsonAppendableValue(JSONAppendable value) throws JSONException {
try {
return currentStructure.jsonAppendableValue(value, writer);
} catch(JSONException e) {
// Propagate directly, because JSONException is a RuntimeException
throw e;
} catch(RuntimeException e) {
throw new JSONException(e);
}
}
/**
* Write the contents of the JSONString as a JSON value to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The JSONString to be written
* @throws JSONException there was a problem writing the JSONString
*/
private boolean jsonStringValue(JSONString value) throws JSONException {
try {
return currentStructure.jsonStringValue(value, writer);
} catch(JSONException e) {
// Propagate directly, because JSONException is a RuntimeException
throw e;
} catch (RuntimeException e) {
throw new JSONException(e);
}
}
/**
* Write the contents of the JSONObject as JSON object to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param object
* The JSONObject to be written
* @throws JSONException there was a problem writing the JSONObject
*/
private boolean jsonObjectValue(JSONObject object) throws JSONException {
Iterator<String> keys = object.keys();
object();
while (keys.hasNext()) {
String key = keys.next();
key(key).writeValue(object.opt(key));
}
endObject();
return stack.isEmpty();
}
/**
* Write the contents of the JSONArray as JSON array to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param array
* The JSONArray to be written
* @throws JSONException there was a problem writing the JSONArray
*/
private boolean jsonArrayValue(JSONArray array) throws JSONException {
final int length = array.length();
array();
if (length != 0) {
for (int i = 0; i < length; i += 1) {
writeValue(array.get(i));
}
}
endArray();
return stack.isEmpty();
}
/**
* Write the contents of the Map as JSON object to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param map
* The Map to be written
* @throws JSONException there was a problem writing the Map
*/
private boolean mapValue(Map<?, ?> map) throws JSONException {
final int length = map.size();
object();
if (length != 0) {
Iterator<?> keys = map.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
key(String.valueOf(key)).writeValue(map.get(key));
}
}
endObject();
return stack.isEmpty();
}
/**
* Write the contents of the Iterable as JSON array to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param collection
* The Iterable to be written
* @throws JSONException there was a problem writing the Iterable
*/
private boolean iterableValue(Iterable<?> collection) throws JSONException {
Iterator<?> iterator = collection.iterator();
array();
while (iterator.hasNext()) {
writeValue(iterator.next());
}
endArray();
return stack.isEmpty();
}
/**
* Write the contents of the array as JSON array to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param array
* The array to be written
* @throws JSONException there was a problem writing the array
*/
private boolean arrayValue(Object array) throws JSONException {
try {
final int length = Array.getLength(array);
array();
for (int i = 0; i < length; i += 1) {
writeValue(Array.get(array, i));
}
endArray();
return stack.isEmpty();
} catch (IllegalArgumentException e) {
throw new JSONWriterException(e, getLocation());
} catch (ArrayIndexOutOfBoundsException e) {
throw new JSONWriterException(e, getLocation());
}
}
/**
* Write the contents of the JavaBean as JSON object to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param bean
* The bean to be written
* @throws JSONException there was a problem writing the bean
*/
private boolean beanValue(Object bean) throws JSONException {
try {
// If klass is a System class then set includeSuperClass to false.
Class<?> klass = bean.getClass();
boolean includeSuperClass = klass.getClassLoader() != null;
Method[] methods = includeSuperClass ? klass.getMethods()
: klass.getDeclaredMethods();
object();
for (int i = 0; i < methods.length; i += 1) {
try {
Method method = methods[i];
if (Modifier.isPublic(method.getModifiers()) &&
!Modifier.isStatic(method.getModifiers()) &&
!method.isSynthetic() &&
(method.getReturnType() != Void.TYPE)) {
String name = method.getName();
String key = JSONObject.keyFromMethodName(name);
if ((key != null)
&& (method.getParameterTypes().length == 0)) {
Object result = method.invoke(bean, (Object[]) null);
if (result != null) {
key(String.valueOf(key)).writeValue(result);
}
}
}
} catch (Exception ignore) {
}
}
endObject();
return stack.isEmpty();
} catch (JSONException exception) {
throw exception;
} catch (RuntimeException exception) {
throw new JSONException(exception);
}
}
}