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