@@ -40,37 +40,37 @@ private static String getValue(JSONTokener x, char delimiter) throws JSONExcepti
40
40
do {
41
41
c = x .next ();
42
42
} while (c == ' ' || c == '\t' );
43
- if (c == 0 ) {
44
- return null ;
45
- } else if (c == '"' || c == '\'' ) {
46
- q = c ;
47
- sb = new StringBuilder ();
48
- for (;;) {
49
- c = x .next ();
50
- if (c == q ) {
51
- //Handle escaped double-quote
52
- char nextC = x .next ();
53
- if (nextC != '\"' ) {
54
- // if our quote was the end of the file, don't step
55
- if (nextC > 0 ) {
56
- x .back ();
57
- }
58
- break ;
59
- }
60
- }
61
- if (c == 0 || c == '\n' || c == '\r' ) {
62
- throw x .syntaxError ("Missing close quote '" + q + "'." );
63
- }
64
- sb .append (c );
65
- }
66
- return sb .toString ();
67
- } else if (c == delimiter ) {
68
- x .back ();
69
- return "" ;
70
- }
71
- x .back ();
72
- return x .nextTo (delimiter );
73
- }
43
+ if (c == 0 ) {
44
+ return null ;
45
+ } else if (c == '"' || c == '\'' ) {
46
+ q = c ;
47
+ sb = new StringBuilder ();
48
+ for (;;) {
49
+ c = x .next ();
50
+ if (c == q ) {
51
+ //Handle escaped double-quote
52
+ char nextC = x .next ();
53
+ if (nextC != '\"' ) {
54
+ // if our quote was the end of the file, don't step
55
+ if (nextC > 0 ) {
56
+ x .back ();
57
+ }
58
+ break ;
59
+ }
60
+ }
61
+ if (c == 0 || c == '\n' || c == '\r' ) {
62
+ throw x .syntaxError ("Missing close quote '" + q + "'." );
63
+ }
64
+ sb .append (c );
65
+ }
66
+ return sb .toString ();
67
+ } else if (c == delimiter ) {
68
+ x .back ();
69
+ return "" ;
70
+ }
71
+ x .back ();
72
+ return x .nextTo (delimiter );
73
+ }
74
74
75
75
/**
76
76
* Produce a JSONArray of strings from a row of comma delimited values.
@@ -83,8 +83,11 @@ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
83
83
}
84
84
85
85
/**
86
- * Same as {@link #rowToJSONArray(JSONTokener)}, but with a custom delimiter.
87
- * @see #rowToJSONArray(JSONTokener)
86
+ * Produce a JSONArray of strings from a row of comma delimited values.
87
+ * @param x A JSONTokener of the source text.
88
+ * @param delimiter custom delimiter char
89
+ * @return A JSONArray of strings.
90
+ * @throws JSONException if a called function fails
88
91
*/
89
92
public static JSONArray rowToJSONArray (JSONTokener x , char delimiter ) throws JSONException {
90
93
JSONArray ja = new JSONArray ();
@@ -127,9 +130,15 @@ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) throws
127
130
}
128
131
129
132
/**
130
- * Same as {@link #rowToJSONObject(JSONArray, JSONTokener)}, but with a custom {@code delimiter}.
131
- *
132
- * @see #rowToJSONObject(JSONArray, JSONTokener)
133
+ * Produce a JSONObject from a row of comma delimited text, using a
134
+ * parallel JSONArray of strings to provides the names of the elements.
135
+ * @param names A JSONArray of names. This is commonly obtained from the
136
+ * first row of a comma delimited text file using the rowToJSONArray
137
+ * method.
138
+ * @param x A JSONTokener of the source text.
139
+ * @param delimiter custom delimiter char
140
+ * @return A JSONObject combining the names and values.
141
+ * @throws JSONException if a called function fails
133
142
*/
134
143
public static JSONObject rowToJSONObject (JSONArray names , JSONTokener x , char delimiter ) throws JSONException {
135
144
JSONArray ja = rowToJSONArray (x , delimiter );
@@ -148,8 +157,12 @@ public static String rowToString(JSONArray ja) {
148
157
}
149
158
150
159
/**
151
- * Same as {@link #rowToString(JSONArray)}, but with a custom delimiter.
152
- * @see #rowToString(JSONArray)
160
+ * Produce a comma delimited text row from a JSONArray. Values containing
161
+ * the comma character will be quoted. Troublesome characters may be
162
+ * removed.
163
+ * @param ja A JSONArray of strings.
164
+ * @param delimiter custom delimiter char
165
+ * @return A string ending in NEWLINE.
153
166
*/
154
167
public static String rowToString (JSONArray ja , char delimiter ) {
155
168
StringBuilder sb = new StringBuilder ();
@@ -193,8 +206,12 @@ public static JSONArray toJSONArray(String string) throws JSONException {
193
206
}
194
207
195
208
/**
196
- * Same as {@link #toJSONArray(String)}, but with a custom delimiter.
197
- * @see #toJSONArray(String)
209
+ * Produce a JSONArray of JSONObjects from a comma delimited text string,
210
+ * using the first row as a source of names.
211
+ * @param string The comma delimited text.
212
+ * @param delimiter custom delimiter char
213
+ * @return A JSONArray of JSONObjects.
214
+ * @throws JSONException if a called function fails
198
215
*/
199
216
public static JSONArray toJSONArray (String string , char delimiter ) throws JSONException {
200
217
return toJSONArray (new JSONTokener (string ), delimiter );
@@ -212,8 +229,12 @@ public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
212
229
}
213
230
214
231
/**
215
- * Same as {@link #toJSONArray(JSONTokener)}, but with a custom delimiter.
216
- * @see #toJSONArray(JSONTokener)
232
+ * Produce a JSONArray of JSONObjects from a comma delimited text string,
233
+ * using the first row as a source of names.
234
+ * @param x The JSONTokener containing the comma delimited text.
235
+ * @param delimiter custom delimiter char
236
+ * @return A JSONArray of JSONObjects.
237
+ * @throws JSONException if a called function fails
217
238
*/
218
239
public static JSONArray toJSONArray (JSONTokener x , char delimiter ) throws JSONException {
219
240
return toJSONArray (rowToJSONArray (x , delimiter ), x , delimiter );
@@ -232,8 +253,13 @@ public static JSONArray toJSONArray(JSONArray names, String string) throws JSONE
232
253
}
233
254
234
255
/**
235
- * Same as {@link #toJSONArray(JSONArray, String)}, but with a custom delimiter.
236
- * @see #toJSONArray(JSONArray, String)
256
+ * Produce a JSONArray of JSONObjects from a comma delimited text string
257
+ * using a supplied JSONArray as the source of element names.
258
+ * @param names A JSONArray of strings.
259
+ * @param string The comma delimited text.
260
+ * @param delimiter custom delimiter char
261
+ * @return A JSONArray of JSONObjects.
262
+ * @throws JSONException if a called function fails
237
263
*/
238
264
public static JSONArray toJSONArray (JSONArray names , String string , char delimiter ) throws JSONException {
239
265
return toJSONArray (names , new JSONTokener (string ), delimiter );
@@ -252,8 +278,13 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x) throws JSONE
252
278
}
253
279
254
280
/**
255
- * Same as {@link #toJSONArray(JSONArray, JSONTokener)}, but with a custom delimiter.
256
- * @see #toJSONArray(JSONArray, JSONTokener)
281
+ * Produce a JSONArray of JSONObjects from a comma delimited text string
282
+ * using a supplied JSONArray as the source of element names.
283
+ * @param names A JSONArray of strings.
284
+ * @param x A JSONTokener of the source text.
285
+ * @param delimiter custom delimiter char
286
+ * @return A JSONArray of JSONObjects.
287
+ * @throws JSONException if a called function fails
257
288
*/
258
289
public static JSONArray toJSONArray (JSONArray names , JSONTokener x , char delimiter ) throws JSONException {
259
290
if (names == null || names .length () == 0 ) {
@@ -287,8 +318,13 @@ public static String toString(JSONArray ja) throws JSONException {
287
318
}
288
319
289
320
/**
290
- * Same as {@link #toString(JSONArray)}, but with a custom delimiter.
291
- * @see #toString(JSONArray)
321
+ * Produce a comma delimited text from a JSONArray of JSONObjects. The
322
+ * first row will be a list of names obtained by inspecting the first
323
+ * JSONObject.
324
+ * @param ja A JSONArray of JSONObjects.
325
+ * @param delimiter custom delimiter char
326
+ * @return A comma delimited text.
327
+ * @throws JSONException if a called function fails
292
328
*/
293
329
public static String toString (JSONArray ja , char delimiter ) throws JSONException {
294
330
JSONObject jo = ja .optJSONObject (0 );
@@ -315,8 +351,14 @@ public static String toString(JSONArray names, JSONArray ja) throws JSONExceptio
315
351
}
316
352
317
353
/**
318
- * Same as {@link #toString(JSONArray,JSONArray)}, but with a custom delimiter.
319
- * @see #toString(JSONArray,JSONArray)
354
+ * Produce a comma delimited text from a JSONArray of JSONObjects using
355
+ * a provided list of names. The list of names is not included in the
356
+ * output.
357
+ * @param names A JSONArray of strings.
358
+ * @param ja A JSONArray of JSONObjects.
359
+ * @param delimiter custom delimiter char
360
+ * @return A comma delimited text.
361
+ * @throws JSONException if a called function fails
320
362
*/
321
363
public static String toString (JSONArray names , JSONArray ja , char delimiter ) throws JSONException {
322
364
if (names == null || names .length () == 0 ) {
0 commit comments