forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriterUtil.java
More file actions
459 lines (427 loc) · 16 KB
/
WriterUtil.java
File metadata and controls
459 lines (427 loc) · 16 KB
1
2
3
4
5
6
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package org.json.write;
/*
Copyright (c) 2006 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import org.json.JSONAppendable;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import java.io.IOException;
import java.util.Arrays;
/**
* Static writer methods.
*
* @author JSON.org
* @version 2016-09-14
*/
public final class WriterUtil {
// 120 spaces, divides by 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, ...
private static final String PADDING_SPACES = makePaddingSpaces();
private static final int PADDING_LENGTH = 120;
private static String makePaddingSpaces() {
char[] padding = new char[PADDING_LENGTH];
Arrays.fill(padding, ' ');
return String.valueOf(padding);
}
private WriterUtil() {
}
/**
* Indent by the given number of spaces.
*
* @param indent
* the number of character to indent.
* @param writer
* the writer.
* @throws IOException there was a problem writing the indentation
*/
static void indent(int indent, Appendable writer) throws IOException {
while(indent >= PADDING_LENGTH) {
writer.append(PADDING_SPACES);
indent -= PADDING_LENGTH;
}
if(indent > 0) {
writer.append(PADDING_SPACES, 0, indent);
}
}
/**
* Is this value a simple value for the purposes of writing a JSON structure.
*
* @param value the Object value to be tested
* @return {@code true} if this value is a simple JSON value, otherwise
* {@code false}
*/
public static boolean isSimpleValue(Object value) {
if ((value instanceof JSONAppendable) || (value instanceof JSONString)) {
return false;
}
return ((value == null) ||
(value instanceof Number) ||
(value instanceof Boolean) ||
(value instanceof CharSequence) ||
(value instanceof Enum<?>) ||
JSONObject.NULL.equals(value));
}
/**
* Write the contents of the {@code Object} as JSON text to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The value to be written
* @param writer
* Writes the serialized JSON
* @throws JSONException there was a problem writing the {@code Object}
*/
public static void writeSimpleValue(Object value, Appendable writer)
throws JSONException {
if (value == null) {
writeNull(writer);
} else if (value instanceof JSONAppendable) {
writeJSONAppendable((JSONAppendable) value, writer);
} else if (value instanceof JSONString) {
writeJSONString((JSONString) value, writer);
} else if (value instanceof Number) {
writeNumber((Number) value, writer);
} else if (value instanceof Boolean) {
writeBoolean((Boolean) value, writer);
} else if (value instanceof CharSequence) {
writeString((CharSequence) value, writer);
} else if (value instanceof Enum<?>) {
writeEnum((Enum<?>) value, writer);
} else if (JSONObject.NULL.equals(value)) {
writeNull(writer);
} else {
writeString(value.toString(), writer);
}
}
/**
* Write the {@code JSONAppendable} as a JSON value to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The {@code JSONAppendable} to be written
* @param writer
* Writes the serialized JSON
* @throws JSONException there was a problem writing the {@code JSONAppendable}
*/
public static void writeJSONAppendable(JSONAppendable value,
Appendable writer) throws JSONException {
try {
value.appendJSON(writer);
} catch(JSONException e) {
// Propagate directly, because JSONException is a RuntimeException
throw e;
} catch(IOException e) {
throw new JSONException(e);
} catch(RuntimeException e) {
throw new JSONException(e);
}
}
/**
* Write the contents of the {@code JSONString} as a JSON value to a writer.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @param value
* The {@code JSONString} to be written
* @param writer
* Writes the serialized JSON
* @throws JSONException there was a problem writing the {@code JSONString}
*/
public static void writeJSONString(JSONString value, Appendable writer)
throws JSONException {
try {
String o = value.toJSONString();
if (o != null) {
writer.append(o);
} else {
writeString(value.toString(), writer);
}
} catch(JSONException e) {
// Propagate directly, because JSONException is a RuntimeException
throw e;
} catch (IOException e) {
throw new JSONException(e);
} catch (RuntimeException e) {
throw new JSONException(e);
}
}
/**
* Write the given {@code Enum} to the given {@code Appendable} as a
* {@code String}.
*
* @param value
* An {@code Enum}
* @param writer
* The {@code Appendable} to which the {@code Enum} value is
* written
* @throws JSONException there was a problem writing the {@code Enum}
*/
public static void writeEnum(Enum<?> value, Appendable writer)
throws JSONException {
writeString(value.name(), writer);
}
/**
* Produce a string in double quotes with backslash sequences in all the
* right places. A backslash will be inserted within </, producing <\/,
* allowing JSON text to be delivered in HTML. In JSON text, a string cannot
* contain a control character or an unescaped quote or backslash.
*
* @param string
* A character sequence to be quoted
* @param w
* the {@code Appendable} to which the quoted character sequence
* will be written
* @throws JSONException there was a problem writing to the {@code Appendable}
*/
public static void writeString(CharSequence string, Appendable w)
throws JSONException {
try {
if (string == null || string.length() == 0) {
w.append("\"\"");
return;
}
char b;
char c = 0;
String hhhh;
int i;
int len = string.length();
int prev = 0;
w.append('"');
for (i = 0; i < len; i += 1) {
b = c;
c = string.charAt(i);
switch (c) {
case '\\':
case '"':
if(prev < i) {
w.append(string, prev, i);
}
w.append('\\');
prev = i;
break;
case '/':
if (b == '<') {
if(prev < i) {
w.append(string, prev, i);
}
w.append('\\');
prev = i;
}
break;
case '\b':
if(prev < i) {
w.append(string, prev, i);
}
w.append("\\b");
prev = i + 1;
break;
case '\t':
if(prev < i) {
w.append(string, prev, i);
}
w.append("\\t");
prev = i + 1;
break;
case '\n':
if(prev < i) {
w.append(string, prev, i);
}
w.append("\\n");
prev = i + 1;
break;
case '\f':
if(prev < i) {
w.append(string, prev, i);
}
w.append("\\f");
prev = i + 1;
break;
case '\r':
if(prev < i) {
w.append(string, prev, i);
}
w.append("\\r");
prev = i + 1;
break;
default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|| (c >= '\u2000' && c < '\u2100')) {
if(prev < i) {
w.append(string, prev, i);
}
hhhh = Integer.toHexString(c);
w.append("\\u0000", 0, 6 - hhhh.length());
w.append(hhhh);
prev = i + 1;
}
}
}
if(prev == 0) {
w.append(string);
} else if(prev < len) {
w.append(string, prev, len);
}
w.append('"');
} catch (IOException e) {
throw new JSONException(e);
}
}
/**
* Write the given {@code double} to the given {@code Appendable}.
*
* @param d
* A double
* @param writer
* The {@code Appendable} to which the double value is written
* @throws JSONException there was a problem writing the double
*/
public static void writeDouble(double d, Appendable writer)
throws JSONException {
if (Double.isInfinite(d) || Double.isNaN(d)) {
writeNull(writer);
} else {
// Shave off trailing zeros and decimal point, if possible.
String string = Double.toString(d);
writeNumberDigits(string, writer);
}
}
/**
* Write the given {@code long} to the given {@code Appendable}.
*
* @param number
* A long
* @param writer
* The {@code Appendable} to which the number value is written
* @throws JSONException there was a problem writing the long
*/
public static void writeLong(long number, Appendable writer)
throws JSONException {
try {
// Shave off trailing zeros and decimal point, if possible.
writer.append(Long.toString(number));
} catch (IOException e) {
throw new JSONException(e);
}
}
/**
* Write the given {@code Number} to the given {@code Appendable}.
*
* @param number
* A {@code Number}
* @param writer
* The {@code Appendable} to which the number value is written
* @throws JSONException there was a problem writing the {@code Number}
*/
public static void writeNumber(Number number, Appendable writer)
throws JSONException {
if ((number instanceof Double) || (number instanceof Float)) {
double d = number.doubleValue();
if (Double.isInfinite(d) || Double.isNaN(d)) {
writeNull(writer);
return;
}
}
// Shave off trailing zeros and decimal point, if possible.
String string = number.toString();
writeNumberDigits(string, writer);
}
/**
* Write a number value, trimming any trailing zero digits from a real number.
* If all zeros appear immediately after a decimal, the decimal is omitted
* as well.
*
* @param string the string of digits
* @param writer the {@code Appendable} to which the digits will be written
* @throws JSONException there was a problem writing to the {@code Appendable}
*/
private static void writeNumberDigits(String string,
Appendable writer) throws JSONException {
try {
int decimal = string.indexOf('.');
if (decimal > 0 && string.indexOf('e', decimal) < 0
&& string.indexOf('E', decimal) < 0) {
final int len = string.length();
int last = len;
while ((last > 0) && (string.charAt(last - 1) == '0')) {
last--;
}
if (decimal == (last - 1)) {
last--;
}
if (last < len) {
writer.append(string, 0, last);
return;
}
}
writer.append(string);
} catch (IOException e) {
throw new JSONException(e);
}
}
/**
* Write the given {@code Boolean} to the given {@code Appendable}.
*
* @param value
* A {@code Boolean}
* @param writer
* The {@code Appendable} to which the boolean value is written
* @throws JSONException there was a problem writing the {@code Boolean}
*/
public static void writeBoolean(Boolean value, Appendable writer)
throws JSONException {
try {
writer.append(value.toString());
} catch (IOException e) {
throw new JSONException(e);
}
}
/**
* Write the given {@code boolean} to the given {@code Appendable}.
*
* @param value
* A boolean
* @param writer
* The {@code Appendable} to which the boolean value is written
* @throws JSONException there was a problem writing the boolean
*/
public static void writeBoolean(boolean value, Appendable writer)
throws JSONException {
try {
writer.append(Boolean.toString(value));
} catch (IOException e) {
throw new JSONException(e);
}
}
/**
* Write the value {@code null} to the given {@code Appendable}.
*
* @param writer
* The {@code Appendable} to which the null value is written
* @throws JSONException there was a problem writing null
*/
public static void writeNull(Appendable writer) throws JSONException {
try {
writer.append("null");
} catch (IOException e) {
throw new JSONException(e);
}
}
}