4 Copyright (c) 2002 JSON.org
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 import java.io.IOException;
28 import java.io.StringWriter;
29 import java.io.Writer;
30 import java.lang.reflect.Array;
31 import java.math.BigDecimal;
32 import java.math.BigInteger;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Iterator;
36 import java.util.List;
40 * A JSONArray is an ordered sequence of values. Its external text form is a
41 * string wrapped in square brackets with commas separating the values. The
42 * internal form is an object having <code>get</code> and <code>opt</code>
43 * methods for accessing the values by index, and <code>put</code> methods for
44 * adding or replacing values. The values can be any of these types:
45 * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
46 * <code>Number</code>, <code>String</code>, or the
47 * <code>JSONObject.NULL object</code>.
49 * The constructor can convert a JSON text into a Java object. The
50 * <code>toString</code> method converts to JSON text.
52 * A <code>get</code> method returns a value if one can be found, and throws an
53 * exception if one cannot be found. An <code>opt</code> method returns a
54 * default value instead of throwing an exception, and so is useful for
55 * obtaining optional values.
57 * The generic <code>get()</code> and <code>opt()</code> methods return an
58 * object which you can cast or query for type. There are also typed
59 * <code>get</code> and <code>opt</code> methods that do type checking and type
62 * The texts produced by the <code>toString</code> methods strictly conform to
63 * JSON syntax rules. The constructors are more forgiving in the texts they will
66 * <li>An extra <code>,</code> <small>(comma)</small> may appear just
67 * before the closing bracket.</li>
68 * <li>The <code>null</code> value will be inserted when there is <code>,</code>
69 * <small>(comma)</small> elision.</li>
70 * <li>Strings may be quoted with <code>'</code> <small>(single
71 * quote)</small>.</li>
72 * <li>Strings do not need to be quoted at all if they do not begin with a quote
73 * or single quote, and if they do not contain leading or trailing spaces, and
74 * if they do not contain any of these characters:
75 * <code>{ } [ ] / \ : , #</code> and if they do not look like numbers and
76 * if they are not the reserved words <code>true</code>, <code>false</code>, or
77 * <code>null</code>.</li>
83 public class JSONArray implements Iterable<Object> {
86 * The arrayList where the JSONArray's properties are kept.
88 private final ArrayList<Object> myArrayList;
91 * Construct an empty JSONArray.
94 this.myArrayList = new ArrayList<Object>();
98 * Construct a JSONArray from a JSONTokener.
102 * @throws JSONException
103 * If there is a syntax error.
105 public JSONArray(JSONTokener x) throws JSONException {
107 if (x.nextClean() != '[') {
108 throw x.syntaxError("A JSONArray text must start with '['");
110 if (x.nextClean() != ']') {
113 if (x.nextClean() == ',') {
115 this.myArrayList.add(JSONObject.NULL);
118 this.myArrayList.add(x.nextValue());
120 switch (x.nextClean()) {
122 if (x.nextClean() == ']') {
130 throw x.syntaxError("Expected a ',' or ']'");
137 * Construct a JSONArray from a source JSON text.
140 * A string that begins with <code>[</code> <small>(left
141 * bracket)</small> and ends with <code>]</code>
142 * <small>(right bracket)</small>.
143 * @throws JSONException
144 * If there is a syntax error.
146 public JSONArray(String source) throws JSONException {
147 this(new JSONTokener(source));
151 * Construct a JSONArray from a Collection.
156 public JSONArray(Collection<?> collection) {
157 this.myArrayList = new ArrayList<Object>();
158 if (collection != null) {
159 for (Object o: collection){
160 this.myArrayList.add(JSONObject.wrap(o));
166 * Construct a JSONArray from an array
168 * @throws JSONException
171 public JSONArray(Object array) throws JSONException {
173 if (array.getClass().isArray()) {
174 int length = Array.getLength(array);
175 for (int i = 0; i < length; i += 1) {
176 this.put(JSONObject.wrap(Array.get(array, i)));
179 throw new JSONException(
180 "JSONArray initial value should be a string or collection or array.");
185 public Iterator<Object> iterator() {
186 return myArrayList.iterator();
190 * Get the object value associated with an index.
193 * The index must be between 0 and length() - 1.
194 * @return An object value.
195 * @throws JSONException
196 * If there is no value for the index.
198 public Object get(int index) throws JSONException {
199 Object object = this.opt(index);
200 if (object == null) {
201 throw new JSONException("JSONArray[" + index + "] not found.");
207 * Get the boolean value associated with an index. The string values "true"
208 * and "false" are converted to boolean.
211 * The index must be between 0 and length() - 1.
213 * @throws JSONException
214 * If there is no value for the index or if the value is not
215 * convertible to boolean.
217 public boolean getBoolean(int index) throws JSONException {
218 Object object = this.get(index);
219 if (object.equals(Boolean.FALSE)
220 || (object instanceof String && ((String) object)
221 .equalsIgnoreCase("false"))) {
223 } else if (object.equals(Boolean.TRUE)
224 || (object instanceof String && ((String) object)
225 .equalsIgnoreCase("true"))) {
228 throw new JSONException("JSONArray[" + index + "] is not a boolean.");
232 * Get the double value associated with an index.
235 * The index must be between 0 and length() - 1.
237 * @throws JSONException
238 * If the key is not found or if the value cannot be converted
241 public double getDouble(int index) throws JSONException {
242 Object object = this.get(index);
244 return object instanceof Number ? ((Number) object).doubleValue()
245 : Double.parseDouble((String) object);
246 } catch (Exception e) {
247 throw new JSONException("JSONArray[" + index + "] is not a number.");
252 * Get the enum value associated with an index.
255 * The type of enum to retrieve.
257 * The index must be between 0 and length() - 1.
258 * @return The enum value at the index location
259 * @throws JSONException
260 * if the key is not found or if the value cannot be converted
263 public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
264 E val = optEnum(clazz, index);
266 // JSONException should really take a throwable argument.
267 // If it did, I would re-implement this with the Enum.valueOf
268 // method and place any thrown exception in the JSONException
269 throw new JSONException("JSONObject[" + JSONObject.quote(Integer.toString(index))
270 + "] is not an enum of type " + JSONObject.quote(clazz.getSimpleName())
277 * Get the BigDecimal value associated with an index.
280 * The index must be between 0 and length() - 1.
282 * @throws JSONException
283 * If the key is not found or if the value cannot be converted
286 public BigDecimal getBigDecimal (int index) throws JSONException {
287 Object object = this.get(index);
289 return new BigDecimal(object.toString());
290 } catch (Exception e) {
291 throw new JSONException("JSONArray[" + index +
292 "] could not convert to BigDecimal.");
297 * Get the BigInteger value associated with an index.
300 * The index must be between 0 and length() - 1.
302 * @throws JSONException
303 * If the key is not found or if the value cannot be converted
306 public BigInteger getBigInteger (int index) throws JSONException {
307 Object object = this.get(index);
309 return new BigInteger(object.toString());
310 } catch (Exception e) {
311 throw new JSONException("JSONArray[" + index +
312 "] could not convert to BigInteger.");
317 * Get the int value associated with an index.
320 * The index must be between 0 and length() - 1.
322 * @throws JSONException
323 * If the key is not found or if the value is not a number.
325 public int getInt(int index) throws JSONException {
326 Object object = this.get(index);
328 return object instanceof Number ? ((Number) object).intValue()
329 : Integer.parseInt((String) object);
330 } catch (Exception e) {
331 throw new JSONException("JSONArray[" + index + "] is not a number.");
336 * Get the JSONArray associated with an index.
339 * The index must be between 0 and length() - 1.
340 * @return A JSONArray value.
341 * @throws JSONException
342 * If there is no value for the index. or if the value is not a
345 public JSONArray getJSONArray(int index) throws JSONException {
346 Object object = this.get(index);
347 if (object instanceof JSONArray) {
348 return (JSONArray) object;
350 throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
354 * Get the JSONObject associated with an index.
358 * @return A JSONObject value.
359 * @throws JSONException
360 * If there is no value for the index or if the value is not a
363 public JSONObject getJSONObject(int index) throws JSONException {
364 Object object = this.get(index);
365 if (object instanceof JSONObject) {
366 return (JSONObject) object;
368 throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
372 * Get the long value associated with an index.
375 * The index must be between 0 and length() - 1.
377 * @throws JSONException
378 * If the key is not found or if the value cannot be converted
381 public long getLong(int index) throws JSONException {
382 Object object = this.get(index);
384 return object instanceof Number ? ((Number) object).longValue()
385 : Long.parseLong((String) object);
386 } catch (Exception e) {
387 throw new JSONException("JSONArray[" + index + "] is not a number.");
392 * Get the string associated with an index.
395 * The index must be between 0 and length() - 1.
396 * @return A string value.
397 * @throws JSONException
398 * If there is no string value for the index.
400 public String getString(int index) throws JSONException {
401 Object object = this.get(index);
402 if (object instanceof String) {
403 return (String) object;
405 throw new JSONException("JSONArray[" + index + "] not a string.");
409 * Determine if the value is null.
412 * The index must be between 0 and length() - 1.
413 * @return true if the value at the index is null, or if there is no value.
415 public boolean isNull(int index) {
416 return JSONObject.NULL.equals(this.opt(index));
420 * Make a string from the contents of this JSONArray. The
421 * <code>separator</code> string is inserted between each element. Warning:
422 * This method assumes that the data structure is acyclical.
425 * A string that will be inserted between the elements.
427 * @throws JSONException
428 * If the array contains an invalid number.
430 public String join(String separator) throws JSONException {
431 int len = this.length();
432 StringBuilder sb = new StringBuilder();
434 for (int i = 0; i < len; i += 1) {
436 sb.append(separator);
438 sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
440 return sb.toString();
444 * Get the number of elements in the JSONArray, included nulls.
446 * @return The length (or size).
448 public int length() {
449 return this.myArrayList.size();
453 * Get the optional object value associated with an index.
456 * The index must be between 0 and length() - 1.
457 * @return An object value, or null if there is no object at that index.
459 public Object opt(int index) {
460 return (index < 0 || index >= this.length()) ? null : this.myArrayList
465 * Get the optional boolean value associated with an index. It returns false
466 * if there is no value at that index, or if the value is not Boolean.TRUE
467 * or the String "true".
470 * The index must be between 0 and length() - 1.
473 public boolean optBoolean(int index) {
474 return this.optBoolean(index, false);
478 * Get the optional boolean value associated with an index. It returns the
479 * defaultValue if there is no value at that index or if it is not a Boolean
480 * or the String "true" or "false" (case insensitive).
483 * The index must be between 0 and length() - 1.
484 * @param defaultValue
488 public boolean optBoolean(int index, boolean defaultValue) {
490 return this.getBoolean(index);
491 } catch (Exception e) {
497 * Get the optional double value associated with an index. NaN is returned
498 * if there is no value for the index, or if the value is not a number and
499 * cannot be converted to a number.
502 * The index must be between 0 and length() - 1.
505 public double optDouble(int index) {
506 return this.optDouble(index, Double.NaN);
510 * Get the optional double value associated with an index. The defaultValue
511 * is returned if there is no value for the index, or if the value is not a
512 * number and cannot be converted to a number.
516 * @param defaultValue
520 public double optDouble(int index, double defaultValue) {
522 return this.getDouble(index);
523 } catch (Exception e) {
529 * Get the optional int value associated with an index. Zero is returned if
530 * there is no value for the index, or if the value is not a number and
531 * cannot be converted to a number.
534 * The index must be between 0 and length() - 1.
537 public int optInt(int index) {
538 return this.optInt(index, 0);
542 * Get the optional int value associated with an index. The defaultValue is
543 * returned if there is no value for the index, or if the value is not a
544 * number and cannot be converted to a number.
547 * The index must be between 0 and length() - 1.
548 * @param defaultValue
552 public int optInt(int index, int defaultValue) {
554 return this.getInt(index);
555 } catch (Exception e) {
561 * Get the enum value associated with a key.
564 * The type of enum to retrieve.
566 * The index must be between 0 and length() - 1.
567 * @return The enum value at the index location or null if not found
569 public <E extends Enum<E>> E optEnum(Class<E> clazz, int index) {
570 return this.optEnum(clazz, index, null);
574 * Get the enum value associated with a key.
577 * The type of enum to retrieve.
579 * The index must be between 0 and length() - 1.
580 * @param defaultValue
581 * The default in case the value is not found
582 * @return The enum value at the index location or defaultValue if
583 * the value is not found or cannot be assigned to clazz
585 public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue) {
587 Object val = this.opt(index);
588 if (JSONObject.NULL.equals(val)) {
591 if (clazz.isAssignableFrom(val.getClass())) {
592 // we just checked it!
593 @SuppressWarnings("unchecked")
597 return Enum.valueOf(clazz, val.toString());
598 } catch (IllegalArgumentException e) {
600 } catch (NullPointerException e) {
607 * Get the optional BigInteger value associated with an index. The
608 * defaultValue is returned if there is no value for the index, or if the
609 * value is not a number and cannot be converted to a number.
612 * The index must be between 0 and length() - 1.
613 * @param defaultValue
617 public BigInteger optBigInteger(int index, BigInteger defaultValue) {
619 return this.getBigInteger(index);
620 } catch (Exception e) {
626 * Get the optional BigDecimal value associated with an index. The
627 * defaultValue is returned if there is no value for the index, or if the
628 * value is not a number and cannot be converted to a number.
631 * The index must be between 0 and length() - 1.
632 * @param defaultValue
636 public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
638 return this.getBigDecimal(index);
639 } catch (Exception e) {
645 * Get the optional JSONArray associated with an index.
649 * @return A JSONArray value, or null if the index has no value, or if the
650 * value is not a JSONArray.
652 public JSONArray optJSONArray(int index) {
653 Object o = this.opt(index);
654 return o instanceof JSONArray ? (JSONArray) o : null;
658 * Get the optional JSONObject associated with an index. Null is returned if
659 * the key is not found, or null if the index has no value, or if the value
660 * is not a JSONObject.
663 * The index must be between 0 and length() - 1.
664 * @return A JSONObject value.
666 public JSONObject optJSONObject(int index) {
667 Object o = this.opt(index);
668 return o instanceof JSONObject ? (JSONObject) o : null;
672 * Get the optional long value associated with an index. Zero is returned if
673 * there is no value for the index, or if the value is not a number and
674 * cannot be converted to a number.
677 * The index must be between 0 and length() - 1.
680 public long optLong(int index) {
681 return this.optLong(index, 0);
685 * Get the optional long value associated with an index. The defaultValue is
686 * returned if there is no value for the index, or if the value is not a
687 * number and cannot be converted to a number.
690 * The index must be between 0 and length() - 1.
691 * @param defaultValue
695 public long optLong(int index, long defaultValue) {
697 return this.getLong(index);
698 } catch (Exception e) {
704 * Get the optional string value associated with an index. It returns an
705 * empty string if there is no value at that index. If the value is not a
706 * string and is not null, then it is coverted to a string.
709 * The index must be between 0 and length() - 1.
710 * @return A String value.
712 public String optString(int index) {
713 return this.optString(index, "");
717 * Get the optional string associated with an index. The defaultValue is
718 * returned if the key is not found.
721 * The index must be between 0 and length() - 1.
722 * @param defaultValue
724 * @return A String value.
726 public String optString(int index, String defaultValue) {
727 Object object = this.opt(index);
728 return JSONObject.NULL.equals(object) ? defaultValue : object
733 * Append a boolean value. This increases the array's length by one.
739 public JSONArray put(boolean value) {
740 this.put(value ? Boolean.TRUE : Boolean.FALSE);
745 * Put a value in the JSONArray, where the value will be a JSONArray which
746 * is produced from a Collection.
749 * A Collection value.
752 public JSONArray put(Collection<?> value) {
753 this.put(new JSONArray(value));
758 * Append a double value. This increases the array's length by one.
762 * @throws JSONException
763 * if the value is not finite.
766 public JSONArray put(double value) throws JSONException {
767 Double d = new Double(value);
768 JSONObject.testValidity(d);
774 * Append an int value. This increases the array's length by one.
780 public JSONArray put(int value) {
781 this.put(new Integer(value));
786 * Append an long value. This increases the array's length by one.
792 public JSONArray put(long value) {
793 this.put(new Long(value));
798 * Put a value in the JSONArray, where the value will be a JSONObject which
799 * is produced from a Map.
805 public JSONArray put(Map<?, ?> value) {
806 this.put(new JSONObject(value));
811 * Append an object value. This increases the array's length by one.
814 * An object value. The value should be a Boolean, Double,
815 * Integer, JSONArray, JSONObject, Long, or String, or the
816 * JSONObject.NULL object.
819 public JSONArray put(Object value) {
820 this.myArrayList.add(value);
825 * Put or replace a boolean value in the JSONArray. If the index is greater
826 * than the length of the JSONArray, then null elements will be added as
827 * necessary to pad it out.
834 * @throws JSONException
835 * If the index is negative.
837 public JSONArray put(int index, boolean value) throws JSONException {
838 this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
843 * Put a value in the JSONArray, where the value will be a JSONArray which
844 * is produced from a Collection.
849 * A Collection value.
851 * @throws JSONException
852 * If the index is negative or if the value is not finite.
854 public JSONArray put(int index, Collection<?> value) throws JSONException {
855 this.put(index, new JSONArray(value));
860 * Put or replace a double value. If the index is greater than the length of
861 * the JSONArray, then null elements will be added as necessary to pad it
869 * @throws JSONException
870 * If the index is negative or if the value is not finite.
872 public JSONArray put(int index, double value) throws JSONException {
873 this.put(index, new Double(value));
878 * Put or replace an int value. If the index is greater than the length of
879 * the JSONArray, then null elements will be added as necessary to pad it
887 * @throws JSONException
888 * If the index is negative.
890 public JSONArray put(int index, int value) throws JSONException {
891 this.put(index, new Integer(value));
896 * Put or replace a long value. If the index is greater than the length of
897 * the JSONArray, then null elements will be added as necessary to pad it
905 * @throws JSONException
906 * If the index is negative.
908 public JSONArray put(int index, long value) throws JSONException {
909 this.put(index, new Long(value));
914 * Put a value in the JSONArray, where the value will be a JSONObject that
915 * is produced from a Map.
922 * @throws JSONException
923 * If the index is negative or if the the value is an invalid
926 public JSONArray put(int index, Map<?, ?> value) throws JSONException {
927 this.put(index, new JSONObject(value));
932 * Put or replace an object value in the JSONArray. If the index is greater
933 * than the length of the JSONArray, then null elements will be added as
934 * necessary to pad it out.
939 * The value to put into the array. The value should be a
940 * Boolean, Double, Integer, JSONArray, JSONObject, Long, or
941 * String, or the JSONObject.NULL object.
943 * @throws JSONException
944 * If the index is negative or if the the value is an invalid
947 public JSONArray put(int index, Object value) throws JSONException {
948 JSONObject.testValidity(value);
950 throw new JSONException("JSONArray[" + index + "] not found.");
952 if (index < this.length()) {
953 this.myArrayList.set(index, value);
955 while (index != this.length()) {
956 this.put(JSONObject.NULL);
964 * Creates a JSONPointer using an intialization string and tries to
965 * match it to an item within this JSONArray. For example, given a
966 * JSONArray initialized with this document:
972 * and this JSONPointer string:
976 * Then this method will return the String "c"
977 * A JSONPointerException may be thrown from code called by this method.
979 * @param jsonPointer string that can be used to create a JSONPointer
980 * @return the item matched by the JSONPointer, otherwise null
982 public Object query(String jsonPointer) {
983 return new JSONPointer(jsonPointer).queryFrom(this);
987 * Queries and returns a value from this object using {@code jsonPointer}, or
988 * returns null if the query fails due to a missing key.
990 * @param jsonPointer the string representation of the JSON pointer
991 * @return the queried value or {@code null}
992 * @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
994 public Object optQuery(String jsonPointer) {
995 JSONPointer pointer = new JSONPointer(jsonPointer);
997 return pointer.queryFrom(this);
998 } catch (JSONPointerException e) {
1004 * Remove an index and close the hole.
1007 * The index of the element to be removed.
1008 * @return The value that was associated with the index, or null if there
1011 public Object remove(int index) {
1012 return index >= 0 && index < this.length()
1013 ? this.myArrayList.remove(index)
1018 * Determine if two JSONArrays are similar.
1019 * They must contain similar sequences.
1021 * @param other The other JSONArray
1022 * @return true if they are equal
1024 public boolean similar(Object other) {
1025 if (!(other instanceof JSONArray)) {
1028 int len = this.length();
1029 if (len != ((JSONArray)other).length()) {
1032 for (int i = 0; i < len; i += 1) {
1033 Object valueThis = this.get(i);
1034 Object valueOther = ((JSONArray)other).get(i);
1035 if (valueThis instanceof JSONObject) {
1036 if (!((JSONObject)valueThis).similar(valueOther)) {
1039 } else if (valueThis instanceof JSONArray) {
1040 if (!((JSONArray)valueThis).similar(valueOther)) {
1043 } else if (!valueThis.equals(valueOther)) {
1051 * Produce a JSONObject by combining a JSONArray of names with the values of
1055 * A JSONArray containing a list of key strings. These will be
1056 * paired with the values.
1057 * @return A JSONObject, or null if there are no names or if this JSONArray
1059 * @throws JSONException
1060 * If any of the names are null.
1062 public JSONObject toJSONObject(JSONArray names) throws JSONException {
1063 if (names == null || names.length() == 0 || this.length() == 0) {
1066 JSONObject jo = new JSONObject();
1067 for (int i = 0; i < names.length(); i += 1) {
1068 jo.put(names.getString(i), this.opt(i));
1074 * Make a JSON text of this JSONArray. For compactness, no unnecessary
1075 * whitespace is added. If it is not possible to produce a syntactically
1076 * correct JSON text then null will be returned instead. This could occur if
1077 * the array contains an invalid number.
1079 * Warning: This method assumes that the data structure is acyclical.
1081 * @return a printable, displayable, transmittable representation of the
1084 public String toString() {
1086 return this.toString(0);
1087 } catch (Exception e) {
1093 * Make a prettyprinted JSON text of this JSONArray. Warning: This method
1094 * assumes that the data structure is acyclical.
1096 * @param indentFactor
1097 * The number of spaces to add to each level of indentation.
1098 * @return a printable, displayable, transmittable representation of the
1099 * object, beginning with <code>[</code> <small>(left
1100 * bracket)</small> and ending with <code>]</code>
1101 * <small>(right bracket)</small>.
1102 * @throws JSONException
1104 public String toString(int indentFactor) throws JSONException {
1105 StringWriter sw = new StringWriter();
1106 synchronized (sw.getBuffer()) {
1107 return this.write(sw, indentFactor, 0).toString();
1112 * Write the contents of the JSONArray as JSON text to a writer. For
1113 * compactness, no whitespace is added.
1115 * Warning: This method assumes that the data structure is acyclical.
1117 * @return The writer.
1118 * @throws JSONException
1120 public Writer write(Writer writer) throws JSONException {
1121 return this.write(writer, 0, 0);
1125 * Write the contents of the JSONArray as JSON text to a writer. For
1126 * compactness, no whitespace is added.
1128 * Warning: This method assumes that the data structure is acyclical.
1131 * Writes the serialized JSON
1132 * @param indentFactor
1133 * The number of spaces to add to each level of indentation.
1135 * The indention of the top level.
1136 * @return The writer.
1137 * @throws JSONException
1139 public Writer write(Writer writer, int indentFactor, int indent)
1140 throws JSONException {
1142 boolean commanate = false;
1143 int length = this.length();
1147 JSONObject.writeValue(writer, this.myArrayList.get(0),
1148 indentFactor, indent);
1149 } else if (length != 0) {
1150 final int newindent = indent + indentFactor;
1152 for (int i = 0; i < length; i += 1) {
1156 if (indentFactor > 0) {
1159 JSONObject.indent(writer, newindent);
1160 JSONObject.writeValue(writer, this.myArrayList.get(i),
1161 indentFactor, newindent);
1164 if (indentFactor > 0) {
1167 JSONObject.indent(writer, indent);
1171 } catch (IOException e) {
1172 throw new JSONException(e);
1177 * Returns a java.util.List containing all of the elements in this array.
1178 * If an element in the array is a JSONArray or JSONObject it will also
1181 * Warning: This method assumes that the data structure is acyclical.
1183 * @return a java.util.List containing the elements of this array
1185 public List<Object> toList() {
1186 List<Object> results = new ArrayList<Object>(this.myArrayList.size());
1187 for (Object element : this.myArrayList) {
1188 if (element == null || JSONObject.NULL.equals(element)) {
1190 } else if (element instanceof JSONArray) {
1191 results.add(((JSONArray) element).toList());
1192 } else if (element instanceof JSONObject) {
1193 results.add(((JSONObject) element).toMap());
1195 results.add(element);