[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / JSONArray.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.json;\r
24 \r
25 /*\r
26  Copyright (c) 2002 JSON.org\r
27 \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
34 \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
37 \r
38  The Software shall be used for Good, not Evil.\r
39 \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
46  SOFTWARE.\r
47  */\r
48 \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
58 \r
59 /**\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
68  * <p>\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
71  * <p>\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
76  * <p>\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
80  * coercion for you.\r
81  * <p>\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
84  * accept:\r
85  * <ul>\r
86  * <li>An extra <code>,</code>&nbsp;<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  * &nbsp;<small>(comma)</small> elision.</li>\r
90  * <li>Strings may be quoted with <code>'</code>&nbsp;<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
100  * </ul>\r
101  *\r
102  * @author JSON.org\r
103  * @version 2012-11-13\r
104  */\r
105 public class JSONArray {\r
106 \r
107     /**\r
108      * The arrayList where the JSONArray's properties are kept.\r
109      */\r
110     private final List<Object> myArrayList;\r
111 \r
112     /**\r
113      * Construct an empty JSONArray.\r
114      */\r
115     public JSONArray() {\r
116         this.myArrayList = new ArrayList<Object>();\r
117     }\r
118 \r
119     /**\r
120      * Construct a JSONArray from a JSONTokener.\r
121      *\r
122      * @param x\r
123      *            A JSONTokener\r
124      * @throws JSONException\r
125      *             If there is a syntax error.\r
126      */\r
127     public JSONArray(JSONTokener x) throws JSONException {\r
128         this();\r
129         if (x.nextClean() != '[') {\r
130             throw x.syntaxError("A JSONArray text must start with '['");\r
131         }\r
132         if (x.nextClean() != ']') {\r
133             x.back();\r
134             for (;;) {\r
135                 if (x.nextClean() == ',') {\r
136                     x.back();\r
137                     this.myArrayList.add(JSONObject.NULL);\r
138                 } else {\r
139                     x.back();\r
140                     this.myArrayList.add(x.nextValue());\r
141                 }\r
142                 switch (x.nextClean()) {\r
143                 case ';':\r
144                 case ',':\r
145                     if (x.nextClean() == ']') {\r
146                         return;\r
147                     }\r
148                     x.back();\r
149                     break;\r
150                 case ']':\r
151                     return;\r
152                 default:\r
153                     throw x.syntaxError("Expected a ',' or ']'");\r
154                 }\r
155             }\r
156         }\r
157     }\r
158 \r
159     /**\r
160      * Construct a JSONArray from a source JSON text.\r
161      *\r
162      * @param source\r
163      *            A string that begins with <code>[</code>&nbsp;<small>(left\r
164      *            bracket)</small> and ends with <code>]</code>\r
165      *            &nbsp;<small>(right bracket)</small>.\r
166      * @throws JSONException\r
167      *             If there is a syntax error.\r
168      */\r
169     public JSONArray(String source) throws JSONException {\r
170         this(new JSONTokener(source));\r
171     }\r
172 \r
173     /**\r
174      * Construct a JSONArray from a Collection.\r
175      *\r
176      * @param collection\r
177      *            A Collection.\r
178      */\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
185             }\r
186         }\r
187     }\r
188 \r
189     /**\r
190      * Construct a JSONArray from an array\r
191      *\r
192      * @throws JSONException\r
193      *             If not an array.\r
194      */\r
195     public JSONArray(Object array) throws JSONException {\r
196         this();\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
201             }\r
202         } else {\r
203             throw new JSONException(\r
204                     "JSONArray initial value should be a string or collection or array.");\r
205         }\r
206     }\r
207 \r
208     /**\r
209      * Get the object value associated with an index.\r
210      *\r
211      * @param 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
216      */\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
221         }\r
222         return object;\r
223     }\r
224 \r
225     /**\r
226      * Get the boolean value associated with an index. The string values "true"\r
227      * and "false" are converted to boolean.\r
228      *\r
229      * @param index\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
235      */\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
241             return false;\r
242         } else if (object.equals(Boolean.TRUE)\r
243                 || (object instanceof String && ((String) object)\r
244                         .equalsIgnoreCase("true"))) {\r
245             return true;\r
246         }\r
247         throw new JSONException("JSONArray[" + index + "] is not a boolean.");\r
248     }\r
249 \r
250     /**\r
251      * Get the double value associated with an index.\r
252      *\r
253      * @param 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
258      *             to a number.\r
259      */\r
260     public double getDouble(int index) throws JSONException {\r
261         Object object = this.get(index);\r
262         try {\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
267         }\r
268     }\r
269 \r
270     /**\r
271      * Get the int value associated with an index.\r
272      *\r
273      * @param 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
278      */\r
279     public int getInt(int index) throws JSONException {\r
280         Object object = this.get(index);\r
281         try {\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
286         }\r
287     }\r
288 \r
289     /**\r
290      * Get the JSONArray associated with an index.\r
291      *\r
292      * @param 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
297      *             JSONArray\r
298      */\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
303         }\r
304         throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");\r
305     }\r
306 \r
307     /**\r
308      * Get the JSONObject associated with an index.\r
309      *\r
310      * @param index\r
311      *            subscript\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
315      *             JSONObject\r
316      */\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
321         }\r
322         throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");\r
323     }\r
324 \r
325     /**\r
326      * Get the long value associated with an index.\r
327      *\r
328      * @param 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
333      *             to a number.\r
334      */\r
335     public long getLong(int index) throws JSONException {\r
336         Object object = this.get(index);\r
337         try {\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
342         }\r
343     }\r
344 \r
345     /**\r
346      * Get the string associated with an index.\r
347      *\r
348      * @param 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
353      */\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
358         }\r
359         throw new JSONException("JSONArray[" + index + "] not a string.");\r
360     }\r
361 \r
362     /**\r
363      * Determine if the value is null.\r
364      *\r
365      * @param index\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
368      */\r
369     public boolean isNull(int index) {\r
370         return JSONObject.NULL.equals(this.opt(index));\r
371     }\r
372 \r
373     /**\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
377      *\r
378      * @param separator\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
383      */\r
384     public String join(String separator) throws JSONException {\r
385         int len = this.length();\r
386         StringBuffer sb = new StringBuffer();\r
387 \r
388         for (int i = 0; i < len; i += 1) {\r
389             if (i > 0) {\r
390                 sb.append(separator);\r
391             }\r
392             sb.append(JSONObject.valueToString(this.myArrayList.get(i)));\r
393         }\r
394         return sb.toString();\r
395     }\r
396 \r
397     /**\r
398      * Get the number of elements in the JSONArray, included nulls.\r
399      *\r
400      * @return The length (or size).\r
401      */\r
402     public int length() {\r
403         return this.myArrayList.size();\r
404     }\r
405 \r
406     /**\r
407      * Get the optional object value associated with an index.\r
408      *\r
409      * @param 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
412      */\r
413     public Object opt(int index) {\r
414         return (index < 0 || index >= this.length()) ? null : this.myArrayList\r
415                 .get(index);\r
416     }\r
417 \r
418     /**\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
422      *\r
423      * @param index\r
424      *            The index must be between 0 and length() - 1.\r
425      * @return The truth.\r
426      */\r
427     public boolean optBoolean(int index) {\r
428         return this.optBoolean(index, false);\r
429     }\r
430 \r
431     /**\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
435      *\r
436      * @param index\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
441      */\r
442     public boolean optBoolean(int index, boolean defaultValue) {\r
443         try {\r
444             return this.getBoolean(index);\r
445         } catch (Exception e) {\r
446             return defaultValue;\r
447         }\r
448     }\r
449 \r
450     /**\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
454      *\r
455      * @param index\r
456      *            The index must be between 0 and length() - 1.\r
457      * @return The value.\r
458      */\r
459     public double optDouble(int index) {\r
460         return this.optDouble(index, Double.NaN);\r
461     }\r
462 \r
463     /**\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
467      *\r
468      * @param index\r
469      *            subscript\r
470      * @param defaultValue\r
471      *            The default value.\r
472      * @return The value.\r
473      */\r
474     public double optDouble(int index, double defaultValue) {\r
475         try {\r
476             return this.getDouble(index);\r
477         } catch (Exception e) {\r
478             return defaultValue;\r
479         }\r
480     }\r
481 \r
482     /**\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
486      *\r
487      * @param index\r
488      *            The index must be between 0 and length() - 1.\r
489      * @return The value.\r
490      */\r
491     public int optInt(int index) {\r
492         return this.optInt(index, 0);\r
493     }\r
494 \r
495     /**\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
499      *\r
500      * @param index\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
505      */\r
506     public int optInt(int index, int defaultValue) {\r
507         try {\r
508             return this.getInt(index);\r
509         } catch (Exception e) {\r
510             return defaultValue;\r
511         }\r
512     }\r
513 \r
514     /**\r
515      * Get the optional JSONArray associated with an index.\r
516      *\r
517      * @param index\r
518      *            subscript\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
521      */\r
522     public JSONArray optJSONArray(int index) {\r
523         Object o = this.opt(index);\r
524         return o instanceof JSONArray ? (JSONArray) o : null;\r
525     }\r
526 \r
527     /**\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
531      *\r
532      * @param index\r
533      *            The index must be between 0 and length() - 1.\r
534      * @return A JSONObject value.\r
535      */\r
536     public JSONObject optJSONObject(int index) {\r
537         Object o = this.opt(index);\r
538         return o instanceof JSONObject ? (JSONObject) o : null;\r
539     }\r
540 \r
541     /**\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
545      *\r
546      * @param index\r
547      *            The index must be between 0 and length() - 1.\r
548      * @return The value.\r
549      */\r
550     public long optLong(int index) {\r
551         return this.optLong(index, 0);\r
552     }\r
553 \r
554     /**\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
558      *\r
559      * @param index\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
564      */\r
565     public long optLong(int index, long defaultValue) {\r
566         try {\r
567             return this.getLong(index);\r
568         } catch (Exception e) {\r
569             return defaultValue;\r
570         }\r
571     }\r
572 \r
573     /**\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
577      *\r
578      * @param index\r
579      *            The index must be between 0 and length() - 1.\r
580      * @return A String value.\r
581      */\r
582     public String optString(int index) {\r
583         return this.optString(index, "");\r
584     }\r
585 \r
586     /**\r
587      * Get the optional string associated with an index. The defaultValue is\r
588      * returned if the key is not found.\r
589      *\r
590      * @param index\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
595      */\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
599                 .toString();\r
600     }\r
601 \r
602     /**\r
603      * Append a boolean value. This increases the array's length by one.\r
604      *\r
605      * @param value\r
606      *            A boolean value.\r
607      * @return this.\r
608      */\r
609     public JSONArray put(boolean value) {\r
610         this.put(value ? Boolean.TRUE : Boolean.FALSE);\r
611         return this;\r
612     }\r
613 \r
614     /**\r
615      * Put a value in the JSONArray, where the value will be a JSONArray which\r
616      * is produced from a Collection.\r
617      *\r
618      * @param value\r
619      *            A Collection value.\r
620      * @return this.\r
621      */\r
622     public JSONArray put(Collection<Object> value) {\r
623         this.put(new JSONArray(value));\r
624         return this;\r
625     }\r
626 \r
627     /**\r
628      * Append a double value. This increases the array's length by one.\r
629      *\r
630      * @param value\r
631      *            A double value.\r
632      * @throws JSONException\r
633      *             if the value is not finite.\r
634      * @return this.\r
635      */\r
636     public JSONArray put(double value) throws JSONException {\r
637         Double d = new Double(value);\r
638         JSONObject.testValidity(d);\r
639         this.put(d);\r
640         return this;\r
641     }\r
642 \r
643     /**\r
644      * Append an int value. This increases the array's length by one.\r
645      *\r
646      * @param value\r
647      *            An int value.\r
648      * @return this.\r
649      */\r
650     public JSONArray put(int value) {\r
651         this.put(new Integer(value));\r
652         return this;\r
653     }\r
654 \r
655     /**\r
656      * Append an long value. This increases the array's length by one.\r
657      *\r
658      * @param value\r
659      *            A long value.\r
660      * @return this.\r
661      */\r
662     public JSONArray put(long value) {\r
663         this.put(new Long(value));\r
664         return this;\r
665     }\r
666 \r
667     /**\r
668      * Put a value in the JSONArray, where the value will be a JSONObject which\r
669      * is produced from a Map.\r
670      *\r
671      * @param value\r
672      *            A Map value.\r
673      * @return this.\r
674      */\r
675     public JSONArray put(Map<String,Object> value) {\r
676         this.put(new JSONObject(value));\r
677         return this;\r
678     }\r
679 \r
680     /**\r
681      * Append an object value. This increases the array's length by one.\r
682      *\r
683      * @param value\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
687      * @return this.\r
688      */\r
689     public JSONArray put(Object value) {\r
690         this.myArrayList.add(value);\r
691         return this;\r
692     }\r
693 \r
694     /**\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
698      *\r
699      * @param index\r
700      *            The subscript.\r
701      * @param value\r
702      *            A boolean value.\r
703      * @return this.\r
704      * @throws JSONException\r
705      *             If the index is negative.\r
706      */\r
707     public JSONArray put(int index, boolean value) throws JSONException {\r
708         this.put(index, value ? Boolean.TRUE : Boolean.FALSE);\r
709         return this;\r
710     }\r
711 \r
712     /**\r
713      * Put a value in the JSONArray, where the value will be a JSONArray which\r
714      * is produced from a Collection.\r
715      *\r
716      * @param index\r
717      *            The subscript.\r
718      * @param value\r
719      *            A Collection value.\r
720      * @return this.\r
721      * @throws JSONException\r
722      *             If the index is negative or if the value is not finite.\r
723      */\r
724     public JSONArray put(int index, Collection<Object> value) throws JSONException {\r
725         this.put(index, new JSONArray(value));\r
726         return this;\r
727     }\r
728 \r
729     /**\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
732      * out.\r
733      *\r
734      * @param index\r
735      *            The subscript.\r
736      * @param value\r
737      *            A double value.\r
738      * @return this.\r
739      * @throws JSONException\r
740      *             If the index is negative or if the value is not finite.\r
741      */\r
742     public JSONArray put(int index, double value) throws JSONException {\r
743         this.put(index, new Double(value));\r
744         return this;\r
745     }\r
746 \r
747     /**\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
750      * out.\r
751      *\r
752      * @param index\r
753      *            The subscript.\r
754      * @param value\r
755      *            An int value.\r
756      * @return this.\r
757      * @throws JSONException\r
758      *             If the index is negative.\r
759      */\r
760     public JSONArray put(int index, int value) throws JSONException {\r
761         this.put(index, new Integer(value));\r
762         return this;\r
763     }\r
764 \r
765     /**\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
768      * out.\r
769      *\r
770      * @param index\r
771      *            The subscript.\r
772      * @param value\r
773      *            A long value.\r
774      * @return this.\r
775      * @throws JSONException\r
776      *             If the index is negative.\r
777      */\r
778     public JSONArray put(int index, long value) throws JSONException {\r
779         this.put(index, new Long(value));\r
780         return this;\r
781     }\r
782 \r
783     /**\r
784      * Put a value in the JSONArray, where the value will be a JSONObject that\r
785      * is produced from a Map.\r
786      *\r
787      * @param index\r
788      *            The subscript.\r
789      * @param value\r
790      *            The Map value.\r
791      * @return this.\r
792      * @throws JSONException\r
793      *             If the index is negative or if the the value is an invalid\r
794      *             number.\r
795      */\r
796     public JSONArray put(int index, Map<String,Object> value) throws JSONException {\r
797         this.put(index, new JSONObject(value));\r
798         return this;\r
799     }\r
800 \r
801     /**\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
805      *\r
806      * @param index\r
807      *            The subscript.\r
808      * @param value\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
812      * @return this.\r
813      * @throws JSONException\r
814      *             If the index is negative or if the the value is an invalid\r
815      *             number.\r
816      */\r
817     public JSONArray put(int index, Object value) throws JSONException {\r
818         JSONObject.testValidity(value);\r
819         if (index < 0) {\r
820             throw new JSONException("JSONArray[" + index + "] not found.");\r
821         }\r
822         if (index < this.length()) {\r
823             this.myArrayList.set(index, value);\r
824         } else {\r
825             while (index != this.length()) {\r
826                 this.put(JSONObject.NULL);\r
827             }\r
828             this.put(value);\r
829         }\r
830         return this;\r
831     }\r
832 \r
833     /**\r
834      * Remove an index and close the hole.\r
835      *\r
836      * @param index\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
839      *         was no value.\r
840      */\r
841     public Object remove(int index) {\r
842         Object o = this.opt(index);\r
843         this.myArrayList.remove(index);\r
844         return o;\r
845     }\r
846 \r
847     /**\r
848      * Produce a JSONObject by combining a JSONArray of names with the values of\r
849      * this JSONArray.\r
850      *\r
851      * @param names\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
855      *         has no values.\r
856      * @throws JSONException\r
857      *             If any of the names are null.\r
858      */\r
859     public JSONObject toJSONObject(JSONArray names) throws JSONException {\r
860         if (names == null || names.length() == 0 || this.length() == 0) {\r
861             return null;\r
862         }\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
866         }\r
867         return jo;\r
868     }\r
869 \r
870     /**\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
875      * <p>\r
876      * Warning: This method assumes that the data structure is acyclical.\r
877      *\r
878      * @return a printable, displayable, transmittable representation of the\r
879      *         array.\r
880      */\r
881     public String toString() {\r
882         try {\r
883             return this.toString(0);\r
884         } catch (Exception e) {\r
885             return null;\r
886         }\r
887     }\r
888 \r
889     /**\r
890      * Make a prettyprinted JSON text of this JSONArray. Warning: This method\r
891      * assumes that the data structure is acyclical.\r
892      *\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>&nbsp;<small>(left\r
897      *         bracket)</small> and ending with <code>]</code>\r
898      *         &nbsp;<small>(right bracket)</small>.\r
899      * @throws JSONException\r
900      */\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
905         }\r
906     }\r
907 \r
908     /**\r
909      * Write the contents of the JSONArray as JSON text to a writer. For\r
910      * compactness, no whitespace is added.\r
911      * <p>\r
912      * Warning: This method assumes that the data structure is acyclical.\r
913      *\r
914      * @return The writer.\r
915      * @throws JSONException\r
916      */\r
917     public Writer write(Writer writer) throws JSONException {\r
918         return this.write(writer, 0, 0);\r
919     }\r
920 \r
921     /**\r
922      * Write the contents of the JSONArray as JSON text to a writer. For\r
923      * compactness, no whitespace is added.\r
924      * <p>\r
925      * Warning: This method assumes that the data structure is acyclical.\r
926      *\r
927      * @param indentFactor\r
928      *            The number of spaces to add to each level of indentation.\r
929      * @param indent\r
930      *            The indention of the top level.\r
931      * @return The writer.\r
932      * @throws JSONException\r
933      */\r
934     Writer write(Writer writer, int indentFactor, int indent)\r
935             throws JSONException {\r
936         try {\r
937             boolean commanate = false;\r
938             int length = this.length();\r
939             writer.write('[');\r
940 \r
941             if (length == 1) {\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
946 \r
947                 for (int i = 0; i < length; i += 1) {\r
948                     if (commanate) {\r
949                         writer.write(',');\r
950                     }\r
951                     if (indentFactor > 0) {\r
952                         writer.write('\n');\r
953                     }\r
954                     JSONObject.indent(writer, newindent);\r
955                     JSONObject.writeValue(writer, this.myArrayList.get(i),\r
956                             indentFactor, newindent);\r
957                     commanate = true;\r
958                 }\r
959                 if (indentFactor > 0) {\r
960                     writer.write('\n');\r
961                 }\r
962                 JSONObject.indent(writer, indent);\r
963             }\r
964             writer.write(']');\r
965             return writer;\r
966         } catch (IOException e) {\r
967             throw new JSONException(e);\r
968         }\r
969     }\r
970 }\r