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 implements JSONAware, JSONStreamAware {
20 private static final long serialVersionUID = 3957988303675231981L;
23 * Constructs an empty JSONArray.
30 * Constructs a JSONArray containing the elements of the specified
31 * collection, in the order they are returned by the collection's iterator.
33 * @param c the collection whose elements are to be placed into this JSONArray
35 public JSONArray(Collection c){
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.
43 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
48 public static void writeJSONString(Collection collection, Writer out) throws IOException{
49 if(collection == null){
55 Iterator iter=collection.iterator();
58 while(iter.hasNext()){
64 Object value=iter.next();
70 JSONValue.writeJSONString(value, out);
75 public void writeJSONString(Writer out) throws IOException{
76 writeJSONString(this, out);
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.
83 * @see org.json.simple.JSONValue#toJSONString(Object)
86 * @return JSON text, or "null" if list is null.
88 public static String toJSONString(Collection collection){
89 final StringWriter writer = new StringWriter();
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);
100 public static void writeJSONString(byte[] array, Writer out) throws IOException{
103 } else if(array.length == 0) {
107 out.write(String.valueOf(array[0]));
109 for(int i = 1; i < array.length; i++){
111 out.write(String.valueOf(array[i]));
118 public static String toJSONString(byte[] array){
119 final StringWriter writer = new StringWriter();
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);
130 public static void writeJSONString(short[] array, Writer out) throws IOException{
133 } else if(array.length == 0) {
137 out.write(String.valueOf(array[0]));
139 for(int i = 1; i < array.length; i++){
141 out.write(String.valueOf(array[i]));
148 public static String toJSONString(short[] array){
149 final StringWriter writer = new StringWriter();
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);
160 public static void writeJSONString(int[] array, Writer out) throws IOException{
163 } else if(array.length == 0) {
167 out.write(String.valueOf(array[0]));
169 for(int i = 1; i < array.length; i++){
171 out.write(String.valueOf(array[i]));
178 public static String toJSONString(int[] array){
179 final StringWriter writer = new StringWriter();
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);
190 public static void writeJSONString(long[] array, Writer out) throws IOException{
193 } else if(array.length == 0) {
197 out.write(String.valueOf(array[0]));
199 for(int i = 1; i < array.length; i++){
201 out.write(String.valueOf(array[i]));
208 public static String toJSONString(long[] array){
209 final StringWriter writer = new StringWriter();
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);
220 public static void writeJSONString(float[] array, Writer out) throws IOException{
223 } else if(array.length == 0) {
227 out.write(String.valueOf(array[0]));
229 for(int i = 1; i < array.length; i++){
231 out.write(String.valueOf(array[i]));
238 public static String toJSONString(float[] array){
239 final StringWriter writer = new StringWriter();
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);
250 public static void writeJSONString(double[] array, Writer out) throws IOException{
253 } else if(array.length == 0) {
257 out.write(String.valueOf(array[0]));
259 for(int i = 1; i < array.length; i++){
261 out.write(String.valueOf(array[i]));
268 public static String toJSONString(double[] array){
269 final StringWriter writer = new StringWriter();
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);
280 public static void writeJSONString(boolean[] array, Writer out) throws IOException{
283 } else if(array.length == 0) {
287 out.write(String.valueOf(array[0]));
289 for(int i = 1; i < array.length; i++){
291 out.write(String.valueOf(array[i]));
298 public static String toJSONString(boolean[] array){
299 final StringWriter writer = new StringWriter();
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);
310 public static void writeJSONString(char[] array, Writer out) throws IOException{
313 } else if(array.length == 0) {
317 out.write(String.valueOf(array[0]));
319 for(int i = 1; i < array.length; i++){
321 out.write(String.valueOf(array[i]));
328 public static String toJSONString(char[] array){
329 final StringWriter writer = new StringWriter();
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);
340 public static void writeJSONString(Object[] array, Writer out) throws IOException{
343 } else if(array.length == 0) {
347 JSONValue.writeJSONString(array[0], out);
349 for(int i = 1; i < array.length; i++){
351 JSONValue.writeJSONString(array[i], out);
358 public static String toJSONString(Object[] array){
359 final StringWriter writer = new StringWriter();
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);
370 public String toJSONString(){
371 return toJSONString(this);
375 * Returns a string representation of this array. This is equivalent to
376 * calling {@link JSONArray#toJSONString()}.
378 public String toString() {
379 return toJSONString();