--- /dev/null
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ * * http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ * * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package org.json;\r
+\r
+/*\r
+ Copyright (c) 2002 JSON.org\r
+\r
+ Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ of this software and associated documentation files (the "Software"), to deal\r
+ in the Software without restriction, including without limitation the rights\r
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ copies of the Software, and to permit persons to whom the Software is\r
+ furnished to do so, subject to the following conditions:\r
+\r
+ The above copyright notice and this permission notice shall be included in all\r
+ copies or substantial portions of the Software.\r
+\r
+ The Software shall be used for Good, not Evil.\r
+\r
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ SOFTWARE.\r
+ */\r
+\r
+import java.io.IOException;\r
+import java.io.StringWriter;\r
+import java.io.Writer;\r
+import java.lang.reflect.Array;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+/**\r
+ * A JSONArray is an ordered sequence of values. Its external text form is a\r
+ * string wrapped in square brackets with commas separating the values. The\r
+ * internal form is an object having <code>get</code> and <code>opt</code>\r
+ * methods for accessing the values by index, and <code>put</code> methods for\r
+ * adding or replacing values. The values can be any of these types:\r
+ * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,\r
+ * <code>Number</code>, <code>String</code>, or the\r
+ * <code>JSONObject.NULL object</code>.\r
+ * <p>\r
+ * The constructor can convert a JSON text into a Java object. The\r
+ * <code>toString</code> method converts to JSON text.\r
+ * <p>\r
+ * A <code>get</code> method returns a value if one can be found, and throws an\r
+ * exception if one cannot be found. An <code>opt</code> method returns a\r
+ * default value instead of throwing an exception, and so is useful for\r
+ * obtaining optional values.\r
+ * <p>\r
+ * The generic <code>get()</code> and <code>opt()</code> methods return an\r
+ * object which you can cast or query for type. There are also typed\r
+ * <code>get</code> and <code>opt</code> methods that do type checking and type\r
+ * coercion for you.\r
+ * <p>\r
+ * The texts produced by the <code>toString</code> methods strictly conform to\r
+ * JSON syntax rules. The constructors are more forgiving in the texts they will\r
+ * accept:\r
+ * <ul>\r
+ * <li>An extra <code>,</code> <small>(comma)</small> may appear just\r
+ * before the closing bracket.</li>\r
+ * <li>The <code>null</code> value will be inserted when there is <code>,</code>\r
+ * <small>(comma)</small> elision.</li>\r
+ * <li>Strings may be quoted with <code>'</code> <small>(single\r
+ * quote)</small>.</li>\r
+ * <li>Strings do not need to be quoted at all if they do not begin with a quote\r
+ * or single quote, and if they do not contain leading or trailing spaces, and\r
+ * if they do not contain any of these characters:\r
+ * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and\r
+ * if they are not the reserved words <code>true</code>, <code>false</code>, or\r
+ * <code>null</code>.</li>\r
+ * <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as\r
+ * well as by <code>,</code> <small>(comma)</small>.</li>\r
+ * </ul>\r
+ *\r
+ * @author JSON.org\r
+ * @version 2012-11-13\r
+ */\r
+public class JSONArray {\r
+\r
+ /**\r
+ * The arrayList where the JSONArray's properties are kept.\r
+ */\r
+ private final List<Object> myArrayList;\r
+\r
+ /**\r
+ * Construct an empty JSONArray.\r
+ */\r
+ public JSONArray() {\r
+ this.myArrayList = new ArrayList<Object>();\r
+ }\r
+\r
+ /**\r
+ * Construct a JSONArray from a JSONTokener.\r
+ *\r
+ * @param x\r
+ * A JSONTokener\r
+ * @throws JSONException\r
+ * If there is a syntax error.\r
+ */\r
+ public JSONArray(JSONTokener x) throws JSONException {\r
+ this();\r
+ if (x.nextClean() != '[') {\r
+ throw x.syntaxError("A JSONArray text must start with '['");\r
+ }\r
+ if (x.nextClean() != ']') {\r
+ x.back();\r
+ for (;;) {\r
+ if (x.nextClean() == ',') {\r
+ x.back();\r
+ this.myArrayList.add(JSONObject.NULL);\r
+ } else {\r
+ x.back();\r
+ this.myArrayList.add(x.nextValue());\r
+ }\r
+ switch (x.nextClean()) {\r
+ case ';':\r
+ case ',':\r
+ if (x.nextClean() == ']') {\r
+ return;\r
+ }\r
+ x.back();\r
+ break;\r
+ case ']':\r
+ return;\r
+ default:\r
+ throw x.syntaxError("Expected a ',' or ']'");\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Construct a JSONArray from a source JSON text.\r
+ *\r
+ * @param source\r
+ * A string that begins with <code>[</code> <small>(left\r
+ * bracket)</small> and ends with <code>]</code>\r
+ * <small>(right bracket)</small>.\r
+ * @throws JSONException\r
+ * If there is a syntax error.\r
+ */\r
+ public JSONArray(String source) throws JSONException {\r
+ this(new JSONTokener(source));\r
+ }\r
+\r
+ /**\r
+ * Construct a JSONArray from a Collection.\r
+ *\r
+ * @param collection\r
+ * A Collection.\r
+ */\r
+ public JSONArray(Collection<Object> collection) {\r
+ this.myArrayList = new ArrayList<Object>();\r
+ if (collection != null) {\r
+ Iterator<Object> iter = collection.iterator();\r
+ while (iter.hasNext()) {\r
+ this.myArrayList.add(JSONObject.wrap(iter.next()));\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Construct a JSONArray from an array\r
+ *\r
+ * @throws JSONException\r
+ * If not an array.\r
+ */\r
+ public JSONArray(Object array) throws JSONException {\r
+ this();\r
+ if (array.getClass().isArray()) {\r
+ int length = Array.getLength(array);\r
+ for (int i = 0; i < length; i += 1) {\r
+ this.put(JSONObject.wrap(Array.get(array, i)));\r
+ }\r
+ } else {\r
+ throw new JSONException(\r
+ "JSONArray initial value should be a string or collection or array.");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the object value associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return An object value.\r
+ * @throws JSONException\r
+ * If there is no value for the index.\r
+ */\r
+ public Object get(int index) throws JSONException {\r
+ Object object = this.opt(index);\r
+ if (object == null) {\r
+ throw new JSONException("JSONArray[" + index + "] not found.");\r
+ }\r
+ return object;\r
+ }\r
+\r
+ /**\r
+ * Get the boolean value associated with an index. The string values "true"\r
+ * and "false" are converted to boolean.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The truth.\r
+ * @throws JSONException\r
+ * If there is no value for the index or if the value is not\r
+ * convertible to boolean.\r
+ */\r
+ public boolean getBoolean(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ if (object.equals(Boolean.FALSE)\r
+ || (object instanceof String && ((String) object)\r
+ .equalsIgnoreCase("false"))) {\r
+ return false;\r
+ } else if (object.equals(Boolean.TRUE)\r
+ || (object instanceof String && ((String) object)\r
+ .equalsIgnoreCase("true"))) {\r
+ return true;\r
+ }\r
+ throw new JSONException("JSONArray[" + index + "] is not a boolean.");\r
+ }\r
+\r
+ /**\r
+ * Get the double value associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The value.\r
+ * @throws JSONException\r
+ * If the key is not found or if the value cannot be converted\r
+ * to a number.\r
+ */\r
+ public double getDouble(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ try {\r
+ return object instanceof Number ? ((Number) object).doubleValue()\r
+ : Double.parseDouble((String) object);\r
+ } catch (Exception e) {\r
+ throw new JSONException("JSONArray[" + index + "] is not a number.");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the int value associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The value.\r
+ * @throws JSONException\r
+ * If the key is not found or if the value is not a number.\r
+ */\r
+ public int getInt(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ try {\r
+ return object instanceof Number ? ((Number) object).intValue()\r
+ : Integer.parseInt((String) object);\r
+ } catch (Exception e) {\r
+ throw new JSONException("JSONArray[" + index + "] is not a number.");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the JSONArray associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return A JSONArray value.\r
+ * @throws JSONException\r
+ * If there is no value for the index. or if the value is not a\r
+ * JSONArray\r
+ */\r
+ public JSONArray getJSONArray(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ if (object instanceof JSONArray) {\r
+ return (JSONArray) object;\r
+ }\r
+ throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");\r
+ }\r
+\r
+ /**\r
+ * Get the JSONObject associated with an index.\r
+ *\r
+ * @param index\r
+ * subscript\r
+ * @return A JSONObject value.\r
+ * @throws JSONException\r
+ * If there is no value for the index or if the value is not a\r
+ * JSONObject\r
+ */\r
+ public JSONObject getJSONObject(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ if (object instanceof JSONObject) {\r
+ return (JSONObject) object;\r
+ }\r
+ throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");\r
+ }\r
+\r
+ /**\r
+ * Get the long value associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The value.\r
+ * @throws JSONException\r
+ * If the key is not found or if the value cannot be converted\r
+ * to a number.\r
+ */\r
+ public long getLong(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ try {\r
+ return object instanceof Number ? ((Number) object).longValue()\r
+ : Long.parseLong((String) object);\r
+ } catch (Exception e) {\r
+ throw new JSONException("JSONArray[" + index + "] is not a number.");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the string associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return A string value.\r
+ * @throws JSONException\r
+ * If there is no string value for the index.\r
+ */\r
+ public String getString(int index) throws JSONException {\r
+ Object object = this.get(index);\r
+ if (object instanceof String) {\r
+ return (String) object;\r
+ }\r
+ throw new JSONException("JSONArray[" + index + "] not a string.");\r
+ }\r
+\r
+ /**\r
+ * Determine if the value is null.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return true if the value at the index is null, or if there is no value.\r
+ */\r
+ public boolean isNull(int index) {\r
+ return JSONObject.NULL.equals(this.opt(index));\r
+ }\r
+\r
+ /**\r
+ * Make a string from the contents of this JSONArray. The\r
+ * <code>separator</code> string is inserted between each element. Warning:\r
+ * This method assumes that the data structure is acyclical.\r
+ *\r
+ * @param separator\r
+ * A string that will be inserted between the elements.\r
+ * @return a string.\r
+ * @throws JSONException\r
+ * If the array contains an invalid number.\r
+ */\r
+ public String join(String separator) throws JSONException {\r
+ int len = this.length();\r
+ StringBuffer sb = new StringBuffer();\r
+\r
+ for (int i = 0; i < len; i += 1) {\r
+ if (i > 0) {\r
+ sb.append(separator);\r
+ }\r
+ sb.append(JSONObject.valueToString(this.myArrayList.get(i)));\r
+ }\r
+ return sb.toString();\r
+ }\r
+\r
+ /**\r
+ * Get the number of elements in the JSONArray, included nulls.\r
+ *\r
+ * @return The length (or size).\r
+ */\r
+ public int length() {\r
+ return this.myArrayList.size();\r
+ }\r
+\r
+ /**\r
+ * Get the optional object value associated with an index.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return An object value, or null if there is no object at that index.\r
+ */\r
+ public Object opt(int index) {\r
+ return (index < 0 || index >= this.length()) ? null : this.myArrayList\r
+ .get(index);\r
+ }\r
+\r
+ /**\r
+ * Get the optional boolean value associated with an index. It returns false\r
+ * if there is no value at that index, or if the value is not Boolean.TRUE\r
+ * or the String "true".\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The truth.\r
+ */\r
+ public boolean optBoolean(int index) {\r
+ return this.optBoolean(index, false);\r
+ }\r
+\r
+ /**\r
+ * Get the optional boolean value associated with an index. It returns the\r
+ * defaultValue if there is no value at that index or if it is not a Boolean\r
+ * or the String "true" or "false" (case insensitive).\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @param defaultValue\r
+ * A boolean default.\r
+ * @return The truth.\r
+ */\r
+ public boolean optBoolean(int index, boolean defaultValue) {\r
+ try {\r
+ return this.getBoolean(index);\r
+ } catch (Exception e) {\r
+ return defaultValue;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the optional double value associated with an index. NaN is returned\r
+ * if there is no value for the index, or if the value is not a number and\r
+ * cannot be converted to a number.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The value.\r
+ */\r
+ public double optDouble(int index) {\r
+ return this.optDouble(index, Double.NaN);\r
+ }\r
+\r
+ /**\r
+ * Get the optional double value associated with an index. The defaultValue\r
+ * is returned if there is no value for the index, or if the value is not a\r
+ * number and cannot be converted to a number.\r
+ *\r
+ * @param index\r
+ * subscript\r
+ * @param defaultValue\r
+ * The default value.\r
+ * @return The value.\r
+ */\r
+ public double optDouble(int index, double defaultValue) {\r
+ try {\r
+ return this.getDouble(index);\r
+ } catch (Exception e) {\r
+ return defaultValue;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the optional int value associated with an index. Zero is returned if\r
+ * there is no value for the index, or if the value is not a number and\r
+ * cannot be converted to a number.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The value.\r
+ */\r
+ public int optInt(int index) {\r
+ return this.optInt(index, 0);\r
+ }\r
+\r
+ /**\r
+ * Get the optional int value associated with an index. The defaultValue is\r
+ * returned if there is no value for the index, or if the value is not a\r
+ * number and cannot be converted to a number.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @param defaultValue\r
+ * The default value.\r
+ * @return The value.\r
+ */\r
+ public int optInt(int index, int defaultValue) {\r
+ try {\r
+ return this.getInt(index);\r
+ } catch (Exception e) {\r
+ return defaultValue;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the optional JSONArray associated with an index.\r
+ *\r
+ * @param index\r
+ * subscript\r
+ * @return A JSONArray value, or null if the index has no value, or if the\r
+ * value is not a JSONArray.\r
+ */\r
+ public JSONArray optJSONArray(int index) {\r
+ Object o = this.opt(index);\r
+ return o instanceof JSONArray ? (JSONArray) o : null;\r
+ }\r
+\r
+ /**\r
+ * Get the optional JSONObject associated with an index. Null is returned if\r
+ * the key is not found, or null if the index has no value, or if the value\r
+ * is not a JSONObject.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return A JSONObject value.\r
+ */\r
+ public JSONObject optJSONObject(int index) {\r
+ Object o = this.opt(index);\r
+ return o instanceof JSONObject ? (JSONObject) o : null;\r
+ }\r
+\r
+ /**\r
+ * Get the optional long value associated with an index. Zero is returned if\r
+ * there is no value for the index, or if the value is not a number and\r
+ * cannot be converted to a number.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return The value.\r
+ */\r
+ public long optLong(int index) {\r
+ return this.optLong(index, 0);\r
+ }\r
+\r
+ /**\r
+ * Get the optional long value associated with an index. The defaultValue is\r
+ * returned if there is no value for the index, or if the value is not a\r
+ * number and cannot be converted to a number.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @param defaultValue\r
+ * The default value.\r
+ * @return The value.\r
+ */\r
+ public long optLong(int index, long defaultValue) {\r
+ try {\r
+ return this.getLong(index);\r
+ } catch (Exception e) {\r
+ return defaultValue;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get the optional string value associated with an index. It returns an\r
+ * empty string if there is no value at that index. If the value is not a\r
+ * string and is not null, then it is coverted to a string.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @return A String value.\r
+ */\r
+ public String optString(int index) {\r
+ return this.optString(index, "");\r
+ }\r
+\r
+ /**\r
+ * Get the optional string associated with an index. The defaultValue is\r
+ * returned if the key is not found.\r
+ *\r
+ * @param index\r
+ * The index must be between 0 and length() - 1.\r
+ * @param defaultValue\r
+ * The default value.\r
+ * @return A String value.\r
+ */\r
+ public String optString(int index, String defaultValue) {\r
+ Object object = this.opt(index);\r
+ return JSONObject.NULL.equals(object) ? defaultValue : object\r
+ .toString();\r
+ }\r
+\r
+ /**\r
+ * Append a boolean value. This increases the array's length by one.\r
+ *\r
+ * @param value\r
+ * A boolean value.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(boolean value) {\r
+ this.put(value ? Boolean.TRUE : Boolean.FALSE);\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put a value in the JSONArray, where the value will be a JSONArray which\r
+ * is produced from a Collection.\r
+ *\r
+ * @param value\r
+ * A Collection value.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(Collection<Object> value) {\r
+ this.put(new JSONArray(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Append a double value. This increases the array's length by one.\r
+ *\r
+ * @param value\r
+ * A double value.\r
+ * @throws JSONException\r
+ * if the value is not finite.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(double value) throws JSONException {\r
+ Double d = new Double(value);\r
+ JSONObject.testValidity(d);\r
+ this.put(d);\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Append an int value. This increases the array's length by one.\r
+ *\r
+ * @param value\r
+ * An int value.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(int value) {\r
+ this.put(new Integer(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Append an long value. This increases the array's length by one.\r
+ *\r
+ * @param value\r
+ * A long value.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(long value) {\r
+ this.put(new Long(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put a value in the JSONArray, where the value will be a JSONObject which\r
+ * is produced from a Map.\r
+ *\r
+ * @param value\r
+ * A Map value.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(Map<String,Object> value) {\r
+ this.put(new JSONObject(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Append an object value. This increases the array's length by one.\r
+ *\r
+ * @param value\r
+ * An object value. The value should be a Boolean, Double,\r
+ * Integer, JSONArray, JSONObject, Long, or String, or the\r
+ * JSONObject.NULL object.\r
+ * @return this.\r
+ */\r
+ public JSONArray put(Object value) {\r
+ this.myArrayList.add(value);\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put or replace a boolean value in the JSONArray. If the index is greater\r
+ * than the length of the JSONArray, then null elements will be added as\r
+ * necessary to pad it out.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * A boolean value.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative.\r
+ */\r
+ public JSONArray put(int index, boolean value) throws JSONException {\r
+ this.put(index, value ? Boolean.TRUE : Boolean.FALSE);\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put a value in the JSONArray, where the value will be a JSONArray which\r
+ * is produced from a Collection.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * A Collection value.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative or if the value is not finite.\r
+ */\r
+ public JSONArray put(int index, Collection<Object> value) throws JSONException {\r
+ this.put(index, new JSONArray(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put or replace a double value. If the index is greater than the length of\r
+ * the JSONArray, then null elements will be added as necessary to pad it\r
+ * out.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * A double value.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative or if the value is not finite.\r
+ */\r
+ public JSONArray put(int index, double value) throws JSONException {\r
+ this.put(index, new Double(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put or replace an int value. If the index is greater than the length of\r
+ * the JSONArray, then null elements will be added as necessary to pad it\r
+ * out.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * An int value.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative.\r
+ */\r
+ public JSONArray put(int index, int value) throws JSONException {\r
+ this.put(index, new Integer(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put or replace a long value. If the index is greater than the length of\r
+ * the JSONArray, then null elements will be added as necessary to pad it\r
+ * out.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * A long value.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative.\r
+ */\r
+ public JSONArray put(int index, long value) throws JSONException {\r
+ this.put(index, new Long(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put a value in the JSONArray, where the value will be a JSONObject that\r
+ * is produced from a Map.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * The Map value.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative or if the the value is an invalid\r
+ * number.\r
+ */\r
+ public JSONArray put(int index, Map<String,Object> value) throws JSONException {\r
+ this.put(index, new JSONObject(value));\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Put or replace an object value in the JSONArray. If the index is greater\r
+ * than the length of the JSONArray, then null elements will be added as\r
+ * necessary to pad it out.\r
+ *\r
+ * @param index\r
+ * The subscript.\r
+ * @param value\r
+ * The value to put into the array. The value should be a\r
+ * Boolean, Double, Integer, JSONArray, JSONObject, Long, or\r
+ * String, or the JSONObject.NULL object.\r
+ * @return this.\r
+ * @throws JSONException\r
+ * If the index is negative or if the the value is an invalid\r
+ * number.\r
+ */\r
+ public JSONArray put(int index, Object value) throws JSONException {\r
+ JSONObject.testValidity(value);\r
+ if (index < 0) {\r
+ throw new JSONException("JSONArray[" + index + "] not found.");\r
+ }\r
+ if (index < this.length()) {\r
+ this.myArrayList.set(index, value);\r
+ } else {\r
+ while (index != this.length()) {\r
+ this.put(JSONObject.NULL);\r
+ }\r
+ this.put(value);\r
+ }\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Remove an index and close the hole.\r
+ *\r
+ * @param index\r
+ * The index of the element to be removed.\r
+ * @return The value that was associated with the index, or null if there\r
+ * was no value.\r
+ */\r
+ public Object remove(int index) {\r
+ Object o = this.opt(index);\r
+ this.myArrayList.remove(index);\r
+ return o;\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONObject by combining a JSONArray of names with the values of\r
+ * this JSONArray.\r
+ *\r
+ * @param names\r
+ * A JSONArray containing a list of key strings. These will be\r
+ * paired with the values.\r
+ * @return A JSONObject, or null if there are no names or if this JSONArray\r
+ * has no values.\r
+ * @throws JSONException\r
+ * If any of the names are null.\r
+ */\r
+ public JSONObject toJSONObject(JSONArray names) throws JSONException {\r
+ if (names == null || names.length() == 0 || this.length() == 0) {\r
+ return null;\r
+ }\r
+ JSONObject jo = new JSONObject();\r
+ for (int i = 0; i < names.length(); i += 1) {\r
+ jo.put(names.getString(i), this.opt(i));\r
+ }\r
+ return jo;\r
+ }\r
+\r
+ /**\r
+ * Make a JSON text of this JSONArray. For compactness, no unnecessary\r
+ * whitespace is added. If it is not possible to produce a syntactically\r
+ * correct JSON text then null will be returned instead. This could occur if\r
+ * the array contains an invalid number.\r
+ * <p>\r
+ * Warning: This method assumes that the data structure is acyclical.\r
+ *\r
+ * @return a printable, displayable, transmittable representation of the\r
+ * array.\r
+ */\r
+ public String toString() {\r
+ try {\r
+ return this.toString(0);\r
+ } catch (Exception e) {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Make a prettyprinted JSON text of this JSONArray. Warning: This method\r
+ * assumes that the data structure is acyclical.\r
+ *\r
+ * @param indentFactor\r
+ * The number of spaces to add to each level of indentation.\r
+ * @return a printable, displayable, transmittable representation of the\r
+ * object, beginning with <code>[</code> <small>(left\r
+ * bracket)</small> and ending with <code>]</code>\r
+ * <small>(right bracket)</small>.\r
+ * @throws JSONException\r
+ */\r
+ public String toString(int indentFactor) throws JSONException {\r
+ StringWriter sw = new StringWriter();\r
+ synchronized (sw.getBuffer()) {\r
+ return this.write(sw, indentFactor, 0).toString();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Write the contents of the JSONArray as JSON text to a writer. For\r
+ * compactness, no whitespace is added.\r
+ * <p>\r
+ * Warning: This method assumes that the data structure is acyclical.\r
+ *\r
+ * @return The writer.\r
+ * @throws JSONException\r
+ */\r
+ public Writer write(Writer writer) throws JSONException {\r
+ return this.write(writer, 0, 0);\r
+ }\r
+\r
+ /**\r
+ * Write the contents of the JSONArray as JSON text to a writer. For\r
+ * compactness, no whitespace is added.\r
+ * <p>\r
+ * Warning: This method assumes that the data structure is acyclical.\r
+ *\r
+ * @param indentFactor\r
+ * The number of spaces to add to each level of indentation.\r
+ * @param indent\r
+ * The indention of the top level.\r
+ * @return The writer.\r
+ * @throws JSONException\r
+ */\r
+ Writer write(Writer writer, int indentFactor, int indent)\r
+ throws JSONException {\r
+ try {\r
+ boolean commanate = false;\r
+ int length = this.length();\r
+ writer.write('[');\r
+\r
+ if (length == 1) {\r
+ JSONObject.writeValue(writer, this.myArrayList.get(0),\r
+ indentFactor, indent);\r
+ } else if (length != 0) {\r
+ final int newindent = indent + indentFactor;\r
+\r
+ for (int i = 0; i < length; i += 1) {\r
+ if (commanate) {\r
+ writer.write(',');\r
+ }\r
+ if (indentFactor > 0) {\r
+ writer.write('\n');\r
+ }\r
+ JSONObject.indent(writer, newindent);\r
+ JSONObject.writeValue(writer, this.myArrayList.get(i),\r
+ indentFactor, newindent);\r
+ commanate = true;\r
+ }\r
+ if (indentFactor > 0) {\r
+ writer.write('\n');\r
+ }\r
+ JSONObject.indent(writer, indent);\r
+ }\r
+ writer.write(']');\r
+ return writer;\r
+ } catch (IOException e) {\r
+ throw new JSONException(e);\r
+ }\r
+ }\r
+}\r