50 |
26 Feb 19 |
nicklas |
1 |
/* |
50 |
26 Feb 19 |
nicklas |
* $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $ |
50 |
26 Feb 19 |
nicklas |
* Created on 2006-4-10 |
50 |
26 Feb 19 |
nicklas |
4 |
*/ |
50 |
26 Feb 19 |
nicklas |
5 |
package org.json.simple; |
50 |
26 Feb 19 |
nicklas |
6 |
|
50 |
26 Feb 19 |
nicklas |
7 |
import java.io.IOException; |
50 |
26 Feb 19 |
nicklas |
8 |
import java.io.Writer; |
50 |
26 Feb 19 |
nicklas |
9 |
import java.util.ArrayList; |
50 |
26 Feb 19 |
nicklas |
10 |
import java.util.Iterator; |
50 |
26 Feb 19 |
nicklas |
11 |
import java.util.List; |
50 |
26 Feb 19 |
nicklas |
12 |
|
50 |
26 Feb 19 |
nicklas |
13 |
|
50 |
26 Feb 19 |
nicklas |
14 |
/** |
50 |
26 Feb 19 |
nicklas |
* A JSON array. JSONObject supports java.util.List interface. |
50 |
26 Feb 19 |
nicklas |
16 |
* |
50 |
26 Feb 19 |
nicklas |
* @author FangYidong<fangyidong@yahoo.com.cn> |
50 |
26 Feb 19 |
nicklas |
18 |
*/ |
57 |
26 Feb 19 |
nicklas |
19 |
public class JSONArray extends ArrayList<Object> implements List<Object>, JSONAware, JSONStreamAware { |
50 |
26 Feb 19 |
nicklas |
20 |
private static final long serialVersionUID = 3957988303675231981L; |
50 |
26 Feb 19 |
nicklas |
21 |
|
50 |
26 Feb 19 |
nicklas |
22 |
/** |
50 |
26 Feb 19 |
nicklas |
* Encode a list into JSON text and write it to out. |
50 |
26 Feb 19 |
nicklas |
* If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level. |
50 |
26 Feb 19 |
nicklas |
25 |
* |
50 |
26 Feb 19 |
nicklas |
* @see org.json.simple.JSONValue#writeJSONString(Object, Writer) |
50 |
26 Feb 19 |
nicklas |
27 |
* |
50 |
26 Feb 19 |
nicklas |
* @param list |
50 |
26 Feb 19 |
nicklas |
* @param out |
50 |
26 Feb 19 |
nicklas |
30 |
*/ |
57 |
26 Feb 19 |
nicklas |
31 |
public static void writeJSONString(List<?> list, Writer out) throws IOException{ |
50 |
26 Feb 19 |
nicklas |
32 |
if(list == null){ |
50 |
26 Feb 19 |
nicklas |
33 |
out.write("null"); |
50 |
26 Feb 19 |
nicklas |
34 |
return; |
50 |
26 Feb 19 |
nicklas |
35 |
} |
50 |
26 Feb 19 |
nicklas |
36 |
|
50 |
26 Feb 19 |
nicklas |
37 |
boolean first = true; |
57 |
26 Feb 19 |
nicklas |
38 |
Iterator<?> iter=list.iterator(); |
50 |
26 Feb 19 |
nicklas |
39 |
|
50 |
26 Feb 19 |
nicklas |
40 |
out.write('['); |
50 |
26 Feb 19 |
nicklas |
41 |
while(iter.hasNext()){ |
50 |
26 Feb 19 |
nicklas |
42 |
if(first) |
50 |
26 Feb 19 |
nicklas |
43 |
first = false; |
50 |
26 Feb 19 |
nicklas |
44 |
else |
50 |
26 Feb 19 |
nicklas |
45 |
out.write(','); |
50 |
26 Feb 19 |
nicklas |
46 |
|
50 |
26 Feb 19 |
nicklas |
47 |
Object value=iter.next(); |
50 |
26 Feb 19 |
nicklas |
48 |
if(value == null){ |
50 |
26 Feb 19 |
nicklas |
49 |
out.write("null"); |
50 |
26 Feb 19 |
nicklas |
50 |
continue; |
50 |
26 Feb 19 |
nicklas |
51 |
} |
50 |
26 Feb 19 |
nicklas |
52 |
|
50 |
26 Feb 19 |
nicklas |
53 |
JSONValue.writeJSONString(value, out); |
50 |
26 Feb 19 |
nicklas |
54 |
} |
50 |
26 Feb 19 |
nicklas |
55 |
out.write(']'); |
50 |
26 Feb 19 |
nicklas |
56 |
} |
50 |
26 Feb 19 |
nicklas |
57 |
|
55 |
26 Feb 19 |
nicklas |
58 |
@Override |
50 |
26 Feb 19 |
nicklas |
59 |
public void writeJSONString(Writer out) throws IOException{ |
50 |
26 Feb 19 |
nicklas |
60 |
writeJSONString(this, out); |
50 |
26 Feb 19 |
nicklas |
61 |
} |
50 |
26 Feb 19 |
nicklas |
62 |
|
50 |
26 Feb 19 |
nicklas |
63 |
/** |
50 |
26 Feb 19 |
nicklas |
* Convert a list to JSON text. The result is a JSON array. |
50 |
26 Feb 19 |
nicklas |
* If this list is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. |
50 |
26 Feb 19 |
nicklas |
66 |
* |
50 |
26 Feb 19 |
nicklas |
* @see org.json.simple.JSONValue#toJSONString(Object) |
50 |
26 Feb 19 |
nicklas |
68 |
* |
50 |
26 Feb 19 |
nicklas |
* @param list |
50 |
26 Feb 19 |
nicklas |
* @return JSON text, or "null" if list is null. |
50 |
26 Feb 19 |
nicklas |
71 |
*/ |
57 |
26 Feb 19 |
nicklas |
72 |
public static String toJSONString(List<?> list){ |
50 |
26 Feb 19 |
nicklas |
73 |
if(list == null) |
50 |
26 Feb 19 |
nicklas |
74 |
return "null"; |
50 |
26 Feb 19 |
nicklas |
75 |
|
50 |
26 Feb 19 |
nicklas |
76 |
boolean first = true; |
50 |
26 Feb 19 |
nicklas |
77 |
StringBuffer sb = new StringBuffer(); |
57 |
26 Feb 19 |
nicklas |
78 |
Iterator<?> iter=list.iterator(); |
50 |
26 Feb 19 |
nicklas |
79 |
|
50 |
26 Feb 19 |
nicklas |
80 |
sb.append('['); |
50 |
26 Feb 19 |
nicklas |
81 |
while(iter.hasNext()){ |
50 |
26 Feb 19 |
nicklas |
82 |
if(first) |
50 |
26 Feb 19 |
nicklas |
83 |
first = false; |
50 |
26 Feb 19 |
nicklas |
84 |
else |
50 |
26 Feb 19 |
nicklas |
85 |
sb.append(','); |
50 |
26 Feb 19 |
nicklas |
86 |
|
50 |
26 Feb 19 |
nicklas |
87 |
Object value=iter.next(); |
50 |
26 Feb 19 |
nicklas |
88 |
if(value == null){ |
50 |
26 Feb 19 |
nicklas |
89 |
sb.append("null"); |
50 |
26 Feb 19 |
nicklas |
90 |
continue; |
50 |
26 Feb 19 |
nicklas |
91 |
} |
50 |
26 Feb 19 |
nicklas |
92 |
sb.append(JSONValue.toJSONString(value)); |
50 |
26 Feb 19 |
nicklas |
93 |
} |
50 |
26 Feb 19 |
nicklas |
94 |
sb.append(']'); |
50 |
26 Feb 19 |
nicklas |
95 |
return sb.toString(); |
50 |
26 Feb 19 |
nicklas |
96 |
} |
50 |
26 Feb 19 |
nicklas |
97 |
|
55 |
26 Feb 19 |
nicklas |
98 |
@Override |
50 |
26 Feb 19 |
nicklas |
99 |
public String toJSONString(){ |
50 |
26 Feb 19 |
nicklas |
100 |
return toJSONString(this); |
50 |
26 Feb 19 |
nicklas |
101 |
} |
50 |
26 Feb 19 |
nicklas |
102 |
|
55 |
26 Feb 19 |
nicklas |
103 |
@Override |
50 |
26 Feb 19 |
nicklas |
104 |
public String toString() { |
50 |
26 Feb 19 |
nicklas |
105 |
return toJSONString(); |
50 |
26 Feb 19 |
nicklas |
106 |
} |
50 |
26 Feb 19 |
nicklas |
107 |
|
50 |
26 Feb 19 |
nicklas |
108 |
|
50 |
26 Feb 19 |
nicklas |
109 |
|
50 |
26 Feb 19 |
nicklas |
110 |
} |