source: main/trunk/model-sites-dev/cambridge-museum/collect/waikato-independent/pre-import/EditableDatabaseTable/src/org/json/JSONArray.java@ 34493

Last change on this file since 34493 was 34493, checked in by davidb, 4 years ago

Base project for providing jquery/jquery-ui controlled interface to editing a database table

File size: 29.0 KB
Line 
1package org.json;
2
3/*
4Copyright (c) 2002 JSON.org
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16The Software shall be used for Good, not Evil.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25*/
26
27import java.io.IOException;
28import java.io.Writer;
29import java.lang.reflect.Array;
30import java.util.ArrayList;
31import java.util.Collection;
32import java.util.Iterator;
33import java.util.Map;
34
35/**
36 * A JSONArray is an ordered sequence of values. Its external text form is a
37 * string wrapped in square brackets with commas separating the values. The
38 * internal form is an object having <code>get</code> and <code>opt</code>
39 * methods for accessing the values by index, and <code>put</code> methods for
40 * adding or replacing values. The values can be any of these types:
41 * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
42 * <code>Number</code>, <code>String</code>, or the
43 * <code>JSONObject.NULL object</code>.
44 * <p>
45 * The constructor can convert a JSON text into a Java object. The
46 * <code>toString</code> method converts to JSON text.
47 * <p>
48 * A <code>get</code> method returns a value if one can be found, and throws an
49 * exception if one cannot be found. An <code>opt</code> method returns a
50 * default value instead of throwing an exception, and so is useful for
51 * obtaining optional values.
52 * <p>
53 * The generic <code>get()</code> and <code>opt()</code> methods return an
54 * object which you can cast or query for type. There are also typed
55 * <code>get</code> and <code>opt</code> methods that do type checking and type
56 * coercion for you.
57 * <p>
58 * The texts produced by the <code>toString</code> methods strictly conform to
59 * JSON syntax rules. The constructors are more forgiving in the texts they will
60 * accept:
61 * <ul>
62 * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
63 * before the closing bracket.</li>
64 * <li>The <code>null</code> value will be inserted when there
65 * is <code>,</code>&nbsp;<small>(comma)</small> elision.</li>
66 * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
67 * quote)</small>.</li>
68 * <li>Strings do not need to be quoted at all if they do not begin with a quote
69 * or single quote, and if they do not contain leading or trailing spaces,
70 * and if they do not contain any of these characters:
71 * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers
72 * and if they are not the reserved words <code>true</code>,
73 * <code>false</code>, or <code>null</code>.</li>
74 * <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
75 * well as by <code>,</code> <small>(comma)</small>.</li>
76 * <li>Numbers may have the
77 * <code>0x-</code> <small>(hex)</small> prefix.</li>
78 * </ul>
79
80 * @author JSON.org
81 * @version 2010-12-28
82 */
83public class JSONArray {
84
85
86 /**
87 * The arrayList where the JSONArray's properties are kept.
88 */
89 private ArrayList myArrayList;
90
91
92 /**
93 * Construct an empty JSONArray.
94 */
95 public JSONArray() {
96 this.myArrayList = new ArrayList();
97 }
98
99 /**
100 * Construct a JSONArray from a JSONTokener.
101 * @param x A JSONTokener
102 * @throws JSONException If there is a syntax error.
103 */
104 public JSONArray(JSONTokener x) throws JSONException {
105 this();
106 if (x.nextClean() != '[') {
107 throw x.syntaxError("A JSONArray text must start with '['");
108 }
109 if (x.nextClean() != ']') {
110 x.back();
111 for (;;) {
112 if (x.nextClean() == ',') {
113 x.back();
114 this.myArrayList.add(JSONObject.NULL);
115 } else {
116 x.back();
117 this.myArrayList.add(x.nextValue());
118 }
119 switch (x.nextClean()) {
120 case ';':
121 case ',':
122 if (x.nextClean() == ']') {
123 return;
124 }
125 x.back();
126 break;
127 case ']':
128 return;
129 default:
130 throw x.syntaxError("Expected a ',' or ']'");
131 }
132 }
133 }
134 }
135
136
137 /**
138 * Construct a JSONArray from a source JSON text.
139 * @param source A string that begins with
140 * <code>[</code>&nbsp;<small>(left bracket)</small>
141 * and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
142 * @throws JSONException If there is a syntax error.
143 */
144 public JSONArray(String source) throws JSONException {
145 this(new JSONTokener(source));
146 }
147
148
149 /**
150 * Construct a JSONArray from a Collection.
151 * @param collection A Collection.
152 */
153 public JSONArray(Collection collection) {
154 this.myArrayList = new ArrayList();
155 if (collection != null) {
156 Iterator iter = collection.iterator();
157 while (iter.hasNext()) {
158 this.myArrayList.add(JSONObject.wrap(iter.next()));
159 }
160 }
161 }
162
163
164 /**
165 * Construct a JSONArray from an array
166 * @throws JSONException If not an array.
167 */
168 public JSONArray(Object array) throws JSONException {
169 this();
170 if (array.getClass().isArray()) {
171 int length = Array.getLength(array);
172 for (int i = 0; i < length; i += 1) {
173 this.put(JSONObject.wrap(Array.get(array, i)));
174 }
175 } else {
176 throw new JSONException(
177"JSONArray initial value should be a string or collection or array.");
178 }
179 }
180
181
182 /**
183 * Get the object value associated with an index.
184 * @param index
185 * The index must be between 0 and length() - 1.
186 * @return An object value.
187 * @throws JSONException If there is no value for the index.
188 */
189 public Object get(int index) throws JSONException {
190 Object object = opt(index);
191 if (object == null) {
192 throw new JSONException("JSONArray[" + index + "] not found.");
193 }
194 return object;
195 }
196
197
198 /**
199 * Get the boolean value associated with an index.
200 * The string values "true" and "false" are converted to boolean.
201 *
202 * @param index The index must be between 0 and length() - 1.
203 * @return The truth.
204 * @throws JSONException If there is no value for the index or if the
205 * value is not convertible to boolean.
206 */
207 public boolean getBoolean(int index) throws JSONException {
208 Object object = get(index);
209 if (object.equals(Boolean.FALSE) ||
210 (object instanceof String &&
211 ((String)object).equalsIgnoreCase("false"))) {
212 return false;
213 } else if (object.equals(Boolean.TRUE) ||
214 (object instanceof String &&
215 ((String)object).equalsIgnoreCase("true"))) {
216 return true;
217 }
218 throw new JSONException("JSONArray[" + index + "] is not a boolean.");
219 }
220
221
222 /**
223 * Get the double value associated with an index.
224 *
225 * @param index The index must be between 0 and length() - 1.
226 * @return The value.
227 * @throws JSONException If the key is not found or if the value cannot
228 * be converted to a number.
229 */
230 public double getDouble(int index) throws JSONException {
231 Object object = get(index);
232 try {
233 return object instanceof Number ?
234 ((Number)object).doubleValue() :
235 Double.parseDouble((String)object);
236 } catch (Exception e) {
237 throw new JSONException("JSONArray[" + index +
238 "] is not a number.");
239 }
240 }
241
242
243 /**
244 * Get the int value associated with an index.
245 *
246 * @param index The index must be between 0 and length() - 1.
247 * @return The value.
248 * @throws JSONException If the key is not found or if the value is not a number.
249 */
250 public int getInt(int index) throws JSONException {
251 Object object = get(index);
252 try {
253 return object instanceof Number ?
254 ((Number)object).intValue() :
255 Integer.parseInt((String)object);
256 } catch (Exception e) {
257 throw new JSONException("JSONArray[" + index +
258 "] is not a number.");
259 }
260 }
261
262
263 /**
264 * Get the JSONArray associated with an index.
265 * @param index The index must be between 0 and length() - 1.
266 * @return A JSONArray value.
267 * @throws JSONException If there is no value for the index. or if the
268 * value is not a JSONArray
269 */
270 public JSONArray getJSONArray(int index) throws JSONException {
271 Object object = get(index);
272 if (object instanceof JSONArray) {
273 return (JSONArray)object;
274 }
275 throw new JSONException("JSONArray[" + index +
276 "] is not a JSONArray.");
277 }
278
279
280 /**
281 * Get the JSONObject associated with an index.
282 * @param index subscript
283 * @return A JSONObject value.
284 * @throws JSONException If there is no value for the index or if the
285 * value is not a JSONObject
286 */
287 public JSONObject getJSONObject(int index) throws JSONException {
288 Object object = get(index);
289 if (object instanceof JSONObject) {
290 return (JSONObject)object;
291 }
292 throw new JSONException("JSONArray[" + index +
293 "] is not a JSONObject.");
294 }
295
296
297 /**
298 * Get the long value associated with an index.
299 *
300 * @param index The index must be between 0 and length() - 1.
301 * @return The value.
302 * @throws JSONException If the key is not found or if the value cannot
303 * be converted to a number.
304 */
305 public long getLong(int index) throws JSONException {
306 Object object = get(index);
307 try {
308 return object instanceof Number ?
309 ((Number)object).longValue() :
310 Long.parseLong((String)object);
311 } catch (Exception e) {
312 throw new JSONException("JSONArray[" + index +
313 "] is not a number.");
314 }
315 }
316
317
318 /**
319 * Get the string associated with an index.
320 * @param index The index must be between 0 and length() - 1.
321 * @return A string value.
322 * @throws JSONException If there is no value for the index.
323 */
324 public String getString(int index) throws JSONException {
325 Object object = get(index);
326 return object == JSONObject.NULL ? null : object.toString();
327 }
328
329
330 /**
331 * Determine if the value is null.
332 * @param index The index must be between 0 and length() - 1.
333 * @return true if the value at the index is null, or if there is no value.
334 */
335 public boolean isNull(int index) {
336 return JSONObject.NULL.equals(opt(index));
337 }
338
339
340 /**
341 * Make a string from the contents of this JSONArray. The
342 * <code>separator</code> string is inserted between each element.
343 * Warning: This method assumes that the data structure is acyclical.
344 * @param separator A string that will be inserted between the elements.
345 * @return a string.
346 * @throws JSONException If the array contains an invalid number.
347 */
348 public String join(String separator) throws JSONException {
349 int len = length();
350 StringBuffer sb = new StringBuffer();
351
352 for (int i = 0; i < len; i += 1) {
353 if (i > 0) {
354 sb.append(separator);
355 }
356 sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
357 }
358 return sb.toString();
359 }
360
361
362 /**
363 * Get the number of elements in the JSONArray, included nulls.
364 *
365 * @return The length (or size).
366 */
367 public int length() {
368 return this.myArrayList.size();
369 }
370
371
372 /**
373 * Get the optional object value associated with an index.
374 * @param index The index must be between 0 and length() - 1.
375 * @return An object value, or null if there is no
376 * object at that index.
377 */
378 public Object opt(int index) {
379 return (index < 0 || index >= length()) ?
380 null : this.myArrayList.get(index);
381 }
382
383
384 /**
385 * Get the optional boolean value associated with an index.
386 * It returns false if there is no value at that index,
387 * or if the value is not Boolean.TRUE or the String "true".
388 *
389 * @param index The index must be between 0 and length() - 1.
390 * @return The truth.
391 */
392 public boolean optBoolean(int index) {
393 return optBoolean(index, false);
394 }
395
396
397 /**
398 * Get the optional boolean value associated with an index.
399 * It returns the defaultValue if there is no value at that index or if
400 * it is not a Boolean or the String "true" or "false" (case insensitive).
401 *
402 * @param index The index must be between 0 and length() - 1.
403 * @param defaultValue A boolean default.
404 * @return The truth.
405 */
406 public boolean optBoolean(int index, boolean defaultValue) {
407 try {
408 return getBoolean(index);
409 } catch (Exception e) {
410 return defaultValue;
411 }
412 }
413
414
415 /**
416 * Get the optional double value associated with an index.
417 * NaN is returned if there is no value for the index,
418 * or if the value is not a number and cannot be converted to a number.
419 *
420 * @param index The index must be between 0 and length() - 1.
421 * @return The value.
422 */
423 public double optDouble(int index) {
424 return optDouble(index, Double.NaN);
425 }
426
427
428 /**
429 * Get the optional double value associated with an index.
430 * The defaultValue is returned if there is no value for the index,
431 * or if the value is not a number and cannot be converted to a number.
432 *
433 * @param index subscript
434 * @param defaultValue The default value.
435 * @return The value.
436 */
437 public double optDouble(int index, double defaultValue) {
438 try {
439 return getDouble(index);
440 } catch (Exception e) {
441 return defaultValue;
442 }
443 }
444
445
446 /**
447 * Get the optional int value associated with an index.
448 * Zero is returned if there is no value for the index,
449 * or if the value is not a number and cannot be converted to a number.
450 *
451 * @param index The index must be between 0 and length() - 1.
452 * @return The value.
453 */
454 public int optInt(int index) {
455 return optInt(index, 0);
456 }
457
458
459 /**
460 * Get the optional int value associated with an index.
461 * The defaultValue is returned if there is no value for the index,
462 * or if the value is not a number and cannot be converted to a number.
463 * @param index The index must be between 0 and length() - 1.
464 * @param defaultValue The default value.
465 * @return The value.
466 */
467 public int optInt(int index, int defaultValue) {
468 try {
469 return getInt(index);
470 } catch (Exception e) {
471 return defaultValue;
472 }
473 }
474
475
476 /**
477 * Get the optional JSONArray associated with an index.
478 * @param index subscript
479 * @return A JSONArray value, or null if the index has no value,
480 * or if the value is not a JSONArray.
481 */
482 public JSONArray optJSONArray(int index) {
483 Object o = opt(index);
484 return o instanceof JSONArray ? (JSONArray)o : null;
485 }
486
487
488 /**
489 * Get the optional JSONObject associated with an index.
490 * Null is returned if the key is not found, or null if the index has
491 * no value, or if the value is not a JSONObject.
492 *
493 * @param index The index must be between 0 and length() - 1.
494 * @return A JSONObject value.
495 */
496 public JSONObject optJSONObject(int index) {
497 Object o = opt(index);
498 return o instanceof JSONObject ? (JSONObject)o : null;
499 }
500
501
502 /**
503 * Get the optional long value associated with an index.
504 * Zero is returned if there is no value for the index,
505 * or if the value is not a number and cannot be converted to a number.
506 *
507 * @param index The index must be between 0 and length() - 1.
508 * @return The value.
509 */
510 public long optLong(int index) {
511 return optLong(index, 0);
512 }
513
514
515 /**
516 * Get the optional long value associated with an index.
517 * The defaultValue is returned if there is no value for the index,
518 * or if the value is not a number and cannot be converted to a number.
519 * @param index The index must be between 0 and length() - 1.
520 * @param defaultValue The default value.
521 * @return The value.
522 */
523 public long optLong(int index, long defaultValue) {
524 try {
525 return getLong(index);
526 } catch (Exception e) {
527 return defaultValue;
528 }
529 }
530
531
532 /**
533 * Get the optional string value associated with an index. It returns an
534 * empty string if there is no value at that index. If the value
535 * is not a string and is not null, then it is coverted to a string.
536 *
537 * @param index The index must be between 0 and length() - 1.
538 * @return A String value.
539 */
540 public String optString(int index) {
541 return optString(index, "");
542 }
543
544
545 /**
546 * Get the optional string associated with an index.
547 * The defaultValue is returned if the key is not found.
548 *
549 * @param index The index must be between 0 and length() - 1.
550 * @param defaultValue The default value.
551 * @return A String value.
552 */
553 public String optString(int index, String defaultValue) {
554 Object object = opt(index);
555 return object != null ? object.toString() : defaultValue;
556 }
557
558
559 /**
560 * Append a boolean value. This increases the array's length by one.
561 *
562 * @param value A boolean value.
563 * @return this.
564 */
565 public JSONArray put(boolean value) {
566 put(value ? Boolean.TRUE : Boolean.FALSE);
567 return this;
568 }
569
570
571 /**
572 * Put a value in the JSONArray, where the value will be a
573 * JSONArray which is produced from a Collection.
574 * @param value A Collection value.
575 * @return this.
576 */
577 public JSONArray put(Collection value) {
578 put(new JSONArray(value));
579 return this;
580 }
581
582
583 /**
584 * Append a double value. This increases the array's length by one.
585 *
586 * @param value A double value.
587 * @throws JSONException if the value is not finite.
588 * @return this.
589 */
590 public JSONArray put(double value) throws JSONException {
591 Double d = new Double(value);
592 JSONObject.testValidity(d);
593 put(d);
594 return this;
595 }
596
597
598 /**
599 * Append an int value. This increases the array's length by one.
600 *
601 * @param value An int value.
602 * @return this.
603 */
604 public JSONArray put(int value) {
605 put(new Integer(value));
606 return this;
607 }
608
609
610 /**
611 * Append an long value. This increases the array's length by one.
612 *
613 * @param value A long value.
614 * @return this.
615 */
616 public JSONArray put(long value) {
617 put(new Long(value));
618 return this;
619 }
620
621
622 /**
623 * Put a value in the JSONArray, where the value will be a
624 * JSONObject which is produced from a Map.
625 * @param value A Map value.
626 * @return this.
627 */
628 public JSONArray put(Map value) {
629 put(new JSONObject(value));
630 return this;
631 }
632
633
634 /**
635 * Append an object value. This increases the array's length by one.
636 * @param value An object value. The value should be a
637 * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
638 * JSONObject.NULL object.
639 * @return this.
640 */
641 public JSONArray put(Object value) {
642 this.myArrayList.add(value);
643 return this;
644 }
645
646
647 /**
648 * Put or replace a boolean value in the JSONArray. If the index is greater
649 * than the length of the JSONArray, then null elements will be added as
650 * necessary to pad it out.
651 * @param index The subscript.
652 * @param value A boolean value.
653 * @return this.
654 * @throws JSONException If the index is negative.
655 */
656 public JSONArray put(int index, boolean value) throws JSONException {
657 put(index, value ? Boolean.TRUE : Boolean.FALSE);
658 return this;
659 }
660
661
662 /**
663 * Put a value in the JSONArray, where the value will be a
664 * JSONArray which is produced from a Collection.
665 * @param index The subscript.
666 * @param value A Collection value.
667 * @return this.
668 * @throws JSONException If the index is negative or if the value is
669 * not finite.
670 */
671 public JSONArray put(int index, Collection value) throws JSONException {
672 put(index, new JSONArray(value));
673 return this;
674 }
675
676
677 /**
678 * Put or replace a double value. If the index is greater than the length of
679 * the JSONArray, then null elements will be added as necessary to pad
680 * it out.
681 * @param index The subscript.
682 * @param value A double value.
683 * @return this.
684 * @throws JSONException If the index is negative or if the value is
685 * not finite.
686 */
687 public JSONArray put(int index, double value) throws JSONException {
688 put(index, new Double(value));
689 return this;
690 }
691
692
693 /**
694 * Put or replace an int value. If the index is greater than the length of
695 * the JSONArray, then null elements will be added as necessary to pad
696 * it out.
697 * @param index The subscript.
698 * @param value An int value.
699 * @return this.
700 * @throws JSONException If the index is negative.
701 */
702 public JSONArray put(int index, int value) throws JSONException {
703 put(index, new Integer(value));
704 return this;
705 }
706
707
708 /**
709 * Put or replace a long value. If the index is greater than the length of
710 * the JSONArray, then null elements will be added as necessary to pad
711 * it out.
712 * @param index The subscript.
713 * @param value A long value.
714 * @return this.
715 * @throws JSONException If the index is negative.
716 */
717 public JSONArray put(int index, long value) throws JSONException {
718 put(index, new Long(value));
719 return this;
720 }
721
722
723 /**
724 * Put a value in the JSONArray, where the value will be a
725 * JSONObject which is produced from a Map.
726 * @param index The subscript.
727 * @param value The Map value.
728 * @return this.
729 * @throws JSONException If the index is negative or if the the value is
730 * an invalid number.
731 */
732 public JSONArray put(int index, Map value) throws JSONException {
733 put(index, new JSONObject(value));
734 return this;
735 }
736
737
738 /**
739 * Put or replace an object value in the JSONArray. If the index is greater
740 * than the length of the JSONArray, then null elements will be added as
741 * necessary to pad it out.
742 * @param index The subscript.
743 * @param value The value to put into the array. The value should be a
744 * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
745 * JSONObject.NULL object.
746 * @return this.
747 * @throws JSONException If the index is negative or if the the value is
748 * an invalid number.
749 */
750 public JSONArray put(int index, Object value) throws JSONException {
751 JSONObject.testValidity(value);
752 if (index < 0) {
753 throw new JSONException("JSONArray[" + index + "] not found.");
754 }
755 if (index < length()) {
756 this.myArrayList.set(index, value);
757 } else {
758 while (index != length()) {
759 put(JSONObject.NULL);
760 }
761 put(value);
762 }
763 return this;
764 }
765
766
767 /**
768 * Remove an index and close the hole.
769 * @param index The index of the element to be removed.
770 * @return The value that was associated with the index,
771 * or null if there was no value.
772 */
773 public Object remove(int index) {
774 Object o = opt(index);
775 this.myArrayList.remove(index);
776 return o;
777 }
778
779
780 /**
781 * Produce a JSONObject by combining a JSONArray of names with the values
782 * of this JSONArray.
783 * @param names A JSONArray containing a list of key strings. These will be
784 * paired with the values.
785 * @return A JSONObject, or null if there are no names or if this JSONArray
786 * has no values.
787 * @throws JSONException If any of the names are null.
788 */
789 public JSONObject toJSONObject(JSONArray names) throws JSONException {
790 if (names == null || names.length() == 0 || length() == 0) {
791 return null;
792 }
793 JSONObject jo = new JSONObject();
794 for (int i = 0; i < names.length(); i += 1) {
795 jo.put(names.getString(i), this.opt(i));
796 }
797 return jo;
798 }
799
800
801 /**
802 * Make a JSON text of this JSONArray. For compactness, no
803 * unnecessary whitespace is added. If it is not possible to produce a
804 * syntactically correct JSON text then null will be returned instead. This
805 * could occur if the array contains an invalid number.
806 * <p>
807 * Warning: This method assumes that the data structure is acyclical.
808 *
809 * @return a printable, displayable, transmittable
810 * representation of the array.
811 */
812 public String toString() {
813 try {
814 return '[' + join(",") + ']';
815 } catch (Exception e) {
816 return null;
817 }
818 }
819
820
821 /**
822 * Make a prettyprinted JSON text of this JSONArray.
823 * Warning: This method assumes that the data structure is acyclical.
824 * @param indentFactor The number of spaces to add to each level of
825 * indentation.
826 * @return a printable, displayable, transmittable
827 * representation of the object, beginning
828 * with <code>[</code>&nbsp;<small>(left bracket)</small> and ending
829 * with <code>]</code>&nbsp;<small>(right bracket)</small>.
830 * @throws JSONException
831 */
832 public String toString(int indentFactor) throws JSONException {
833 return toString(indentFactor, 0);
834 }
835
836
837 /**
838 * Make a prettyprinted JSON text of this JSONArray.
839 * Warning: This method assumes that the data structure is acyclical.
840 * @param indentFactor The number of spaces to add to each level of
841 * indentation.
842 * @param indent The indention of the top level.
843 * @return a printable, displayable, transmittable
844 * representation of the array.
845 * @throws JSONException
846 */
847 String toString(int indentFactor, int indent) throws JSONException {
848 int len = length();
849 if (len == 0) {
850 return "[]";
851 }
852 int i;
853 StringBuffer sb = new StringBuffer("[");
854 if (len == 1) {
855 sb.append(JSONObject.valueToString(this.myArrayList.get(0),
856 indentFactor, indent));
857 } else {
858 int newindent = indent + indentFactor;
859 sb.append('\n');
860 for (i = 0; i < len; i += 1) {
861 if (i > 0) {
862 sb.append(",\n");
863 }
864 for (int j = 0; j < newindent; j += 1) {
865 sb.append(' ');
866 }
867 sb.append(JSONObject.valueToString(this.myArrayList.get(i),
868 indentFactor, newindent));
869 }
870 sb.append('\n');
871 for (i = 0; i < indent; i += 1) {
872 sb.append(' ');
873 }
874 }
875 sb.append(']');
876 return sb.toString();
877 }
878
879
880 /**
881 * Write the contents of the JSONArray as JSON text to a writer.
882 * For compactness, no whitespace is added.
883 * <p>
884 * Warning: This method assumes that the data structure is acyclical.
885 *
886 * @return The writer.
887 * @throws JSONException
888 */
889 public Writer write(Writer writer) throws JSONException {
890 try {
891 boolean b = false;
892 int len = length();
893
894 writer.write('[');
895
896 for (int i = 0; i < len; i += 1) {
897 if (b) {
898 writer.write(',');
899 }
900 Object v = this.myArrayList.get(i);
901 if (v instanceof JSONObject) {
902 ((JSONObject)v).write(writer);
903 } else if (v instanceof JSONArray) {
904 ((JSONArray)v).write(writer);
905 } else {
906 writer.write(JSONObject.valueToString(v));
907 }
908 b = true;
909 }
910 writer.write(']');
911 return writer;
912 } catch (IOException e) {
913 throw new JSONException(e);
914 }
915 }
916}
Note: See TracBrowser for help on using the repository browser.