1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
package org.json.simple; |
6 |
|
|
7 |
|
import java.io.IOException; |
8 |
|
import java.io.Reader; |
9 |
|
import java.io.StringReader; |
10 |
|
import java.io.StringWriter; |
11 |
|
import java.io.Writer; |
12 |
|
import java.util.Collection; |
13 |
|
import java.util.Locale; |
14 |
|
|
15 |
|
import java.util.Map; |
16 |
|
|
17 |
|
import org.json.simple.parser.JSONParser; |
18 |
|
import org.json.simple.parser.ParseException; |
19 |
|
|
20 |
|
|
21 |
|
@author |
22 |
|
|
|
|
| 0% |
Uncovered Elements: 178 (178) |
Complexity: 50 |
Complexity Density: 0.42 |
|
23 |
|
public class JSONValue |
24 |
|
{ |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
@see |
30 |
|
@see |
31 |
|
|
32 |
|
@param |
33 |
|
@return |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@deprecated |
38 |
|
|
39 |
|
@link |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
41 |
0 |
@Deprecated... |
42 |
|
public static Object parse(Reader in) |
43 |
|
{ |
44 |
0 |
try |
45 |
|
{ |
46 |
0 |
JSONParser parser = new JSONParser(); |
47 |
0 |
return parser.parse(in); |
48 |
|
} catch (Exception e) |
49 |
|
{ |
50 |
0 |
return null; |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@see |
59 |
|
@see |
60 |
|
|
61 |
|
@param |
62 |
|
@return |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@deprecated |
67 |
|
|
68 |
|
@link |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
70 |
0 |
@Deprecated... |
71 |
|
public static Object parse(String s) |
72 |
|
{ |
73 |
0 |
StringReader in = new StringReader(s); |
74 |
0 |
return parse(in); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@see |
81 |
|
|
82 |
|
@param |
83 |
|
@return |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@throws |
88 |
|
@throws |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
90 |
0 |
public static Object parseWithException(Reader in)... |
91 |
|
throws IOException, ParseException |
92 |
|
{ |
93 |
0 |
JSONParser parser = new JSONParser(); |
94 |
0 |
return parser.parse(in); |
95 |
|
} |
96 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
97 |
0 |
public static Object parseWithException(String s) throws ParseException... |
98 |
|
{ |
99 |
0 |
JSONParser parser = new JSONParser(); |
100 |
0 |
return parser.parse(s); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@see |
115 |
|
@see |
116 |
|
|
117 |
|
@param |
118 |
|
@param |
119 |
|
|
|
|
| 0% |
Uncovered Elements: 106 (106) |
Complexity: 24 |
Complexity Density: 0.38 |
|
120 |
0 |
public static void writeJSONString(Object value, Writer out)... |
121 |
|
throws IOException |
122 |
|
{ |
123 |
0 |
if (value == null) |
124 |
|
{ |
125 |
0 |
out.write("null"); |
126 |
0 |
return; |
127 |
|
} |
128 |
|
|
129 |
0 |
if (value instanceof String) |
130 |
|
{ |
131 |
0 |
out.write('\"'); |
132 |
0 |
out.write(escape((String) value)); |
133 |
0 |
out.write('\"'); |
134 |
0 |
return; |
135 |
|
} |
136 |
|
|
137 |
0 |
if (value instanceof Double) |
138 |
|
{ |
139 |
0 |
if (((Double) value).isInfinite() || ((Double) value).isNaN()) |
140 |
0 |
out.write("null"); |
141 |
|
else |
142 |
0 |
out.write(value.toString()); |
143 |
0 |
return; |
144 |
|
} |
145 |
|
|
146 |
0 |
if (value instanceof Float) |
147 |
|
{ |
148 |
0 |
if (((Float) value).isInfinite() || ((Float) value).isNaN()) |
149 |
0 |
out.write("null"); |
150 |
|
else |
151 |
0 |
out.write(value.toString()); |
152 |
0 |
return; |
153 |
|
} |
154 |
|
|
155 |
0 |
if (value instanceof Number) |
156 |
|
{ |
157 |
0 |
out.write(value.toString()); |
158 |
0 |
return; |
159 |
|
} |
160 |
|
|
161 |
0 |
if (value instanceof Boolean) |
162 |
|
{ |
163 |
0 |
out.write(value.toString()); |
164 |
0 |
return; |
165 |
|
} |
166 |
|
|
167 |
0 |
if ((value instanceof JSONStreamAware)) |
168 |
|
{ |
169 |
0 |
((JSONStreamAware) value).writeJSONString(out); |
170 |
0 |
return; |
171 |
|
} |
172 |
|
|
173 |
0 |
if ((value instanceof JSONAware)) |
174 |
|
{ |
175 |
0 |
out.write(((JSONAware) value).toJSONString()); |
176 |
0 |
return; |
177 |
|
} |
178 |
|
|
179 |
0 |
if (value instanceof Map) |
180 |
|
{ |
181 |
0 |
JSONObject.writeJSONString((Map) value, out); |
182 |
0 |
return; |
183 |
|
} |
184 |
|
|
185 |
0 |
if (value instanceof Collection) |
186 |
|
{ |
187 |
0 |
JSONArray.writeJSONString((Collection) value, out); |
188 |
0 |
return; |
189 |
|
} |
190 |
|
|
191 |
0 |
if (value instanceof byte[]) |
192 |
|
{ |
193 |
0 |
JSONArray.writeJSONString((byte[]) value, out); |
194 |
0 |
return; |
195 |
|
} |
196 |
|
|
197 |
0 |
if (value instanceof short[]) |
198 |
|
{ |
199 |
0 |
JSONArray.writeJSONString((short[]) value, out); |
200 |
0 |
return; |
201 |
|
} |
202 |
|
|
203 |
0 |
if (value instanceof int[]) |
204 |
|
{ |
205 |
0 |
JSONArray.writeJSONString((int[]) value, out); |
206 |
0 |
return; |
207 |
|
} |
208 |
|
|
209 |
0 |
if (value instanceof long[]) |
210 |
|
{ |
211 |
0 |
JSONArray.writeJSONString((long[]) value, out); |
212 |
0 |
return; |
213 |
|
} |
214 |
|
|
215 |
0 |
if (value instanceof float[]) |
216 |
|
{ |
217 |
0 |
JSONArray.writeJSONString((float[]) value, out); |
218 |
0 |
return; |
219 |
|
} |
220 |
|
|
221 |
0 |
if (value instanceof double[]) |
222 |
|
{ |
223 |
0 |
JSONArray.writeJSONString((double[]) value, out); |
224 |
0 |
return; |
225 |
|
} |
226 |
|
|
227 |
0 |
if (value instanceof boolean[]) |
228 |
|
{ |
229 |
0 |
JSONArray.writeJSONString((boolean[]) value, out); |
230 |
0 |
return; |
231 |
|
} |
232 |
|
|
233 |
0 |
if (value instanceof char[]) |
234 |
|
{ |
235 |
0 |
JSONArray.writeJSONString((char[]) value, out); |
236 |
0 |
return; |
237 |
|
} |
238 |
|
|
239 |
0 |
if (value instanceof Object[]) |
240 |
|
{ |
241 |
0 |
JSONArray.writeJSONString((Object[]) value, out); |
242 |
0 |
return; |
243 |
|
} |
244 |
|
|
245 |
0 |
out.write(value.toString()); |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
@see |
259 |
|
@see |
260 |
|
|
261 |
|
@param |
262 |
|
@return |
263 |
|
|
264 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
265 |
0 |
public static String toJSONString(Object value)... |
266 |
|
{ |
267 |
0 |
final StringWriter writer = new StringWriter(); |
268 |
|
|
269 |
0 |
try |
270 |
|
{ |
271 |
0 |
writeJSONString(value, writer); |
272 |
0 |
return writer.toString(); |
273 |
|
} catch (IOException e) |
274 |
|
{ |
275 |
|
|
276 |
0 |
throw new RuntimeException(e); |
277 |
|
} |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
@param |
285 |
|
@return |
286 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
287 |
0 |
public static String escape(String s)... |
288 |
|
{ |
289 |
0 |
if (s == null) |
290 |
0 |
return null; |
291 |
0 |
StringBuffer sb = new StringBuffer(); |
292 |
0 |
escape(s, sb); |
293 |
0 |
return sb.toString(); |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
@param |
298 |
|
|
299 |
|
@param |
300 |
|
|
|
|
| 0% |
Uncovered Elements: 42 (42) |
Complexity: 17 |
Complexity Density: 0.47 |
|
301 |
0 |
static void escape(String s, StringBuffer sb)... |
302 |
|
{ |
303 |
0 |
final int len = s.length(); |
304 |
0 |
for (int i = 0; i < len; i++) |
305 |
|
{ |
306 |
0 |
char ch = s.charAt(i); |
307 |
0 |
switch (ch) |
308 |
|
{ |
309 |
0 |
case '"': |
310 |
0 |
sb.append("\\\""); |
311 |
0 |
break; |
312 |
0 |
case '\\': |
313 |
0 |
sb.append("\\\\"); |
314 |
0 |
break; |
315 |
0 |
case '\b': |
316 |
0 |
sb.append("\\b"); |
317 |
0 |
break; |
318 |
0 |
case '\f': |
319 |
0 |
sb.append("\\f"); |
320 |
0 |
break; |
321 |
0 |
case '\n': |
322 |
0 |
sb.append("\\n"); |
323 |
0 |
break; |
324 |
0 |
case '\r': |
325 |
0 |
sb.append("\\r"); |
326 |
0 |
break; |
327 |
0 |
case '\t': |
328 |
0 |
sb.append("\\t"); |
329 |
0 |
break; |
330 |
0 |
case '/': |
331 |
0 |
sb.append("\\/"); |
332 |
0 |
break; |
333 |
0 |
default: |
334 |
|
|
335 |
0 |
if ((ch >= '\u0000' && ch <= '\u001F') |
336 |
|
|| (ch >= '\u007F' && ch <= '\u009F') |
337 |
|
|| (ch >= '\u2000' && ch <= '\u20FF')) |
338 |
|
{ |
339 |
0 |
String ss = Integer.toHexString(ch); |
340 |
0 |
sb.append("\\u"); |
341 |
0 |
for (int k = 0; k < 4 - ss.length(); k++) |
342 |
|
{ |
343 |
0 |
sb.append('0'); |
344 |
|
} |
345 |
0 |
sb.append(ss.toUpperCase(Locale.ROOT)); |
346 |
|
} |
347 |
|
else |
348 |
|
{ |
349 |
0 |
sb.append(ch); |
350 |
|
} |
351 |
|
} |
352 |
|
} |
353 |
|
} |
354 |
|
|
355 |
|
} |