|
| 1 | +package org.json; |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2002 JSON.org |
| 5 | +
|
| 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: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +The Software shall be used for Good, not Evil. |
| 17 | +
|
| 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 |
| 24 | +SOFTWARE. |
| 25 | +*/ |
| 26 | + |
| 27 | +/** |
| 28 | + * This provides static methods to convert comma delimited text into a |
| 29 | + * JSONArray, and to covert 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. |
| 32 | + * <p> |
| 33 | + * Each row of text represents a row in a table or a data record. Each row |
| 34 | + * ends with a NEWLINE character. Each row contains one or more values. |
| 35 | + * Values are separated by commas. A value can contain any character except |
| 36 | + * for comma, unless is is wrapped in single quotes or double quotes. |
| 37 | + * <p> |
| 38 | + * The first row usually contains the names of the columns. |
| 39 | + * <p> |
| 40 | + * A comma delimited list can be converted into a JSONArray of JSONObjects. |
| 41 | + * The names for the elements in the JSONObjects can be taken from the names |
| 42 | + * in the first row. |
| 43 | + * @author JSON.org |
| 44 | + * @version 2009-09-11 |
| 45 | + */ |
| 46 | +public class CDL { |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the next value. The value can be wrapped in quotes. The value can |
| 50 | + * be empty. |
| 51 | + * @param x A JSONTokener of the source text. |
| 52 | + * @return The value string, or null if empty. |
| 53 | + * @throws JSONException if the quoted string is badly formed. |
| 54 | + */ |
| 55 | + private static String getValue(JSONTokener x) throws JSONException { |
| 56 | + char c; |
| 57 | + char q; |
| 58 | + StringBuffer sb; |
| 59 | + do { |
| 60 | + c = x.next(); |
| 61 | + } while (c == ' ' || c == '\t'); |
| 62 | + switch (c) { |
| 63 | + case 0: |
| 64 | + return null; |
| 65 | + case '"': |
| 66 | + case '\'': |
| 67 | + q = c; |
| 68 | + sb = new StringBuffer(); |
| 69 | + for (;;) { |
| 70 | + c = x.next(); |
| 71 | + if (c == q) { |
| 72 | + break; |
| 73 | + } |
| 74 | + if (c == 0 || c == '\n' || c == '\r') { |
| 75 | + throw x.syntaxError("Missing close quote '" + q + "'."); |
| 76 | + } |
| 77 | + sb.append(c); |
| 78 | + } |
| 79 | + return sb.toString(); |
| 80 | + case ',': |
| 81 | + x.back(); |
| 82 | + return ""; |
| 83 | + default: |
| 84 | + x.back(); |
| 85 | + return x.nextTo(','); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Produce a JSONArray of strings from a row of comma delimited values. |
| 91 | + * @param x A JSONTokener of the source text. |
| 92 | + * @return A JSONArray of strings. |
| 93 | + * @throws JSONException |
| 94 | + */ |
| 95 | + public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { |
| 96 | + JSONArray ja = new JSONArray(); |
| 97 | + for (;;) { |
| 98 | + String value = getValue(x); |
| 99 | + char c = x.next(); |
| 100 | + if (value == null || |
| 101 | + (ja.length() == 0 && value.length() == 0 && c != ',')) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + ja.put(value); |
| 105 | + for (;;) { |
| 106 | + if (c == ',') { |
| 107 | + break; |
| 108 | + } |
| 109 | + if (c != ' ') { |
| 110 | + if (c == '\n' || c == '\r' || c == 0) { |
| 111 | + return ja; |
| 112 | + } |
| 113 | + throw x.syntaxError("Bad character '" + c + "' (" + |
| 114 | + (int)c + ")."); |
| 115 | + } |
| 116 | + c = x.next(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Produce a JSONObject from a row of comma delimited text, using a |
| 123 | + * parallel JSONArray of strings to provides the names of the elements. |
| 124 | + * @param names A JSONArray of names. This is commonly obtained from the |
| 125 | + * first row of a comma delimited text file using the rowToJSONArray |
| 126 | + * method. |
| 127 | + * @param x A JSONTokener of the source text. |
| 128 | + * @return A JSONObject combining the names and values. |
| 129 | + * @throws JSONException |
| 130 | + */ |
| 131 | + public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) |
| 132 | + throws JSONException { |
| 133 | + JSONArray ja = rowToJSONArray(x); |
| 134 | + return ja != null ? ja.toJSONObject(names) : null; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Produce a JSONArray of JSONObjects from a comma delimited text string, |
| 139 | + * using the first row as a source of names. |
| 140 | + * @param string The comma delimited text. |
| 141 | + * @return A JSONArray of JSONObjects. |
| 142 | + * @throws JSONException |
| 143 | + */ |
| 144 | + public static JSONArray toJSONArray(String string) throws JSONException { |
| 145 | + return toJSONArray(new JSONTokener(string)); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Produce a JSONArray of JSONObjects from a comma delimited text string, |
| 150 | + * using the first row as a source of names. |
| 151 | + * @param x The JSONTokener containing the comma delimited text. |
| 152 | + * @return A JSONArray of JSONObjects. |
| 153 | + * @throws JSONException |
| 154 | + */ |
| 155 | + public static JSONArray toJSONArray(JSONTokener x) throws JSONException { |
| 156 | + return toJSONArray(rowToJSONArray(x), x); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Produce a JSONArray of JSONObjects from a comma delimited text string |
| 161 | + * using a supplied JSONArray as the source of element names. |
| 162 | + * @param names A JSONArray of strings. |
| 163 | + * @param string The comma delimited text. |
| 164 | + * @return A JSONArray of JSONObjects. |
| 165 | + * @throws JSONException |
| 166 | + */ |
| 167 | + public static JSONArray toJSONArray(JSONArray names, String string) |
| 168 | + throws JSONException { |
| 169 | + return toJSONArray(names, new JSONTokener(string)); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Produce a JSONArray of JSONObjects from a comma delimited text string |
| 174 | + * using a supplied JSONArray as the source of element names. |
| 175 | + * @param names A JSONArray of strings. |
| 176 | + * @param x A JSONTokener of the source text. |
| 177 | + * @return A JSONArray of JSONObjects. |
| 178 | + * @throws JSONException |
| 179 | + */ |
| 180 | + public static JSONArray toJSONArray(JSONArray names, JSONTokener x) |
| 181 | + throws JSONException { |
| 182 | + if (names == null || names.length() == 0) { |
| 183 | + return null; |
| 184 | + } |
| 185 | + JSONArray ja = new JSONArray(); |
| 186 | + for (;;) { |
| 187 | + JSONObject jo = rowToJSONObject(names, x); |
| 188 | + if (jo == null) { |
| 189 | + break; |
| 190 | + } |
| 191 | + ja.put(jo); |
| 192 | + } |
| 193 | + if (ja.length() == 0) { |
| 194 | + return null; |
| 195 | + } |
| 196 | + return ja; |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + /** |
| 201 | + * Produce a comma delimited text row from a JSONArray. Values containing |
| 202 | + * the comma character will be quoted. Troublesome characters may be |
| 203 | + * removed. |
| 204 | + * @param ja A JSONArray of strings. |
| 205 | + * @return A string ending in NEWLINE. |
| 206 | + */ |
| 207 | + public static String rowToString(JSONArray ja) { |
| 208 | + StringBuffer sb = new StringBuffer(); |
| 209 | + for (int i = 0; i < ja.length(); i += 1) { |
| 210 | + if (i > 0) { |
| 211 | + sb.append(','); |
| 212 | + } |
| 213 | + Object o = ja.opt(i); |
| 214 | + if (o != null) { |
| 215 | + String s = o.toString(); |
| 216 | + if (s.length() > 0 && (s.indexOf(',') >= 0 || s.indexOf('\n') >= 0 || |
| 217 | + s.indexOf('\r') >= 0 || s.indexOf(0) >= 0 || |
| 218 | + s.charAt(0) == '"')) { |
| 219 | + sb.append('"'); |
| 220 | + int length = s.length(); |
| 221 | + for (int j = 0; j < length; j += 1) { |
| 222 | + char c = s.charAt(j); |
| 223 | + if (c >= ' ' && c != '"') { |
| 224 | + sb.append(c); |
| 225 | + } |
| 226 | + } |
| 227 | + sb.append('"'); |
| 228 | + } else { |
| 229 | + sb.append(s); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + sb.append('\n'); |
| 234 | + return sb.toString(); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Produce a comma delimited text from a JSONArray of JSONObjects. The |
| 239 | + * first row will be a list of names obtained by inspecting the first |
| 240 | + * JSONObject. |
| 241 | + * @param ja A JSONArray of JSONObjects. |
| 242 | + * @return A comma delimited text. |
| 243 | + * @throws JSONException |
| 244 | + */ |
| 245 | + public static String toString(JSONArray ja) throws JSONException { |
| 246 | + JSONObject jo = ja.optJSONObject(0); |
| 247 | + if (jo != null) { |
| 248 | + JSONArray names = jo.names(); |
| 249 | + if (names != null) { |
| 250 | + return rowToString(names) + toString(names, ja); |
| 251 | + } |
| 252 | + } |
| 253 | + return null; |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Produce a comma delimited text from a JSONArray of JSONObjects using |
| 258 | + * a provided list of names. The list of names is not included in the |
| 259 | + * output. |
| 260 | + * @param names A JSONArray of strings. |
| 261 | + * @param ja A JSONArray of JSONObjects. |
| 262 | + * @return A comma delimited text. |
| 263 | + * @throws JSONException |
| 264 | + */ |
| 265 | + public static String toString(JSONArray names, JSONArray ja) |
| 266 | + throws JSONException { |
| 267 | + if (names == null || names.length() == 0) { |
| 268 | + return null; |
| 269 | + } |
| 270 | + StringBuffer sb = new StringBuffer(); |
| 271 | + for (int i = 0; i < ja.length(); i += 1) { |
| 272 | + JSONObject jo = ja.optJSONObject(i); |
| 273 | + if (jo != null) { |
| 274 | + sb.append(rowToString(jo.toJSONArray(names))); |
| 275 | + } |
| 276 | + } |
| 277 | + return sb.toString(); |
| 278 | + } |
| 279 | +} |
0 commit comments