Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 18:06:40 GMT
  2. Package org.json.simple

File JSONArray.java

 

Coverage histogram

../../../img/srcFileCovDistChart2.png
57% of files have more coverage

Code metrics

62
161
25
1
498
386
66
0.41
6.44
25
2.64

Classes

Class Line # Actions
JSONArray 19 161 66
0.1209677412.1%
 

Contributing tests

This file is covered by 93 tests. .

Source view

1    /*
2    * $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $
3    * Created on 2006-4-10
4    */
5    package org.json.simple;
6   
7    import java.io.IOException;
8    import java.io.StringWriter;
9    import java.io.Writer;
10    import java.util.ArrayList;
11    import java.util.Collection;
12    import java.util.Iterator;
13   
14    /**
15    * A JSON array. JSONObject supports java.util.List interface.
16    *
17    * @author FangYidong<fangyidong@yahoo.com.cn>
18    */
 
19    public class JSONArray extends ArrayList
20    implements JSONAware, JSONStreamAware
21    {
22    private static final long serialVersionUID = 3957988303675231981L;
23   
24    /**
25    * Constructs an empty JSONArray.
26    */
 
27  41284 toggle public JSONArray()
28    {
29  41284 super();
30    }
31   
32    /**
33    * Constructs a JSONArray containing the elements of the specified collection,
34    * in the order they are returned by the collection's iterator.
35    *
36    * @param c
37    * the collection whose elements are to be placed into this JSONArray
38    */
 
39  0 toggle public JSONArray(Collection c)
40    {
41  0 super(c);
42    }
43   
44    /**
45    * Encode a list into JSON text and write it to out. If this list is also a
46    * JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific
47    * behaviours will be ignored at this top level.
48    *
49    * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
50    *
51    * @param collection
52    * @param out
53    */
 
54  8246 toggle public static void writeJSONString(Collection collection, Writer out)
55    throws IOException
56    {
57  8246 if (collection == null)
58    {
59  0 out.write("null");
60  0 return;
61    }
62   
63  8246 boolean first = true;
64  8246 Iterator iter = collection.iterator();
65   
66  8246 out.write('[');
67  16534 while (iter.hasNext())
68    {
69  8288 if (first)
70  8246 first = false;
71    else
72  42 out.write(',');
73   
74  8288 Object value = iter.next();
75  8288 if (value == null)
76    {
77  0 out.write("null");
78  0 continue;
79    }
80   
81  8288 JSONValue.writeJSONString(value, out);
82    }
83  8246 out.write(']');
84    }
85   
 
86  0 toggle public void writeJSONString(Writer out) throws IOException
87    {
88  0 writeJSONString(this, out);
89    }
90   
91    /**
92    * Convert a list to JSON text. The result is a JSON array. If this list is
93    * also a JSONAware, JSONAware specific behaviours will be omitted at this top
94    * level.
95    *
96    * @see org.json.simple.JSONValue#toJSONString(Object)
97    *
98    * @param collection
99    * @return JSON text, or "null" if list is null.
100    */
 
101  8246 toggle public static String toJSONString(Collection collection)
102    {
103  8246 final StringWriter writer = new StringWriter();
104   
105  8246 try
106    {
107  8246 writeJSONString(collection, writer);
108  8246 return writer.toString();
109    } catch (IOException e)
110    {
111    // This should never happen for a StringWriter
112  0 throw new RuntimeException(e);
113    }
114    }
115   
 
116  0 toggle public static void writeJSONString(byte[] array, Writer out)
117    throws IOException
118    {
119  0 if (array == null)
120    {
121  0 out.write("null");
122    }
123  0 else if (array.length == 0)
124    {
125  0 out.write("[]");
126    }
127    else
128    {
129  0 out.write("[");
130  0 out.write(String.valueOf(array[0]));
131   
132  0 for (int i = 1; i < array.length; i++)
133    {
134  0 out.write(",");
135  0 out.write(String.valueOf(array[i]));
136    }
137   
138  0 out.write("]");
139    }
140    }
141   
 
142  0 toggle public static String toJSONString(byte[] array)
143    {
144  0 final StringWriter writer = new StringWriter();
145   
146  0 try
147    {
148  0 writeJSONString(array, writer);
149  0 return writer.toString();
150    } catch (IOException e)
151    {
152    // This should never happen for a StringWriter
153  0 throw new RuntimeException(e);
154    }
155    }
156   
 
157  0 toggle public static void writeJSONString(short[] array, Writer out)
158    throws IOException
159    {
160  0 if (array == null)
161    {
162  0 out.write("null");
163    }
164  0 else if (array.length == 0)
165    {
166  0 out.write("[]");
167    }
168    else
169    {
170  0 out.write("[");
171  0 out.write(String.valueOf(array[0]));
172   
173  0 for (int i = 1; i < array.length; i++)
174    {
175  0 out.write(",");
176  0 out.write(String.valueOf(array[i]));
177    }
178   
179  0 out.write("]");
180    }
181    }
182   
 
183  0 toggle public static String toJSONString(short[] array)
184    {
185  0 final StringWriter writer = new StringWriter();
186   
187  0 try
188    {
189  0 writeJSONString(array, writer);
190  0 return writer.toString();
191    } catch (IOException e)
192    {
193    // This should never happen for a StringWriter
194  0 throw new RuntimeException(e);
195    }
196    }
197   
 
198  0 toggle public static void writeJSONString(int[] array, Writer out)
199    throws IOException
200    {
201  0 if (array == null)
202    {
203  0 out.write("null");
204    }
205  0 else if (array.length == 0)
206    {
207  0 out.write("[]");
208    }
209    else
210    {
211  0 out.write("[");
212  0 out.write(String.valueOf(array[0]));
213   
214  0 for (int i = 1; i < array.length; i++)
215    {
216  0 out.write(",");
217  0 out.write(String.valueOf(array[i]));
218    }
219   
220  0 out.write("]");
221    }
222    }
223   
 
224  0 toggle public static String toJSONString(int[] array)
225    {
226  0 final StringWriter writer = new StringWriter();
227   
228  0 try
229    {
230  0 writeJSONString(array, writer);
231  0 return writer.toString();
232    } catch (IOException e)
233    {
234    // This should never happen for a StringWriter
235  0 throw new RuntimeException(e);
236    }
237    }
238   
 
239  0 toggle public static void writeJSONString(long[] array, Writer out)
240    throws IOException
241    {
242  0 if (array == null)
243    {
244  0 out.write("null");
245    }
246  0 else if (array.length == 0)
247    {
248  0 out.write("[]");
249    }
250    else
251    {
252  0 out.write("[");
253  0 out.write(String.valueOf(array[0]));
254   
255  0 for (int i = 1; i < array.length; i++)
256    {
257  0 out.write(",");
258  0 out.write(String.valueOf(array[i]));
259    }
260   
261  0 out.write("]");
262    }
263    }
264   
 
265  0 toggle public static String toJSONString(long[] array)
266    {
267  0 final StringWriter writer = new StringWriter();
268   
269  0 try
270    {
271  0 writeJSONString(array, writer);
272  0 return writer.toString();
273    } catch (IOException e)
274    {
275    // This should never happen for a StringWriter
276  0 throw new RuntimeException(e);
277    }
278    }
279   
 
280  0 toggle public static void writeJSONString(float[] array, Writer out)
281    throws IOException
282    {
283  0 if (array == null)
284    {
285  0 out.write("null");
286    }
287  0 else if (array.length == 0)
288    {
289  0 out.write("[]");
290    }
291    else
292    {
293  0 out.write("[");
294  0 out.write(String.valueOf(array[0]));
295   
296  0 for (int i = 1; i < array.length; i++)
297    {
298  0 out.write(",");
299  0 out.write(String.valueOf(array[i]));
300    }
301   
302  0 out.write("]");
303    }
304    }
305   
 
306  0 toggle public static String toJSONString(float[] array)
307    {
308  0 final StringWriter writer = new StringWriter();
309   
310  0 try
311    {
312  0 writeJSONString(array, writer);
313  0 return writer.toString();
314    } catch (IOException e)
315    {
316    // This should never happen for a StringWriter
317  0 throw new RuntimeException(e);
318    }
319    }
320   
 
321  0 toggle public static void writeJSONString(double[] array, Writer out)
322    throws IOException
323    {
324  0 if (array == null)
325    {
326  0 out.write("null");
327    }
328  0 else if (array.length == 0)
329    {
330  0 out.write("[]");
331    }
332    else
333    {
334  0 out.write("[");
335  0 out.write(String.valueOf(array[0]));
336   
337  0 for (int i = 1; i < array.length; i++)
338    {
339  0 out.write(",");
340  0 out.write(String.valueOf(array[i]));
341    }
342   
343  0 out.write("]");
344    }
345    }
346   
 
347  0 toggle public static String toJSONString(double[] array)
348    {
349  0 final StringWriter writer = new StringWriter();
350   
351  0 try
352    {
353  0 writeJSONString(array, writer);
354  0 return writer.toString();
355    } catch (IOException e)
356    {
357    // This should never happen for a StringWriter
358  0 throw new RuntimeException(e);
359    }
360    }
361   
 
362  0 toggle public static void writeJSONString(boolean[] array, Writer out)
363    throws IOException
364    {
365  0 if (array == null)
366    {
367  0 out.write("null");
368    }
369  0 else if (array.length == 0)
370    {
371  0 out.write("[]");
372    }
373    else
374    {
375  0 out.write("[");
376  0 out.write(String.valueOf(array[0]));
377   
378  0 for (int i = 1; i < array.length; i++)
379    {
380  0 out.write(",");
381  0 out.write(String.valueOf(array[i]));
382    }
383   
384  0 out.write("]");
385    }
386    }
387   
 
388  0 toggle public static String toJSONString(boolean[] array)
389    {
390  0 final StringWriter writer = new StringWriter();
391   
392  0 try
393    {
394  0 writeJSONString(array, writer);
395  0 return writer.toString();
396    } catch (IOException e)
397    {
398    // This should never happen for a StringWriter
399  0 throw new RuntimeException(e);
400    }
401    }
402   
 
403  0 toggle public static void writeJSONString(char[] array, Writer out)
404    throws IOException
405    {
406  0 if (array == null)
407    {
408  0 out.write("null");
409    }
410  0 else if (array.length == 0)
411    {
412  0 out.write("[]");
413    }
414    else
415    {
416  0 out.write("[\"");
417  0 out.write(String.valueOf(array[0]));
418   
419  0 for (int i = 1; i < array.length; i++)
420    {
421  0 out.write("\",\"");
422  0 out.write(String.valueOf(array[i]));
423    }
424   
425  0 out.write("\"]");
426    }
427    }
428   
 
429  0 toggle public static String toJSONString(char[] array)
430    {
431  0 final StringWriter writer = new StringWriter();
432   
433  0 try
434    {
435  0 writeJSONString(array, writer);
436  0 return writer.toString();
437    } catch (IOException e)
438    {
439    // This should never happen for a StringWriter
440  0 throw new RuntimeException(e);
441    }
442    }
443   
 
444  0 toggle public static void writeJSONString(Object[] array, Writer out)
445    throws IOException
446    {
447  0 if (array == null)
448    {
449  0 out.write("null");
450    }
451  0 else if (array.length == 0)
452    {
453  0 out.write("[]");
454    }
455    else
456    {
457  0 out.write("[");
458  0 JSONValue.writeJSONString(array[0], out);
459   
460  0 for (int i = 1; i < array.length; i++)
461    {
462  0 out.write(",");
463  0 JSONValue.writeJSONString(array[i], out);
464    }
465   
466  0 out.write("]");
467    }
468    }
469   
 
470  0 toggle public static String toJSONString(Object[] array)
471    {
472  0 final StringWriter writer = new StringWriter();
473   
474  0 try
475    {
476  0 writeJSONString(array, writer);
477  0 return writer.toString();
478    } catch (IOException e)
479    {
480    // This should never happen for a StringWriter
481  0 throw new RuntimeException(e);
482    }
483    }
484   
 
485  8246 toggle public String toJSONString()
486    {
487  8246 return toJSONString(this);
488    }
489   
490    /**
491    * Returns a string representation of this array. This is equivalent to
492    * calling {@link JSONArray#toJSONString()}.
493    */
 
494  8246 toggle public String toString()
495    {
496  8246 return toJSONString();
497    }
498    }