-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.java
345 lines (316 loc) · 11.3 KB
/
Generator.java
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
//------------------------------------------------------------------------------
//Copyright 2014 Lukasz Bownik, Yidong Fang, Chris Nokleberg, Dave Hughes
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//------------------------------------------------------------------------------
package primitive.json;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*******************************************************************************
* JSON generator. This class is not thread safe but can be reused for generating
* consecutive JSON messages one by one.
* This generator accepts object of type:
* <ul>
* <li>null,</li>
* <li>java.util.HashMap,</li>
* <li>java.util.ArrayList,</li>
* <li>java.lang.String,</li>
* <li>java.lang.Byte,</li>
* <li>java.lang.Short,</li>
* <li>java.lang.Integer,</li>
* <li>java.lang.Long,</li>
* <li>java.lang.Float,</li>
* <li>java.lang.Double,</li>
* <li>java.lang.Boolean,</li>
* <li>org.json.primitive.Null,</li>
* <li>objects of any other class (converted to string using toString method)</li>
* </ul>
* @see https://github.com/lbownik/primitive-json
* @author [email protected]
******************************************************************************/
public final class Generator {
/****************************************************************************
* Create a generator
***************************************************************************/
public Generator() {
}
/****************************************************************************
* Encode an object into JSON text and write it to out.
*
* @param value JSON object.
* @param out appendable object.
* @throws IOException if woutput error occurs.
* @throws NullPointerException if value or out == null.
* @throws IllegalArgumentException if value is not Map or List.
***************************************************************************/
public void write(final Object value, final Appendable out)
throws IOException {
if (value == null) {
throw new NullPointerException("value");
}
if (Map.class.isInstance(value)) {
write((Map) value, out);
return;
}
if (List.class.isInstance(value)) {
write((List) value, out);
return;
}
throw new IllegalArgumentException("Only Map or List accepted");
}
/****************************************************************************
* Encode an object into JSON text.
*
* @param value JSON object.
* @return JSON string.
* @throws NullPointerException if value == null.
* @throws IllegalArgumentException if value is not Hashtable or ArrayList.
***************************************************************************/
public String toString(final Object value) {
try {
final Appendable wr = new StringBuilder(32);
write(value, wr);
return wr.toString();
} catch (final IOException e) {
throw new RuntimeException(e.getMessage()); // never happens
}
}
/****************************************************************************
* Encode an ArrayList into JSON text and write it to out.
*
* @param value list.
* @param out appendable object.
* @throws IOException if output error occurs.
* @throws NullPointerException if value or out == null.
***************************************************************************/
public void write(final List<?> value, final Appendable out)
throws IOException {
final int lastIndex = value.size() - 1;
out.append('[');
if (lastIndex > -1) {
for (int i = 0; i < lastIndex; ++i) {
writeValue(value.get(i), out);
out.append(',');
}
writeValue(value.get(lastIndex), out);
}
out.append(']');
}
/****************************************************************************
* Encode an List into JSON text.
*
* @param value list.
* @return JSON string.
* @throws NullPointerException if value == null.
***************************************************************************/
public String toString(final List<?> value) {
try {
final Appendable wr = new StringBuilder(16);
write(value, wr);
return wr.toString();
} catch (final IOException e) {
throw new RuntimeException(e.getMessage()); // never happens
}
}
/****************************************************************************
* Encode a HashMap into JSON text and write it to out.
*
* @param value hashtable.
* @param out appendable object.
* @throws IOException if woutput error occurs.
* @throws NullPointerException if value or out == null.
***************************************************************************/
public void write(final Map value, final Appendable out)
throws IOException {
final Iterator<Map.Entry> entries = value.entrySet().iterator();
out.append('{');
if (entries.hasNext()) {
final Map.Entry entry = entries.next();
out.append('\"');
writeEscaped(entry.getKey().toString(), out);
out.append('\"');
out.append(':');
writeValue(entry.getValue(), out);
}
while (entries.hasNext()) {
out.append(',');
final Map.Entry entry = entries.next();
out.append('\"');
writeEscaped(entry.getKey().toString(), out);
out.append('\"');
out.append(':');
writeValue(entry.getValue(), out);
}
out.append('}');
}
/****************************************************************************
* Encode a HashMap into JSON text.
*
* @param value map.
* @return JSON string.
* @throws NullPointerException if value == null.
***************************************************************************/
public String toString(final Map<?,?> value) {
try {
final Appendable wr = new StringBuilder(16);
write(value, wr);
return wr.toString();
} catch (final IOException e) {
throw new RuntimeException(e.getMessage()); // never happens
}
}
/****************************************************************************
*
***************************************************************************/
private void writeValue(final Object value, final Appendable out)
throws IOException {
if (value == null) {
out.append("null");
return;
}
final Class<?> cls = value.getClass();
if (cls == Double.class) {
final Double d = (Double) value;
if (d.isInfinite() | d.isNaN()) {
out.append("null");
} else {
write(d.doubleValue(), out);
}
return;
}
if (cls == Float.class) {
final Float f = (Float) value;
if (f.isInfinite() | f.isNaN()) {
out.append("null");
} else {
write(f.doubleValue(), out);
}
return;
}
if (Number.class.isInstance(value)) {
write(((Number) value).longValue(), out);
return;
}
if (cls == Boolean.class) {
out.append(value.toString());
return;
}
if (Map.class.isInstance(value)) {
write((Map) value, out);
return;
}
if (List.class.isInstance(value)) {
write((List) value, out);
return;
}
out.append('\"');
writeEscaped(value.toString(), out);
out.append('\"');
}
/****************************************************************************
*
***************************************************************************/
private static void writeEscaped(final String s, final Appendable out)
throws IOException {
final int length = s.length();
for (int i = 0; i < length; i++) {
final char ch = s.charAt(i);
switch (ch) {
case '"':
out.append('\\');
out.append('"');
break;
case '\\':
out.append('\\');
out.append('\\');
break;
case '\b':
out.append('\\');
out.append('b');
break;
case '\f':
out.append('\\');
out.append('f');
break;
case '\n':
out.append('\\');
out.append('n');
break;
case '\r':
out.append('\\');
out.append('r');
break;
case '\t':
out.append('\\');
out.append('t');
break;
case '/':
out.append('\\');
out.append('/');
break;
default:
out.append(ch);
}
}//for
}
/****************************************************************************
*
***************************************************************************/
private void write(long value, final Appendable out)
throws IOException {
if (value == 0) {
out.append('0');
return;
}
if (value < 0) {
out.append('-');
value *= -1;
}
final int[] buf = this.buf;
int index = 0;
while (value > 0) {
buf[index++] = '0' + (int) (value % 10);
value /= 10;
}
while (index > 0) {
out.append((char) buf[--index]);
}
}
/****************************************************************************
*
***************************************************************************/
private void write(double value, final Appendable out)
throws IOException {
write((long) value, out);
if (value < 0) {
value *= -1;
}
double decimal = value - (long) value;
out.append('.');
if (decimal == 0.0) {
out.append('0');
return;
}
for (int i = 0; i < 14; i++) {
decimal *= 10.0;
out.append((char) ('0' + (int) decimal));
decimal -= (int) decimal;
}
}
/****************************************************************************
*
***************************************************************************/
final int[] buf = new int[22];
}