1 |
|
package org.json; |
2 |
|
|
3 |
|
import java.io.IOException; |
4 |
|
import java.math.BigDecimal; |
5 |
|
import java.util.Collection; |
6 |
|
import java.util.Map; |
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@author |
64 |
|
@version |
65 |
|
|
|
|
| 0% |
Uncovered Elements: 179 (179) |
Complexity: 58 |
Complexity Density: 0.56 |
|
66 |
|
public class JSONWriter |
67 |
|
{ |
68 |
|
private static final int maxdepth = 200; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private boolean comma; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
protected char mode; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private final JSONObject stack[]; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private int top; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
protected Appendable writer; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
100 |
0 |
public JSONWriter(Appendable w)... |
101 |
|
{ |
102 |
0 |
this.comma = false; |
103 |
0 |
this.mode = 'i'; |
104 |
0 |
this.stack = new JSONObject[maxdepth]; |
105 |
0 |
this.top = 0; |
106 |
0 |
this.writer = w; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
@param |
113 |
|
|
114 |
|
@return |
115 |
|
@throws |
116 |
|
|
117 |
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 8 |
Complexity Density: 0.62 |
|
118 |
0 |
private JSONWriter append(String string) throws JSONException... |
119 |
|
{ |
120 |
0 |
if (string == null) |
121 |
|
{ |
122 |
0 |
throw new JSONException("Null pointer"); |
123 |
|
} |
124 |
0 |
if (this.mode == 'o' || this.mode == 'a') |
125 |
|
{ |
126 |
0 |
try |
127 |
|
{ |
128 |
0 |
if (this.comma && this.mode == 'a') |
129 |
|
{ |
130 |
0 |
this.writer.append(','); |
131 |
|
} |
132 |
0 |
this.writer.append(string); |
133 |
|
} catch (IOException e) |
134 |
|
{ |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
0 |
throw new JSONException(e); |
139 |
|
} |
140 |
0 |
if (this.mode == 'o') |
141 |
|
{ |
142 |
0 |
this.mode = 'k'; |
143 |
|
} |
144 |
0 |
this.comma = true; |
145 |
0 |
return this; |
146 |
|
} |
147 |
0 |
throw new JSONException("Value out of sequence."); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
@return |
156 |
|
@throws |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 4 |
Complexity Density: 0.67 |
|
161 |
0 |
public JSONWriter array() throws JSONException... |
162 |
|
{ |
163 |
0 |
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') |
164 |
|
{ |
165 |
0 |
this.push(null); |
166 |
0 |
this.append("["); |
167 |
0 |
this.comma = false; |
168 |
0 |
return this; |
169 |
|
} |
170 |
0 |
throw new JSONException("Misplaced array."); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
|
178 |
|
@param |
179 |
|
|
180 |
|
@return |
181 |
|
@throws |
182 |
|
|
183 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
184 |
0 |
private JSONWriter end(char m, char c) throws JSONException... |
185 |
|
{ |
186 |
0 |
if (this.mode != m) |
187 |
|
{ |
188 |
0 |
throw new JSONException( |
189 |
0 |
m == 'a' ? "Misplaced endArray." : "Misplaced endObject."); |
190 |
|
} |
191 |
0 |
this.pop(m); |
192 |
0 |
try |
193 |
|
{ |
194 |
0 |
this.writer.append(c); |
195 |
|
} catch (IOException e) |
196 |
|
{ |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
0 |
throw new JSONException(e); |
201 |
|
} |
202 |
0 |
this.comma = true; |
203 |
0 |
return this; |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
@return |
211 |
|
@throws |
212 |
|
|
213 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
0 |
public JSONWriter endArray() throws JSONException... |
215 |
|
{ |
216 |
0 |
return this.end('a', ']'); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@return |
224 |
|
@throws |
225 |
|
|
226 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
227 |
0 |
public JSONWriter endObject() throws JSONException... |
228 |
|
{ |
229 |
0 |
return this.end('k', '}'); |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
@param |
237 |
|
|
238 |
|
@return |
239 |
|
@throws |
240 |
|
|
241 |
|
|
242 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
243 |
0 |
public JSONWriter key(String string) throws JSONException... |
244 |
|
{ |
245 |
0 |
if (string == null) |
246 |
|
{ |
247 |
0 |
throw new JSONException("Null key."); |
248 |
|
} |
249 |
0 |
if (this.mode == 'k') |
250 |
|
{ |
251 |
0 |
try |
252 |
|
{ |
253 |
0 |
JSONObject topObject = this.stack[this.top - 1]; |
254 |
|
|
255 |
0 |
if (topObject.has(string)) |
256 |
|
{ |
257 |
0 |
throw new JSONException("Duplicate key \"" + string + "\""); |
258 |
|
} |
259 |
0 |
topObject.put(string, true); |
260 |
0 |
if (this.comma) |
261 |
|
{ |
262 |
0 |
this.writer.append(','); |
263 |
|
} |
264 |
0 |
this.writer.append(JSONObject.quote(string)); |
265 |
0 |
this.writer.append(':'); |
266 |
0 |
this.comma = false; |
267 |
0 |
this.mode = 'o'; |
268 |
0 |
return this; |
269 |
|
} catch (IOException e) |
270 |
|
{ |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
0 |
throw new JSONException(e); |
275 |
|
} |
276 |
|
} |
277 |
0 |
throw new JSONException("Misplaced key."); |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
@return |
286 |
|
@throws |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
291 |
0 |
public JSONWriter object() throws JSONException... |
292 |
|
{ |
293 |
0 |
if (this.mode == 'i') |
294 |
|
{ |
295 |
0 |
this.mode = 'o'; |
296 |
|
} |
297 |
0 |
if (this.mode == 'o' || this.mode == 'a') |
298 |
|
{ |
299 |
0 |
this.append("{"); |
300 |
0 |
this.push(new JSONObject()); |
301 |
0 |
this.comma = false; |
302 |
0 |
return this; |
303 |
|
} |
304 |
0 |
throw new JSONException("Misplaced object."); |
305 |
|
|
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
@param |
312 |
|
|
313 |
|
@throws |
314 |
|
|
315 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 6 |
Complexity Density: 0.86 |
|
316 |
0 |
private void pop(char c) throws JSONException... |
317 |
|
{ |
318 |
0 |
if (this.top <= 0) |
319 |
|
{ |
320 |
0 |
throw new JSONException("Nesting error."); |
321 |
|
} |
322 |
0 |
char m = this.stack[this.top - 1] == null ? 'a' : 'k'; |
323 |
0 |
if (m != c) |
324 |
|
{ |
325 |
0 |
throw new JSONException("Nesting error."); |
326 |
|
} |
327 |
0 |
this.top -= 1; |
328 |
0 |
this.mode = this.top == 0 ? 'd' |
329 |
0 |
: this.stack[this.top - 1] == null ? 'a' : 'k'; |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
@param |
336 |
|
|
337 |
|
@throws |
338 |
|
|
339 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
340 |
0 |
private void push(JSONObject jo) throws JSONException... |
341 |
|
{ |
342 |
0 |
if (this.top >= maxdepth) |
343 |
|
{ |
344 |
0 |
throw new JSONException("Nesting too deep."); |
345 |
|
} |
346 |
0 |
this.stack[this.top] = jo; |
347 |
0 |
this.mode = jo == null ? 'a' : 'k'; |
348 |
0 |
this.top += 1; |
349 |
|
} |
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
@param |
367 |
|
|
368 |
|
@return |
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@throws |
373 |
|
|
374 |
|
|
|
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 15 |
Complexity Density: 0.52 |
|
375 |
0 |
public static String valueToString(Object value) throws JSONException... |
376 |
|
{ |
377 |
0 |
if (value == null || value.equals(null)) |
378 |
|
{ |
379 |
0 |
return "null"; |
380 |
|
} |
381 |
0 |
if (value instanceof JSONString) |
382 |
|
{ |
383 |
0 |
Object object; |
384 |
0 |
try |
385 |
|
{ |
386 |
0 |
object = ((JSONString) value).toJSONString(); |
387 |
|
} catch (Exception e) |
388 |
|
{ |
389 |
0 |
throw new JSONException(e); |
390 |
|
} |
391 |
0 |
if (object instanceof String) |
392 |
|
{ |
393 |
0 |
return (String) object; |
394 |
|
} |
395 |
0 |
throw new JSONException("Bad value from toJSONString: " + object); |
396 |
|
} |
397 |
0 |
if (value instanceof Number) |
398 |
|
{ |
399 |
|
|
400 |
|
|
401 |
0 |
final String numberAsString = JSONObject |
402 |
|
.numberToString((Number) value); |
403 |
0 |
try |
404 |
|
{ |
405 |
|
|
406 |
|
|
407 |
0 |
@SuppressWarnings("unused") |
408 |
|
BigDecimal unused = new BigDecimal(numberAsString); |
409 |
|
|
410 |
0 |
return numberAsString; |
411 |
|
} catch (NumberFormatException ex) |
412 |
|
{ |
413 |
|
|
414 |
|
|
415 |
0 |
return JSONObject.quote(numberAsString); |
416 |
|
} |
417 |
|
} |
418 |
0 |
if (value instanceof Boolean || value instanceof JSONObject |
419 |
|
|| value instanceof JSONArray) |
420 |
|
{ |
421 |
0 |
return value.toString(); |
422 |
|
} |
423 |
0 |
if (value instanceof Map) |
424 |
|
{ |
425 |
0 |
Map<?, ?> map = (Map<?, ?>) value; |
426 |
0 |
return new JSONObject(map).toString(); |
427 |
|
} |
428 |
0 |
if (value instanceof Collection) |
429 |
|
{ |
430 |
0 |
Collection<?> coll = (Collection<?>) value; |
431 |
0 |
return new JSONArray(coll).toString(); |
432 |
|
} |
433 |
0 |
if (value.getClass().isArray()) |
434 |
|
{ |
435 |
0 |
return new JSONArray(value).toString(); |
436 |
|
} |
437 |
0 |
if (value instanceof Enum<?>) |
438 |
|
{ |
439 |
0 |
return JSONObject.quote(((Enum<?>) value).name()); |
440 |
|
} |
441 |
0 |
return JSONObject.quote(value.toString()); |
442 |
|
} |
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
@param |
448 |
|
|
449 |
|
@return |
450 |
|
@throws |
451 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
452 |
0 |
public JSONWriter value(boolean b) throws JSONException... |
453 |
|
{ |
454 |
0 |
return this.append(b ? "true" : "false"); |
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
|
|
459 |
|
|
460 |
|
@param |
461 |
|
|
462 |
|
@return |
463 |
|
@throws |
464 |
|
|
465 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
466 |
0 |
public JSONWriter value(double d) throws JSONException... |
467 |
|
{ |
468 |
0 |
return this.value(Double.valueOf(d)); |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
@param |
475 |
|
|
476 |
|
@return |
477 |
|
@throws |
478 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
479 |
0 |
public JSONWriter value(long l) throws JSONException... |
480 |
|
{ |
481 |
0 |
return this.append(Long.toString(l)); |
482 |
|
} |
483 |
|
|
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
@param |
488 |
|
|
489 |
|
|
490 |
|
|
491 |
|
@return |
492 |
|
@throws |
493 |
|
|
494 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
495 |
0 |
public JSONWriter value(Object object) throws JSONException... |
496 |
|
{ |
497 |
0 |
return this.append(valueToString(object)); |
498 |
|
} |
499 |
|
} |