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