JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / org / json / simple / JSONArray.java
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   public JSONArray()
28   {
29     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   public JSONArray(Collection c)
40   {
41     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   public static void writeJSONString(Collection collection, Writer out)
55           throws IOException
56   {
57     if (collection == null)
58     {
59       out.write("null");
60       return;
61     }
62
63     boolean first = true;
64     Iterator iter = collection.iterator();
65
66     out.write('[');
67     while (iter.hasNext())
68     {
69       if (first)
70         first = false;
71       else
72         out.write(',');
73
74       Object value = iter.next();
75       if (value == null)
76       {
77         out.write("null");
78         continue;
79       }
80
81       JSONValue.writeJSONString(value, out);
82     }
83     out.write(']');
84   }
85
86   public void writeJSONString(Writer out) throws IOException
87   {
88     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   public static String toJSONString(Collection collection)
102   {
103     final StringWriter writer = new StringWriter();
104
105     try
106     {
107       writeJSONString(collection, writer);
108       return writer.toString();
109     } catch (IOException e)
110     {
111       // This should never happen for a StringWriter
112       throw new RuntimeException(e);
113     }
114   }
115
116   public static void writeJSONString(byte[] array, Writer out)
117           throws IOException
118   {
119     if (array == null)
120     {
121       out.write("null");
122     }
123     else if (array.length == 0)
124     {
125       out.write("[]");
126     }
127     else
128     {
129       out.write("[");
130       out.write(String.valueOf(array[0]));
131
132       for (int i = 1; i < array.length; i++)
133       {
134         out.write(",");
135         out.write(String.valueOf(array[i]));
136       }
137
138       out.write("]");
139     }
140   }
141
142   public static String toJSONString(byte[] array)
143   {
144     final StringWriter writer = new StringWriter();
145
146     try
147     {
148       writeJSONString(array, writer);
149       return writer.toString();
150     } catch (IOException e)
151     {
152       // This should never happen for a StringWriter
153       throw new RuntimeException(e);
154     }
155   }
156
157   public static void writeJSONString(short[] array, Writer out)
158           throws IOException
159   {
160     if (array == null)
161     {
162       out.write("null");
163     }
164     else if (array.length == 0)
165     {
166       out.write("[]");
167     }
168     else
169     {
170       out.write("[");
171       out.write(String.valueOf(array[0]));
172
173       for (int i = 1; i < array.length; i++)
174       {
175         out.write(",");
176         out.write(String.valueOf(array[i]));
177       }
178
179       out.write("]");
180     }
181   }
182
183   public static String toJSONString(short[] array)
184   {
185     final StringWriter writer = new StringWriter();
186
187     try
188     {
189       writeJSONString(array, writer);
190       return writer.toString();
191     } catch (IOException e)
192     {
193       // This should never happen for a StringWriter
194       throw new RuntimeException(e);
195     }
196   }
197
198   public static void writeJSONString(int[] array, Writer out)
199           throws IOException
200   {
201     if (array == null)
202     {
203       out.write("null");
204     }
205     else if (array.length == 0)
206     {
207       out.write("[]");
208     }
209     else
210     {
211       out.write("[");
212       out.write(String.valueOf(array[0]));
213
214       for (int i = 1; i < array.length; i++)
215       {
216         out.write(",");
217         out.write(String.valueOf(array[i]));
218       }
219
220       out.write("]");
221     }
222   }
223
224   public static String toJSONString(int[] array)
225   {
226     final StringWriter writer = new StringWriter();
227
228     try
229     {
230       writeJSONString(array, writer);
231       return writer.toString();
232     } catch (IOException e)
233     {
234       // This should never happen for a StringWriter
235       throw new RuntimeException(e);
236     }
237   }
238
239   public static void writeJSONString(long[] array, Writer out)
240           throws IOException
241   {
242     if (array == null)
243     {
244       out.write("null");
245     }
246     else if (array.length == 0)
247     {
248       out.write("[]");
249     }
250     else
251     {
252       out.write("[");
253       out.write(String.valueOf(array[0]));
254
255       for (int i = 1; i < array.length; i++)
256       {
257         out.write(",");
258         out.write(String.valueOf(array[i]));
259       }
260
261       out.write("]");
262     }
263   }
264
265   public static String toJSONString(long[] array)
266   {
267     final StringWriter writer = new StringWriter();
268
269     try
270     {
271       writeJSONString(array, writer);
272       return writer.toString();
273     } catch (IOException e)
274     {
275       // This should never happen for a StringWriter
276       throw new RuntimeException(e);
277     }
278   }
279
280   public static void writeJSONString(float[] array, Writer out)
281           throws IOException
282   {
283     if (array == null)
284     {
285       out.write("null");
286     }
287     else if (array.length == 0)
288     {
289       out.write("[]");
290     }
291     else
292     {
293       out.write("[");
294       out.write(String.valueOf(array[0]));
295
296       for (int i = 1; i < array.length; i++)
297       {
298         out.write(",");
299         out.write(String.valueOf(array[i]));
300       }
301
302       out.write("]");
303     }
304   }
305
306   public static String toJSONString(float[] array)
307   {
308     final StringWriter writer = new StringWriter();
309
310     try
311     {
312       writeJSONString(array, writer);
313       return writer.toString();
314     } catch (IOException e)
315     {
316       // This should never happen for a StringWriter
317       throw new RuntimeException(e);
318     }
319   }
320
321   public static void writeJSONString(double[] array, Writer out)
322           throws IOException
323   {
324     if (array == null)
325     {
326       out.write("null");
327     }
328     else if (array.length == 0)
329     {
330       out.write("[]");
331     }
332     else
333     {
334       out.write("[");
335       out.write(String.valueOf(array[0]));
336
337       for (int i = 1; i < array.length; i++)
338       {
339         out.write(",");
340         out.write(String.valueOf(array[i]));
341       }
342
343       out.write("]");
344     }
345   }
346
347   public static String toJSONString(double[] array)
348   {
349     final StringWriter writer = new StringWriter();
350
351     try
352     {
353       writeJSONString(array, writer);
354       return writer.toString();
355     } catch (IOException e)
356     {
357       // This should never happen for a StringWriter
358       throw new RuntimeException(e);
359     }
360   }
361
362   public static void writeJSONString(boolean[] array, Writer out)
363           throws IOException
364   {
365     if (array == null)
366     {
367       out.write("null");
368     }
369     else if (array.length == 0)
370     {
371       out.write("[]");
372     }
373     else
374     {
375       out.write("[");
376       out.write(String.valueOf(array[0]));
377
378       for (int i = 1; i < array.length; i++)
379       {
380         out.write(",");
381         out.write(String.valueOf(array[i]));
382       }
383
384       out.write("]");
385     }
386   }
387
388   public static String toJSONString(boolean[] array)
389   {
390     final StringWriter writer = new StringWriter();
391
392     try
393     {
394       writeJSONString(array, writer);
395       return writer.toString();
396     } catch (IOException e)
397     {
398       // This should never happen for a StringWriter
399       throw new RuntimeException(e);
400     }
401   }
402
403   public static void writeJSONString(char[] array, Writer out)
404           throws IOException
405   {
406     if (array == null)
407     {
408       out.write("null");
409     }
410     else if (array.length == 0)
411     {
412       out.write("[]");
413     }
414     else
415     {
416       out.write("[\"");
417       out.write(String.valueOf(array[0]));
418
419       for (int i = 1; i < array.length; i++)
420       {
421         out.write("\",\"");
422         out.write(String.valueOf(array[i]));
423       }
424
425       out.write("\"]");
426     }
427   }
428
429   public static String toJSONString(char[] array)
430   {
431     final StringWriter writer = new StringWriter();
432
433     try
434     {
435       writeJSONString(array, writer);
436       return writer.toString();
437     } catch (IOException e)
438     {
439       // This should never happen for a StringWriter
440       throw new RuntimeException(e);
441     }
442   }
443
444   public static void writeJSONString(Object[] array, Writer out)
445           throws IOException
446   {
447     if (array == null)
448     {
449       out.write("null");
450     }
451     else if (array.length == 0)
452     {
453       out.write("[]");
454     }
455     else
456     {
457       out.write("[");
458       JSONValue.writeJSONString(array[0], out);
459
460       for (int i = 1; i < array.length; i++)
461       {
462         out.write(",");
463         JSONValue.writeJSONString(array[i], out);
464       }
465
466       out.write("]");
467     }
468   }
469
470   public static String toJSONString(Object[] array)
471   {
472     final StringWriter writer = new StringWriter();
473
474     try
475     {
476       writeJSONString(array, writer);
477       return writer.toString();
478     } catch (IOException e)
479     {
480       // This should never happen for a StringWriter
481       throw new RuntimeException(e);
482     }
483   }
484
485   public String toJSONString()
486   {
487     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   public String toString()
495   {
496     return toJSONString();
497   }
498 }