aboutsummaryrefslogtreecommitdiff
path: root/src/org/json/simple/JSONArray.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/json/simple/JSONArray.java')
-rw-r--r--src/org/json/simple/JSONArray.java390
1 files changed, 390 insertions, 0 deletions
diff --git a/src/org/json/simple/JSONArray.java b/src/org/json/simple/JSONArray.java
new file mode 100644
index 0000000..e2c7042
--- /dev/null
+++ b/src/org/json/simple/JSONArray.java
@@ -0,0 +1,390 @@
1/*
2 * $Id: JSONArray.java,v 1.1 2006/04/15 14:10:48 platform Exp $
3 * Created on 2006-4-10
4 */
5package org.json.simple;
6
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.Iterator;
11
12import rejava.io.StringWriter;
13import rejava.io.Writer;
14
15/**
16 * A JSON array. JSONObject supports java.util.List interface.
17 *
18 * @author FangYidong<fangyidong@yahoo.com.cn>
19 */
20public class JSONArray extends ArrayList<Object> implements JSONAware, JSONStreamAware {
21 private static final long serialVersionUID = 3957988303675231981L;
22
23 /**
24 * Constructs an empty JSONArray.
25 */
26 public JSONArray() {
27 super();
28 }
29
30 /**
31 * Constructs a JSONArray containing the elements of the specified
32 * collection, in the order they are returned by the collection's iterator.
33 *
34 * @param c
35 * the collection whose elements are to be placed into this
36 * JSONArray
37 */
38 public JSONArray(final Collection<?> c) {
39 super(c);
40 }
41
42 /**
43 * Encode a list into JSON text and write it to out. If this list is also a
44 * JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific
45 * behaviours will be ignored at this top level.
46 *
47 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
48 *
49 * @param collection
50 * @param out
51 */
52 public static void writeJSONString(final Collection<?> collection, final Writer out) throws IOException {
53 if (collection == null) {
54 out.write("null");
55 return;
56 }
57
58 boolean first = true;
59 final Iterator<?> iter = collection.iterator();
60
61 out.write('[');
62 while (iter.hasNext()) {
63 if (first) {
64 first = false;
65 } else {
66 out.write(',');
67 }
68
69 final Object value = iter.next();
70 if (value == null) {
71 out.write("null");
72 continue;
73 }
74
75 JSONValue.writeJSONString(value, out);
76 }
77 out.write(']');
78 }
79
80 @Override
81 public void writeJSONString(final Writer out) throws IOException {
82 JSONArray.writeJSONString(this, out);
83 }
84
85 /**
86 * Convert a list to JSON text. The result is a JSON array. If this list is
87 * also a JSONAware, JSONAware specific behaviours will be omitted at this
88 * top level.
89 *
90 * @see org.json.simple.JSONValue#toJSONString(Object)
91 *
92 * @param collection
93 * @return JSON text, or "null" if list is null.
94 */
95 public static String toJSONString(final Collection<?> collection) {
96 final StringWriter writer = new StringWriter();
97
98 try {
99 JSONArray.writeJSONString(collection, writer);
100 return writer.toString();
101 } catch (final IOException e) {
102 // This should never happen for a StringWriter
103 throw new RuntimeException(e);
104 }
105 }
106
107 public static void writeJSONString(final byte[] array, final Writer out) throws IOException {
108 if (array == null) {
109 out.write("null");
110 } else if (array.length == 0) {
111 out.write("[]");
112 } else {
113 out.write("[");
114 out.write(String.valueOf(array[0]));
115
116 for (int i = 1; i < array.length; i++) {
117 out.write(",");
118 out.write(String.valueOf(array[i]));
119 }
120
121 out.write("]");
122 }
123 }
124
125 public static String toJSONString(final byte[] array) {
126 final StringWriter writer = new StringWriter();
127
128 try {
129 JSONArray.writeJSONString(array, writer);
130 return writer.toString();
131 } catch (final IOException e) {
132 // This should never happen for a StringWriter
133 throw new RuntimeException(e);
134 }
135 }
136
137 public static void writeJSONString(final short[] array, final Writer out) throws IOException {
138 if (array == null) {
139 out.write("null");
140 } else if (array.length == 0) {
141 out.write("[]");
142 } else {
143 out.write("[");
144 out.write(String.valueOf(array[0]));
145
146 for (int i = 1; i < array.length; i++) {
147 out.write(",");
148 out.write(String.valueOf(array[i]));
149 }
150
151 out.write("]");
152 }
153 }
154
155 public static String toJSONString(final short[] array) {
156 final StringWriter writer = new StringWriter();
157
158 try {
159 JSONArray.writeJSONString(array, writer);
160 return writer.toString();
161 } catch (final IOException e) {
162 // This should never happen for a StringWriter
163 throw new RuntimeException(e);
164 }
165 }
166
167 public static void writeJSONString(final int[] array, final Writer out) throws IOException {
168 if (array == null) {
169 out.write("null");
170 } else if (array.length == 0) {
171 out.write("[]");
172 } else {
173 out.write("[");
174 out.write(String.valueOf(array[0]));
175
176 for (int i = 1; i < array.length; i++) {
177 out.write(",");
178 out.write(String.valueOf(array[i]));
179 }
180
181 out.write("]");
182 }
183 }
184
185 public static String toJSONString(final int[] array) {
186 final StringWriter writer = new StringWriter();
187
188 try {
189 JSONArray.writeJSONString(array, writer);
190 return writer.toString();
191 } catch (final IOException e) {
192 // This should never happen for a StringWriter
193 throw new RuntimeException(e);
194 }
195 }
196
197 public static void writeJSONString(final long[] array, final Writer out) throws IOException {
198 if (array == null) {
199 out.write("null");
200 } else if (array.length == 0) {
201 out.write("[]");
202 } else {
203 out.write("[");
204 out.write(String.valueOf(array[0]));
205
206 for (int i = 1; i < array.length; i++) {
207 out.write(",");
208 out.write(String.valueOf(array[i]));
209 }
210
211 out.write("]");
212 }
213 }
214
215 public static String toJSONString(final long[] array) {
216 final StringWriter writer = new StringWriter();
217
218 try {
219 JSONArray.writeJSONString(array, writer);
220 return writer.toString();
221 } catch (final IOException e) {
222 // This should never happen for a StringWriter
223 throw new RuntimeException(e);
224 }
225 }
226
227 public static void writeJSONString(final float[] array, final Writer out) throws IOException {
228 if (array == null) {