2 * $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $
5 package org.json.simple;
7 import java.io.IOException;
8 import java.io.StringWriter;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
15 * A JSON array. JSONObject supports java.util.List interface.
17 * @author FangYidong<fangyidong@yahoo.com.cn>
19 public class JSONArray extends ArrayList
20 implements JSONAware, JSONStreamAware
22 private static final long serialVersionUID = 3957988303675231981L;
25 * Constructs an empty JSONArray.
33 * Constructs a JSONArray containing the elements of the specified collection,
34 * in the order they are returned by the collection's iterator.
37 * the collection whose elements are to be placed into this JSONArray
39 public JSONArray(Collection c)
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.
49 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
54 public static void writeJSONString(Collection collection, Writer out)
57 if (collection == null)
64 Iterator iter = collection.iterator();
67 while (iter.hasNext())
74 Object value = iter.next();
81 JSONValue.writeJSONString(value, out);
86 public void writeJSONString(Writer out) throws IOException
88 writeJSONString(this, out);
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
96 * @see org.json.simple.JSONValue#toJSONString(Object)
99 * @return JSON text, or "null" if list is null.
101 public static String toJSONString(Collection collection)
103 final StringWriter writer = new StringWriter();
107 writeJSONString(collection, writer);
108 return writer.toString();
109 } catch (IOException e)
111 // This should never happen for a StringWriter
112 throw new RuntimeException(e);
116 public static void writeJSONString(byte[] array, Writer out)
123 else if (array.length == 0)
130 out.write(String.valueOf(array[0]));
132 for (int i = 1; i < array.length; i++)
135 out.write(String.valueOf(array[i]));
142 public static String toJSONString(byte[] array)
144 final StringWriter writer = new StringWriter();
148 writeJSONString(array, writer);
149 return writer.toString();
150 } catch (IOException e)
152 // This should never happen for a StringWriter
153 throw new RuntimeException(e);
157 public static void writeJSONString(short[] array, Writer out)
164 else if (array.length == 0)
171 out.write(String.valueOf(array[0]));
173 for (int i = 1; i < array.length; i++)
176 out.write(String.valueOf(array[i]));
183 public static String toJSONString(short[] array)
185 final StringWriter writer = new StringWriter();
189 writeJSONString(array, writer);
190 return writer.toString();
191 } catch (IOException e)
193 // This should never happen for a StringWriter
194 throw new RuntimeException(e);
198 public static void writeJSONString(int[] array, Writer out)
205 else if (array.length == 0)
212 out.write(String.valueOf(array[0]));
214 for (int i = 1; i < array.length; i++)
217 out.write(String.valueOf(array[i]));
224 public static String toJSONString(int[] array)
226 final StringWriter writer = new StringWriter();
230 writeJSONString(array, writer);
231 return writer.toString();
232 } catch (IOException e)
234 // This should never happen for a StringWriter
235 throw new RuntimeException(e);
239 public static void writeJSONString(long[] array, Writer out)
246 else if (array.length == 0)
253 out.write(String.valueOf(array[0]));
255 for (int i = 1; i < array.length; i++)
258 out.write(String.valueOf(array[i]));
265 public static String toJSONString(long[] array)
267 final StringWriter writer = new StringWriter();
271 writeJSONString(array, writer);
272 return writer.toString();
273 } catch (IOException e)
275 // This should never happen for a StringWriter
276 throw new RuntimeException(e);
280 public static void writeJSONString(float[] array, Writer out)
287 else if (array.length == 0)
294 out.write(String.valueOf(array[0]));
296 for (int i = 1; i < array.length; i++)
299 out.write(String.valueOf(array[i]));
306 public static String toJSONString(float[] array)
308 final StringWriter writer = new StringWriter();
312 writeJSONString(array, writer);
313 return writer.toString();
314 } catch (IOException e)
316 // This should never happen for a StringWriter
317 throw new RuntimeException(e);
321 public static void writeJSONString(double[] array, Writer out)
328 else if (array.length == 0)
335 out.write(String.valueOf(array[0]));
337 for (int i = 1; i < array.length; i++)
340 out.write(String.valueOf(array[i]));
347 public static String toJSONString(double[] array)
349 final StringWriter writer = new StringWriter();
353 writeJSONString(array, writer);
354 return writer.toString();
355 } catch (IOException e)
357 // This should never happen for a StringWriter
358 throw new RuntimeException(e);
362 public static void writeJSONString(boolean[] array, Writer out)
369 else if (array.length == 0)
376 out.write(String.valueOf(array[0]));
378 for (int i = 1; i < array.length; i++)
381 out.write(String.valueOf(array[i]));
388 public static String toJSONString(boolean[] array)
390 final StringWriter writer = new StringWriter();
394 writeJSONString(array, writer);
395 return writer.toString();
396 } catch (IOException e)
398 // This should never happen for a StringWriter
399 throw new RuntimeException(e);
403 public static void writeJSONString(char[] array, Writer out)
410 else if (array.length == 0)
417 out.write(String.valueOf(array[0]));
419 for (int i = 1; i < array.length; i++)
422 out.write(String.valueOf(array[i]));
429 public static String toJSONString(char[] array)
431 final StringWriter writer = new StringWriter();
435 writeJSONString(array, writer);
436 return writer.toString();
437 } catch (IOException e)
439 // This should never happen for a StringWriter
440 throw new RuntimeException(e);
444 public static void writeJSONString(Object[] array, Writer out)
451 else if (array.length == 0)
458 JSONValue.writeJSONString(array[0], out);
460 for (int i = 1; i < array.length; i++)
463 JSONValue.writeJSONString(array[i], out);
470 public static String toJSONString(Object[] array)
472 final StringWriter writer = new StringWriter();
476 writeJSONString(array, writer);
477 return writer.toString();
478 } catch (IOException e)
480 // This should never happen for a StringWriter
481 throw new RuntimeException(e);
485 public String toJSONString()
487 return toJSONString(this);
491 * Returns a string representation of this array. This is equivalent to
492 * calling {@link JSONArray#toJSONString()}.
494 public String toString()
496 return toJSONString();