4 Copyright (c) 2002 JSON.org
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * This provides static methods to convert comma delimited text into a
29 * JSONArray, and to convert a JSONArray into comma delimited text. Comma
30 * delimited text is a very popular format for data interchange. It is
31 * understood by most database, spreadsheet, and organizer programs.
33 * Each row of text represents a row in a table or a data record. Each row ends
34 * with a NEWLINE character. Each row contains one or more values. Values are
35 * separated by commas. A value can contain any character except for comma,
36 * unless is is wrapped in single quotes or double quotes.
38 * The first row usually contains the names of the columns.
40 * A comma delimited list can be converted into a JSONArray of JSONObjects. The
41 * names for the elements in the JSONObjects can be taken from the names in the
51 * Get the next value. The value can be wrapped in quotes. The value can be
55 * A JSONTokener of the source text.
56 * @return The value string, or null if empty.
57 * @throws JSONException
58 * if the quoted string is badly formed.
60 private static String getValue(JSONTokener x) throws JSONException
68 } while (c == ' ' || c == '\t');
76 sb = new StringBuffer();
82 // Handle escaped double-quote
83 char nextC = x.next();
86 // if our quote was the end of the file, don't step
94 if (c == 0 || c == '\n' || c == '\r')
96 throw x.syntaxError("Missing close quote '" + q + "'.");
100 return sb.toString();
106 return x.nextTo(',');
111 * Produce a JSONArray of strings from a row of comma delimited values.
114 * A JSONTokener of the source text.
115 * @return A JSONArray of strings.
116 * @throws JSONException
118 public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException
120 JSONArray ja = new JSONArray();
123 String value = getValue(x);
126 || (ja.length() == 0 && value.length() == 0 && c != ','))
139 if (c == '\n' || c == '\r' || c == 0)
144 "Bad character '" + c + "' (" + (int) c + ").");
152 * Produce a JSONObject from a row of comma delimited text, using a parallel
153 * JSONArray of strings to provides the names of the elements.
156 * A JSONArray of names. This is commonly obtained from the first row
157 * of a comma delimited text file using the rowToJSONArray method.
159 * A JSONTokener of the source text.
160 * @return A JSONObject combining the names and values.
161 * @throws JSONException
163 public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
166 JSONArray ja = rowToJSONArray(x);
167 return ja != null ? ja.toJSONObject(names) : null;
171 * Produce a comma delimited text row from a JSONArray. Values containing the
172 * comma character will be quoted. Troublesome characters may be removed.
175 * A JSONArray of strings.
176 * @return A string ending in NEWLINE.
178 public static String rowToString(JSONArray ja)
180 StringBuilder sb = new StringBuilder();
181 for (int i = 0; i < ja.length(); i += 1)
187 Object object = ja.opt(i);
190 String string = object.toString();
191 if (string.length() > 0 && (string.indexOf(',') >= 0
192 || string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0
193 || string.indexOf(0) >= 0 || string.charAt(0) == '"'))
196 int length = string.length();
197 for (int j = 0; j < length; j += 1)
199 char c = string.charAt(j);
200 if (c >= ' ' && c != '"')
214 return sb.toString();
218 * Produce a JSONArray of JSONObjects from a comma delimited text string,
219 * using the first row as a source of names.
222 * The comma delimited text.
223 * @return A JSONArray of JSONObjects.
224 * @throws JSONException
226 public static JSONArray toJSONArray(String string) throws JSONException
228 return toJSONArray(new JSONTokener(string));
232 * Produce a JSONArray of JSONObjects from a comma delimited text string,
233 * using the first row as a source of names.
236 * The JSONTokener containing the comma delimited text.
237 * @return A JSONArray of JSONObjects.
238 * @throws JSONException
240 public static JSONArray toJSONArray(JSONTokener x) throws JSONException
242 return toJSONArray(rowToJSONArray(x), x);
246 * Produce a JSONArray of JSONObjects from a comma delimited text string using
247 * a supplied JSONArray as the source of element names.
250 * A JSONArray of strings.
252 * The comma delimited text.
253 * @return A JSONArray of JSONObjects.
254 * @throws JSONException
256 public static JSONArray toJSONArray(JSONArray names, String string)
259 return toJSONArray(names, new JSONTokener(string));
263 * Produce a JSONArray of JSONObjects from a comma delimited text string using
264 * a supplied JSONArray as the source of element names.
267 * A JSONArray of strings.
269 * A JSONTokener of the source text.
270 * @return A JSONArray of JSONObjects.
271 * @throws JSONException
273 public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
276 if (names == null || names.length() == 0)
280 JSONArray ja = new JSONArray();
283 JSONObject jo = rowToJSONObject(names, x);
290 if (ja.length() == 0)
298 * Produce a comma delimited text from a JSONArray of JSONObjects. The first
299 * row will be a list of names obtained by inspecting the first JSONObject.
302 * A JSONArray of JSONObjects.
303 * @return A comma delimited text.
304 * @throws JSONException
306 public static String toString(JSONArray ja) throws JSONException
308 JSONObject jo = ja.optJSONObject(0);
311 JSONArray names = jo.names();
314 return rowToString(names) + toString(names, ja);
321 * Produce a comma delimited text from a JSONArray of JSONObjects using a
322 * provided list of names. The list of names is not included in the output.
325 * A JSONArray of strings.
327 * A JSONArray of JSONObjects.
328 * @return A comma delimited text.
329 * @throws JSONException
331 public static String toString(JSONArray names, JSONArray ja)
334 if (names == null || names.length() == 0)
338 StringBuffer sb = new StringBuffer();
339 for (int i = 0; i < ja.length(); i += 1)
341 JSONObject jo = ja.optJSONObject(i);
344 sb.append(rowToString(jo.toJSONArray(names)));
347 return sb.toString();