1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
26 Copyright (c) 2002 JSON.org
\r
28 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
29 of this software and associated documentation files (the "Software"), to deal
\r
30 in the Software without restriction, including without limitation the rights
\r
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
32 copies of the Software, and to permit persons to whom the Software is
\r
33 furnished to do so, subject to the following conditions:
\r
35 The above copyright notice and this permission notice shall be included in all
\r
36 copies or substantial portions of the Software.
\r
38 The Software shall be used for Good, not Evil.
\r
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
41 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
42 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
43 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
44 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
45 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
49 import java.io.IOException;
\r
50 import java.io.StringWriter;
\r
51 import java.io.Writer;
\r
52 import java.lang.reflect.Array;
\r
53 import java.util.ArrayList;
\r
54 import java.util.Collection;
\r
55 import java.util.Iterator;
\r
56 import java.util.List;
\r
57 import java.util.Map;
\r
60 * A JSONArray is an ordered sequence of values. Its external text form is a
\r
61 * string wrapped in square brackets with commas separating the values. The
\r
62 * internal form is an object having <code>get</code> and <code>opt</code>
\r
63 * methods for accessing the values by index, and <code>put</code> methods for
\r
64 * adding or replacing values. The values can be any of these types:
\r
65 * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
\r
66 * <code>Number</code>, <code>String</code>, or the
\r
67 * <code>JSONObject.NULL object</code>.
\r
69 * The constructor can convert a JSON text into a Java object. The
\r
70 * <code>toString</code> method converts to JSON text.
\r
72 * A <code>get</code> method returns a value if one can be found, and throws an
\r
73 * exception if one cannot be found. An <code>opt</code> method returns a
\r
74 * default value instead of throwing an exception, and so is useful for
\r
75 * obtaining optional values.
\r
77 * The generic <code>get()</code> and <code>opt()</code> methods return an
\r
78 * object which you can cast or query for type. There are also typed
\r
79 * <code>get</code> and <code>opt</code> methods that do type checking and type
\r
82 * The texts produced by the <code>toString</code> methods strictly conform to
\r
83 * JSON syntax rules. The constructors are more forgiving in the texts they will
\r
86 * <li>An extra <code>,</code> <small>(comma)</small> may appear just
\r
87 * before the closing bracket.</li>
\r
88 * <li>The <code>null</code> value will be inserted when there is <code>,</code>
\r
89 * <small>(comma)</small> elision.</li>
\r
90 * <li>Strings may be quoted with <code>'</code> <small>(single
\r
91 * quote)</small>.</li>
\r
92 * <li>Strings do not need to be quoted at all if they do not begin with a quote
\r
93 * or single quote, and if they do not contain leading or trailing spaces, and
\r
94 * if they do not contain any of these characters:
\r
95 * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and
\r
96 * if they are not the reserved words <code>true</code>, <code>false</code>, or
\r
97 * <code>null</code>.</li>
\r
98 * <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
\r
99 * well as by <code>,</code> <small>(comma)</small>.</li>
\r
103 * @version 2012-11-13
\r
105 public class JSONArray {
\r
108 * The arrayList where the JSONArray's properties are kept.
\r
110 private final List<Object> myArrayList;
\r
113 * Construct an empty JSONArray.
\r
115 public JSONArray() {
\r
116 this.myArrayList = new ArrayList<Object>();
\r
120 * Construct a JSONArray from a JSONTokener.
\r
124 * @throws JSONException
\r
125 * If there is a syntax error.
\r
127 public JSONArray(JSONTokener x) throws JSONException {
\r
129 if (x.nextClean() != '[') {
\r
130 throw x.syntaxError("A JSONArray text must start with '['");
\r
132 if (x.nextClean() != ']') {
\r
135 if (x.nextClean() == ',') {
\r
137 this.myArrayList.add(JSONObject.NULL);
\r
140 this.myArrayList.add(x.nextValue());
\r
142 switch (x.nextClean()) {
\r
145 if (x.nextClean() == ']') {
\r
153 throw x.syntaxError("Expected a ',' or ']'");
\r
160 * Construct a JSONArray from a source JSON text.
\r
163 * A string that begins with <code>[</code> <small>(left
\r
164 * bracket)</small> and ends with <code>]</code>
\r
165 * <small>(right bracket)</small>.
\r
166 * @throws JSONException
\r
167 * If there is a syntax error.
\r
169 public JSONArray(String source) throws JSONException {
\r
170 this(new JSONTokener(source));
\r
174 * Construct a JSONArray from a Collection.
\r
176 * @param collection
\r
179 public JSONArray(Collection<Object> collection) {
\r
180 this.myArrayList = new ArrayList<Object>();
\r
181 if (collection != null) {
\r
182 Iterator<Object> iter = collection.iterator();
\r
183 while (iter.hasNext()) {
\r
184 this.myArrayList.add(JSONObject.wrap(iter.next()));
\r
190 * Construct a JSONArray from an array
\r
192 * @throws JSONException
\r
195 public JSONArray(Object array) throws JSONException {
\r
197 if (array.getClass().isArray()) {
\r
198 int length = Array.getLength(array);
\r
199 for (int i = 0; i < length; i += 1) {
\r
200 this.put(JSONObject.wrap(Array.get(array, i)));
\r
203 throw new JSONException(
\r
204 "JSONArray initial value should be a string or collection or array.");
\r
209 * Get the object value associated with an index.
\r
212 * The index must be between 0 and length() - 1.
\r
213 * @return An object value.
\r
214 * @throws JSONException
\r
215 * If there is no value for the index.
\r
217 public Object get(int index) throws JSONException {
\r
218 Object object = this.opt(index);
\r
219 if (object == null) {
\r
220 throw new JSONException("JSONArray[" + index + "] not found.");
\r
226 * Get the boolean value associated with an index. The string values "true"
\r
227 * and "false" are converted to boolean.
\r
230 * The index must be between 0 and length() - 1.
\r
231 * @return The truth.
\r
232 * @throws JSONException
\r
233 * If there is no value for the index or if the value is not
\r
234 * convertible to boolean.
\r
236 public boolean getBoolean(int index) throws JSONException {
\r
237 Object object = this.get(index);
\r
238 if (object.equals(Boolean.FALSE)
\r
239 || (object instanceof String && ((String) object)
\r
240 .equalsIgnoreCase("false"))) {
\r
242 } else if (object.equals(Boolean.TRUE)
\r
243 || (object instanceof String && ((String) object)
\r
244 .equalsIgnoreCase("true"))) {
\r
247 throw new JSONException("JSONArray[" + index + "] is not a boolean.");
\r
251 * Get the double value associated with an index.
\r
254 * The index must be between 0 and length() - 1.
\r
255 * @return The value.
\r
256 * @throws JSONException
\r
257 * If the key is not found or if the value cannot be converted
\r
260 public double getDouble(int index) throws JSONException {
\r
261 Object object = this.get(index);
\r
263 return object instanceof Number ? ((Number) object).doubleValue()
\r
264 : Double.parseDouble((String) object);
\r
265 } catch (Exception e) {
\r
266 throw new JSONException("JSONArray[" + index + "] is not a number.");
\r
271 * Get the int value associated with an index.
\r
274 * The index must be between 0 and length() - 1.
\r
275 * @return The value.
\r
276 * @throws JSONException
\r
277 * If the key is not found or if the value is not a number.
\r
279 public int getInt(int index) throws JSONException {
\r
280 Object object = this.get(index);
\r
282 return object instanceof Number ? ((Number) object).intValue()
\r
283 : Integer.parseInt((String) object);
\r
284 } catch (Exception e) {
\r
285 throw new JSONException("JSONArray[" + index + "] is not a number.");
\r
290 * Get the JSONArray associated with an index.
\r
293 * The index must be between 0 and length() - 1.
\r
294 * @return A JSONArray value.
\r
295 * @throws JSONException
\r
296 * If there is no value for the index. or if the value is not a
\r
299 public JSONArray getJSONArray(int index) throws JSONException {
\r
300 Object object = this.get(index);
\r
301 if (object instanceof JSONArray) {
\r
302 return (JSONArray) object;
\r
304 throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
\r
308 * Get the JSONObject associated with an index.
\r
312 * @return A JSONObject value.
\r
313 * @throws JSONException
\r
314 * If there is no value for the index or if the value is not a
\r
317 public JSONObject getJSONObject(int index) throws JSONException {
\r
318 Object object = this.get(index);
\r
319 if (object instanceof JSONObject) {
\r
320 return (JSONObject) object;
\r
322 throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
\r
326 * Get the long value associated with an index.
\r
329 * The index must be between 0 and length() - 1.
\r
330 * @return The value.
\r
331 * @throws JSONException
\r
332 * If the key is not found or if the value cannot be converted
\r
335 public long getLong(int index) throws JSONException {
\r
336 Object object = this.get(index);
\r
338 return object instanceof Number ? ((Number) object).longValue()
\r
339 : Long.parseLong((String) object);
\r
340 } catch (Exception e) {
\r
341 throw new JSONException("JSONArray[" + index + "] is not a number.");
\r
346 * Get the string associated with an index.
\r
349 * The index must be between 0 and length() - 1.
\r
350 * @return A string value.
\r
351 * @throws JSONException
\r
352 * If there is no string value for the index.
\r
354 public String getString(int index) throws JSONException {
\r
355 Object object = this.get(index);
\r
356 if (object instanceof String) {
\r
357 return (String) object;
\r
359 throw new JSONException("JSONArray[" + index + "] not a string.");
\r
363 * Determine if the value is null.
\r
366 * The index must be between 0 and length() - 1.
\r
367 * @return true if the value at the index is null, or if there is no value.
\r
369 public boolean isNull(int index) {
\r
370 return JSONObject.NULL.equals(this.opt(index));
\r
374 * Make a string from the contents of this JSONArray. The
\r
375 * <code>separator</code> string is inserted between each element. Warning:
\r
376 * This method assumes that the data structure is acyclical.
\r
379 * A string that will be inserted between the elements.
\r
380 * @return a string.
\r
381 * @throws JSONException
\r
382 * If the array contains an invalid number.
\r
384 public String join(String separator) throws JSONException {
\r
385 int len = this.length();
\r
386 StringBuffer sb = new StringBuffer();
\r
388 for (int i = 0; i < len; i += 1) {
\r
390 sb.append(separator);
\r
392 sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
\r
394 return sb.toString();
\r
398 * Get the number of elements in the JSONArray, included nulls.
\r
400 * @return The length (or size).
\r
402 public int length() {
\r
403 return this.myArrayList.size();
\r
407 * Get the optional object value associated with an index.
\r
410 * The index must be between 0 and length() - 1.
\r
411 * @return An object value, or null if there is no object at that index.
\r
413 public Object opt(int index) {
\r
414 return (index < 0 || index >= this.length()) ? null : this.myArrayList
\r
419 * Get the optional boolean value associated with an index. It returns false
\r
420 * if there is no value at that index, or if the value is not Boolean.TRUE
\r
421 * or the String "true".
\r
424 * The index must be between 0 and length() - 1.
\r
425 * @return The truth.
\r
427 public boolean optBoolean(int index) {
\r
428 return this.optBoolean(index, false);
\r
432 * Get the optional boolean value associated with an index. It returns the
\r
433 * defaultValue if there is no value at that index or if it is not a Boolean
\r
434 * or the String "true" or "false" (case insensitive).
\r
437 * The index must be between 0 and length() - 1.
\r
438 * @param defaultValue
\r
439 * A boolean default.
\r
440 * @return The truth.
\r
442 public boolean optBoolean(int index, boolean defaultValue) {
\r
444 return this.getBoolean(index);
\r
445 } catch (Exception e) {
\r
446 return defaultValue;
\r
451 * Get the optional double value associated with an index. NaN is returned
\r
452 * if there is no value for the index, or if the value is not a number and
\r
453 * cannot be converted to a number.
\r
456 * The index must be between 0 and length() - 1.
\r
457 * @return The value.
\r
459 public double optDouble(int index) {
\r
460 return this.optDouble(index, Double.NaN);
\r
464 * Get the optional double value associated with an index. The defaultValue
\r
465 * is returned if there is no value for the index, or if the value is not a
\r
466 * number and cannot be converted to a number.
\r
470 * @param defaultValue
\r
471 * The default value.
\r
472 * @return The value.
\r
474 public double optDouble(int index, double defaultValue) {
\r
476 return this.getDouble(index);
\r
477 } catch (Exception e) {
\r
478 return defaultValue;
\r
483 * Get the optional int value associated with an index. Zero is returned if
\r
484 * there is no value for the index, or if the value is not a number and
\r
485 * cannot be converted to a number.
\r
488 * The index must be between 0 and length() - 1.
\r
489 * @return The value.
\r
491 public int optInt(int index) {
\r
492 return this.optInt(index, 0);
\r
496 * Get the optional int value associated with an index. The defaultValue is
\r
497 * returned if there is no value for the index, or if the value is not a
\r
498 * number and cannot be converted to a number.
\r
501 * The index must be between 0 and length() - 1.
\r
502 * @param defaultValue
\r
503 * The default value.
\r
504 * @return The value.
\r
506 public int optInt(int index, int defaultValue) {
\r
508 return this.getInt(index);
\r
509 } catch (Exception e) {
\r
510 return defaultValue;
\r
515 * Get the optional JSONArray associated with an index.
\r
519 * @return A JSONArray value, or null if the index has no value, or if the
\r
520 * value is not a JSONArray.
\r
522 public JSONArray optJSONArray(int index) {
\r
523 Object o = this.opt(index);
\r
524 return o instanceof JSONArray ? (JSONArray) o : null;
\r
528 * Get the optional JSONObject associated with an index. Null is returned if
\r
529 * the key is not found, or null if the index has no value, or if the value
\r
530 * is not a JSONObject.
\r
533 * The index must be between 0 and length() - 1.
\r
534 * @return A JSONObject value.
\r
536 public JSONObject optJSONObject(int index) {
\r
537 Object o = this.opt(index);
\r
538 return o instanceof JSONObject ? (JSONObject) o : null;
\r
542 * Get the optional long value associated with an index. Zero is returned if
\r
543 * there is no value for the index, or if the value is not a number and
\r
544 * cannot be converted to a number.
\r
547 * The index must be between 0 and length() - 1.
\r
548 * @return The value.
\r
550 public long optLong(int index) {
\r
551 return this.optLong(index, 0);
\r
555 * Get the optional long value associated with an index. The defaultValue is
\r
556 * returned if there is no value for the index, or if the value is not a
\r
557 * number and cannot be converted to a number.
\r
560 * The index must be between 0 and length() - 1.
\r
561 * @param defaultValue
\r
562 * The default value.
\r
563 * @return The value.
\r
565 public long optLong(int index, long defaultValue) {
\r
567 return this.getLong(index);
\r
568 } catch (Exception e) {
\r
569 return defaultValue;
\r
574 * Get the optional string value associated with an index. It returns an
\r
575 * empty string if there is no value at that index. If the value is not a
\r
576 * string and is not null, then it is coverted to a string.
\r
579 * The index must be between 0 and length() - 1.
\r
580 * @return A String value.
\r
582 public String optString(int index) {
\r
583 return this.optString(index, "");
\r
587 * Get the optional string associated with an index. The defaultValue is
\r
588 * returned if the key is not found.
\r
591 * The index must be between 0 and length() - 1.
\r
592 * @param defaultValue
\r
593 * The default value.
\r
594 * @return A String value.
\r
596 public String optString(int index, String defaultValue) {
\r
597 Object object = this.opt(index);
\r
598 return JSONObject.NULL.equals(object) ? defaultValue : object
\r
603 * Append a boolean value. This increases the array's length by one.
\r
609 public JSONArray put(boolean value) {
\r
610 this.put(value ? Boolean.TRUE : Boolean.FALSE);
\r
615 * Put a value in the JSONArray, where the value will be a JSONArray which
\r
616 * is produced from a Collection.
\r
619 * A Collection value.
\r
622 public JSONArray put(Collection<Object> value) {
\r
623 this.put(new JSONArray(value));
\r
628 * Append a double value. This increases the array's length by one.
\r
632 * @throws JSONException
\r
633 * if the value is not finite.
\r
636 public JSONArray put(double value) throws JSONException {
\r
637 Double d = new Double(value);
\r
638 JSONObject.testValidity(d);
\r
644 * Append an int value. This increases the array's length by one.
\r
650 public JSONArray put(int value) {
\r
651 this.put(new Integer(value));
\r
656 * Append an long value. This increases the array's length by one.
\r
662 public JSONArray put(long value) {
\r
663 this.put(new Long(value));
\r
668 * Put a value in the JSONArray, where the value will be a JSONObject which
\r
669 * is produced from a Map.
\r
675 public JSONArray put(Map<String,Object> value) {
\r
676 this.put(new JSONObject(value));
\r
681 * Append an object value. This increases the array's length by one.
\r
684 * An object value. The value should be a Boolean, Double,
\r
685 * Integer, JSONArray, JSONObject, Long, or String, or the
\r
686 * JSONObject.NULL object.
\r
689 public JSONArray put(Object value) {
\r
690 this.myArrayList.add(value);
\r
695 * Put or replace a boolean value in the JSONArray. If the index is greater
\r
696 * than the length of the JSONArray, then null elements will be added as
\r
697 * necessary to pad it out.
\r
704 * @throws JSONException
\r
705 * If the index is negative.
\r
707 public JSONArray put(int index, boolean value) throws JSONException {
\r
708 this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
\r
713 * Put a value in the JSONArray, where the value will be a JSONArray which
\r
714 * is produced from a Collection.
\r
719 * A Collection value.
\r
721 * @throws JSONException
\r
722 * If the index is negative or if the value is not finite.
\r
724 public JSONArray put(int index, Collection<Object> value) throws JSONException {
\r
725 this.put(index, new JSONArray(value));
\r
730 * Put or replace a double value. If the index is greater than the length of
\r
731 * the JSONArray, then null elements will be added as necessary to pad it
\r
739 * @throws JSONException
\r
740 * If the index is negative or if the value is not finite.
\r
742 public JSONArray put(int index, double value) throws JSONException {
\r
743 this.put(index, new Double(value));
\r
748 * Put or replace an int value. If the index is greater than the length of
\r
749 * the JSONArray, then null elements will be added as necessary to pad it
\r
757 * @throws JSONException
\r
758 * If the index is negative.
\r
760 public JSONArray put(int index, int value) throws JSONException {
\r
761 this.put(index, new Integer(value));
\r
766 * Put or replace a long value. If the index is greater than the length of
\r
767 * the JSONArray, then null elements will be added as necessary to pad it
\r
775 * @throws JSONException
\r
776 * If the index is negative.
\r
778 public JSONArray put(int index, long value) throws JSONException {
\r
779 this.put(index, new Long(value));
\r
784 * Put a value in the JSONArray, where the value will be a JSONObject that
\r
785 * is produced from a Map.
\r
792 * @throws JSONException
\r
793 * If the index is negative or if the the value is an invalid
\r
796 public JSONArray put(int index, Map<String,Object> value) throws JSONException {
\r
797 this.put(index, new JSONObject(value));
\r
802 * Put or replace an object value in the JSONArray. If the index is greater
\r
803 * than the length of the JSONArray, then null elements will be added as
\r
804 * necessary to pad it out.
\r
809 * The value to put into the array. The value should be a
\r
810 * Boolean, Double, Integer, JSONArray, JSONObject, Long, or
\r
811 * String, or the JSONObject.NULL object.
\r
813 * @throws JSONException
\r
814 * If the index is negative or if the the value is an invalid
\r
817 public JSONArray put(int index, Object value) throws JSONException {
\r
818 JSONObject.testValidity(value);
\r
820 throw new JSONException("JSONArray[" + index + "] not found.");
\r
822 if (index < this.length()) {
\r
823 this.myArrayList.set(index, value);
\r
825 while (index != this.length()) {
\r
826 this.put(JSONObject.NULL);
\r
834 * Remove an index and close the hole.
\r
837 * The index of the element to be removed.
\r
838 * @return The value that was associated with the index, or null if there
\r
841 public Object remove(int index) {
\r
842 Object o = this.opt(index);
\r
843 this.myArrayList.remove(index);
\r
848 * Produce a JSONObject by combining a JSONArray of names with the values of
\r
852 * A JSONArray containing a list of key strings. These will be
\r
853 * paired with the values.
\r
854 * @return A JSONObject, or null if there are no names or if this JSONArray
\r
856 * @throws JSONException
\r
857 * If any of the names are null.
\r
859 public JSONObject toJSONObject(JSONArray names) throws JSONException {
\r
860 if (names == null || names.length() == 0 || this.length() == 0) {
\r
863 JSONObject jo = new JSONObject();
\r
864 for (int i = 0; i < names.length(); i += 1) {
\r
865 jo.put(names.getString(i), this.opt(i));
\r
871 * Make a JSON text of this JSONArray. For compactness, no unnecessary
\r
872 * whitespace is added. If it is not possible to produce a syntactically
\r
873 * correct JSON text then null will be returned instead. This could occur if
\r
874 * the array contains an invalid number.
\r
876 * Warning: This method assumes that the data structure is acyclical.
\r
878 * @return a printable, displayable, transmittable representation of the
\r
881 public String toString() {
\r
883 return this.toString(0);
\r
884 } catch (Exception e) {
\r
890 * Make a prettyprinted JSON text of this JSONArray. Warning: This method
\r
891 * assumes that the data structure is acyclical.
\r
893 * @param indentFactor
\r
894 * The number of spaces to add to each level of indentation.
\r
895 * @return a printable, displayable, transmittable representation of the
\r
896 * object, beginning with <code>[</code> <small>(left
\r
897 * bracket)</small> and ending with <code>]</code>
\r
898 * <small>(right bracket)</small>.
\r
899 * @throws JSONException
\r
901 public String toString(int indentFactor) throws JSONException {
\r
902 StringWriter sw = new StringWriter();
\r
903 synchronized (sw.getBuffer()) {
\r
904 return this.write(sw, indentFactor, 0).toString();
\r
909 * Write the contents of the JSONArray as JSON text to a writer. For
\r
910 * compactness, no whitespace is added.
\r
912 * Warning: This method assumes that the data structure is acyclical.
\r
914 * @return The writer.
\r
915 * @throws JSONException
\r
917 public Writer write(Writer writer) throws JSONException {
\r
918 return this.write(writer, 0, 0);
\r
922 * Write the contents of the JSONArray as JSON text to a writer. For
\r
923 * compactness, no whitespace is added.
\r
925 * Warning: This method assumes that the data structure is acyclical.
\r
927 * @param indentFactor
\r
928 * The number of spaces to add to each level of indentation.
\r
930 * The indention of the top level.
\r
931 * @return The writer.
\r
932 * @throws JSONException
\r
934 Writer write(Writer writer, int indentFactor, int indent)
\r
935 throws JSONException {
\r
937 boolean commanate = false;
\r
938 int length = this.length();
\r
942 JSONObject.writeValue(writer, this.myArrayList.get(0),
\r
943 indentFactor, indent);
\r
944 } else if (length != 0) {
\r
945 final int newindent = indent + indentFactor;
\r
947 for (int i = 0; i < length; i += 1) {
\r
951 if (indentFactor > 0) {
\r
952 writer.write('\n');
\r
954 JSONObject.indent(writer, newindent);
\r
955 JSONObject.writeValue(writer, this.myArrayList.get(i),
\r
956 indentFactor, newindent);
\r
959 if (indentFactor > 0) {
\r
960 writer.write('\n');
\r
962 JSONObject.indent(writer, indent);
\r
966 } catch (IOException e) {
\r
967 throw new JSONException(e);
\r