Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package org.json.simple

File JSONArray.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

62
161
25
1
381
272
66
0.41
6.44
25
2.64

Classes

Class Line # Actions
JSONArray 19 161 66
0.0080645160.8%
 

Contributing tests

This file is covered by 21 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 implements JSONAware, JSONStreamAware {
20    private static final long serialVersionUID = 3957988303675231981L;
21   
22    /**
23    * Constructs an empty JSONArray.
24    */
 
25  88 toggle public JSONArray(){
26  88 super();
27    }
28   
29    /**
30    * Constructs a JSONArray containing the elements of the specified
31    * collection, in the order they are returned by the collection's iterator.
32    *
33    * @param c the collection whose elements are to be placed into this JSONArray
34    */
 
35  0 toggle public JSONArray(Collection c){
36  0 super(c);
37    }
38   
39    /**
40    * Encode a list into JSON text and write it to out.
41    * If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
42    *
43    * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
44    *
45    * @param collection
46    * @param out
47    */
 
48  0 toggle public static void writeJSONString(Collection collection, Writer out) throws IOException{
49  0 if(collection == null){
50  0 out.write("null");
51  0 return;
52    }
53   
54  0 boolean first = true;
55  0 Iterator iter=collection.iterator();
56   
57  0 out.write('[');
58  0 while(iter.hasNext()){
59  0 if(first)
60  0 first = false;
61    else
62  0 out.write(',');
63   
64  0 Object value=iter.next();
65  0 if(value == null){
66  0 out.write("null");
67  0 continue;
68    }
69   
70  0 JSONValue.writeJSONString(value, out);
71    }
72  0 out.write(']');
73    }
74   
 
75  0 toggle public void writeJSONString(Writer out) throws IOException{
76  0 writeJSONString(this, out);
77    }
78   
79    /**
80    * Convert a list to JSON text. The result is a JSON array.
81    * If this list is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
82    *
83    * @see org.json.simple.JSONValue#toJSONString(Object)
84    *
85    * @param collection
86    * @return JSON text, or "null" if list is null.
87    */
 
88  0 toggle public static String toJSONString(Collection collection){
89  0 final StringWriter writer = new StringWriter();
90   
91  0 try {
92  0 writeJSONString(collection, writer);
93  0 return writer.toString();
94    } catch(IOException e){
95    // This should never happen for a StringWriter
96  0 throw new RuntimeException(e);
97    }
98    }
99   
 
100  0 toggle public static void writeJSONString(byte[] array, Writer out) throws IOException{
101  0 if(array == null){
102  0 out.write("null");
103  0 } else if(array.length == 0) {
104  0 out.write("[]");
105    } else {
106  0 out.write("[");
107  0 out.write(String.valueOf(array[0]));
108   
109  0 for(int i = 1; i < array.length; i++){
110  0 out.write(",");
111  0 out.write(String.valueOf(array[i]));
112    }
113   
114  0 out.write("]");
115    }
116    }
117   
 
118  0 toggle public static String toJSONString(byte[] array){
119  0 final StringWriter writer = new StringWriter();
120   
121  0 try {
122  0 writeJSONString(array, writer);
123  0 return writer.toString();
124    } catch(IOException e){
125    // This should never happen for a StringWriter
126  0 throw new RuntimeException(e);
127    }
128    }
129   
 
130  0 toggle public static void writeJSONString(short[] array, Writer out) throws IOException{
131  0 if(array == null){
132  0 out.write("null");
133  0 } else if(array.length == 0) {
134  0 out.write("[]");
135    } else {
136  0 out.write("[");
137  0 out.write(String.valueOf(array[0]));
138   
139  0 for(int i = 1; i < array.length; i++){
140  0 out.write(",");
141  0 out.write(String.valueOf(array[i]));
142    }
143   
144  0 out.write("]");
145    }
146    }
147   
 
148  0 toggle public static String toJSONString(short[] array){
149  0 final StringWriter writer = new StringWriter();
150   
151  0 try {
152  0 writeJSONString(array, writer);
153  0 return writer.toString();
154    } catch(IOException e){
155    // This should never happen for a StringWriter
156  0 throw new RuntimeException(e);
157    }
158    }
159   
 
160  0 toggle public static void writeJSONString(int[] array, Writer out) throws IOException{
161  0 if(array == null){
162  0 out.write("null");
163  0 } else if(array.length == 0) {
164  0 out.write("[]");
165    } else {
166  0 out.write("[");
167  0 out.write(String.valueOf(array[0]));
168   
169  0 for(int i = 1; i < array.length; i++){
170  0 out.write(",");
171  0 out.write(String.valueOf(array[i]));
172    }
173   
174  0 out.write("]");
175    }
176    }
177   
 
178  0 toggle public static String toJSONString(int[] array){
179  0 final StringWriter writer = new StringWriter();
180   
181  0 try {
182  0 writeJSONString(array, writer);
183  0 return writer.toString();
184    } catch(IOException e){
185    // This should never happen for a StringWriter
186  0 throw new RuntimeException(e);
187    }
188    }
189   
 
190  0 toggle public static void writeJSONString(long[] array, Writer out) throws IOException{
191  0 if(array == null){
192  0 out.write("null");
193  0 } else if(array.length == 0) {
194  0 out.write("[]");
195    } else {
196  0 out.write("[");
197  0 out.write(String.valueOf(array[0]));
198   
199  0 for(int i = 1; i < array.length; i++){
200  0 out.write(",");
201  0 out.write(String.valueOf(array[i]));
202    }
203   
204  0 out.write("]");
205    }
206    }
207   
 
208  0 toggle public static String toJSONString(long[] array){
209  0 final StringWriter writer = new StringWriter();
210   
211  0 try {
212  0 writeJSONString(array, writer);
213  0 return writer.toString();
214    } catch(IOException e){
215    // This should never happen for a StringWriter
216  0 throw new RuntimeException(e);
217    }
218    }
219   
 
220  0 toggle public static void writeJSONString(float[] array, Writer out) throws IOException{
221  0 if(array == null){
222  0 out.write("null");
223  0 } else if(array.length == 0) {
224  0 out.write("[]");
225    } else {
226  0 out.write("[");
227  0 out.write(String.valueOf(array[0]));
228   
229  0 for(int i = 1; i < array.length; i++){
230  0 out.write(",");
231  0 out.write(String.valueOf(array[i]));
232    }
233   
234  0 out.write("]");
235    }
236    }
237   
 
238  0 toggle public static String toJSONString(float[] array){
239  0 final StringWriter writer = new StringWriter();
240   
241  0 try {
242  0 writeJSONString(array, writer);
243  0 return writer.toString();
244    } catch(IOException e){
245    // This should never happen for a StringWriter
246  0 throw new RuntimeException(e);
247    }
248    }
249   
 
250  0 toggle public static void writeJSONString(double[] array, Writer out) throws IOException{
251  0 if(array == null){
252  0 out.write("null");
253  0 } else if(array.length == 0) {
254  0 out.write("[]");
255    } else {
256  0 out.write("[");
257  0 out.write(String.valueOf(array[0]));
258   
259  0 for(int i = 1; i < array.length; i++){
260  0 out.write(",");
261  0 out.write(String.valueOf(array[i]));
262    }
263   
264  0 out.write("]");
265    }
266    }
267   
 
268  0 toggle public static String toJSONString(double[] array){
269  0 final StringWriter writer = new StringWriter();
270   
271  0 try {
272  0 writeJSONString(array, writer);
273  0 return writer.toString();
274    } catch(IOException e){
275    // This should never happen for a StringWriter
276  0 throw new RuntimeException(e);
277    }
278    }
279   
 
280  0 toggle public static void writeJSONString(boolean[] array, Writer out) throws IOException{
281  0 if(array == null){
282  0 out.write("null");
283  0 } else if(array.length == 0) {
284  0 out.write("[]");
285    } else {
286  0 out.write("[");
287  0 out.write(String.valueOf(array[0]));
288   
289  0 for(int i = 1; i < array.length; i++){
290  0 out.write(",");
291  0 out.write(String.valueOf(array[i]));
292    }
293   
294  0 out.write("]");
295    }
296    }
297   
 
298  0 toggle public static String toJSONString(boolean[] array){
299  0 final StringWriter writer = new StringWriter();
300   
301  0 try {
302  0 writeJSONString(array, writer);
303  0 return writer.toString();
304    } catch(IOException e){
305    // This should never happen for a StringWriter
306  0 throw new RuntimeException(e);
307    }
308    }
309   
 
310  0 toggle public static void writeJSONString(char[] array, Writer out) throws IOException{
311  0 if(array == null){
312  0 out.write("null");
313  0 } else if(array.length == 0) {
314  0 out.write("[]");
315    } else {
316  0 out.write("[\"");
317  0 out.write(String.valueOf(array[0]));
318   
319  0 for(int i = 1; i < array.length; i++){
320  0 out.write("\",\"");
321  0 out.write(String.valueOf(array[i]));
322    }
323   
324  0 out.write("\"]");
325    }
326    }
327   
 
328  0 toggle public static String toJSONString(char[] array){
329  0 final StringWriter writer = new StringWriter();
330   
331  0 try {
332  0 writeJSONString(array, writer);
333  0 return writer.toString();
334    } catch(IOException e){
335    // This should never happen for a StringWriter
336  0 throw new RuntimeException(e);
337    }
338    }
339   
 
340  0 toggle public static void writeJSONString(Object[] array, Writer out) throws IOException{
341  0 if(array == null){
342  0 out.write("null");
343  0 } else if(array.length == 0) {
344  0 out.write("[]");
345    } else {
346  0 out.write("[");
347  0 JSONValue.writeJSONString(array[0], out);
348   
349  0 for(int i = 1; i < array.length; i++){
350  0 out.write(",");
351  0 JSONValue.writeJSONString(array[i], out);
352    }
353   
354  0 out.write("]");
355    }
356    }
357   
 
358  0 toggle public static String toJSONString(Object[] array){
359  0 final StringWriter writer = new StringWriter();
360   
361  0 try {
362  0 writeJSONString(array, writer);
363  0 return writer.toString();
364    } catch(IOException e){
365    // This should never happen for a StringWriter
366  0 throw new RuntimeException(e);
367    }
368    }
369   
 
370  0 toggle public String toJSONString(){
371  0 return toJSONString(this);
372    }
373   
374    /**
375    * Returns a string representation of this array. This is equivalent to
376    * calling {@link JSONArray#toJSONString()}.
377    */
 
378  0 toggle public String toString() {
379  0 return toJSONString();
380    }
381    }