Clover icon

Coverage Report

  1. Project Clover database Fri Nov 1 2024 11:46:37 GMT
  2. Package org.json.simple

File JSONValue.java

 

Coverage histogram

../../../img/srcFileCovDistChart2.png
54% of files have more coverage

Code metrics

50
120
8
1
355
223
50
0.42
15
8
6.25

Classes

Class Line # Actions
JSONValue 23 120 50
0.1460674114.6%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
3    * Created on 2006-4-15
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    // import java.util.List;
15    import java.util.Map;
16   
17    import org.json.simple.parser.JSONParser;
18    import org.json.simple.parser.ParseException;
19   
20    /**
21    * @author FangYidong<fangyidong@yahoo.com.cn>
22    */
 
23    public class JSONValue
24    {
25    /**
26    * Parse JSON text into java object from the input source. Please use
27    * parseWithException() if you don't want to ignore the exception.
28    *
29    * @see org.json.simple.parser.JSONParser#parse(Reader)
30    * @see #parseWithException(Reader)
31    *
32    * @param in
33    * @return Instance of the following: org.json.simple.JSONObject,
34    * org.json.simple.JSONArray, java.lang.String, java.lang.Number,
35    * java.lang.Boolean, null
36    *
37    * @deprecated this method may throw an {@code Error} instead of returning
38    * {@code null}; please use
39    * {@link JSONValue#parseWithException(Reader)} instead
40    */
 
41  0 toggle @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    * Parse JSON text into java object from the given string. Please use
56    * parseWithException() if you don't want to ignore the exception.
57    *
58    * @see org.json.simple.parser.JSONParser#parse(Reader)
59    * @see #parseWithException(Reader)
60    *
61    * @param s
62    * @return Instance of the following: org.json.simple.JSONObject,
63    * org.json.simple.JSONArray, java.lang.String, java.lang.Number,
64    * java.lang.Boolean, null
65    *
66    * @deprecated this method may throw an {@code Error} instead of returning
67    * {@code null}; please use
68    * {@link JSONValue#parseWithException(String)} instead
69    */
 
70  0 toggle @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    * Parse JSON text into java object from the input source.
79    *
80    * @see org.json.simple.parser.JSONParser
81    *
82    * @param in
83    * @return Instance of the following: org.json.simple.JSONObject,
84    * org.json.simple.JSONArray, java.lang.String, java.lang.Number,
85    * java.lang.Boolean, null
86    *
87    * @throws IOException
88    * @throws ParseException
89    */
 
90  0 toggle 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   
 
97  0 toggle 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    * Encode an object into JSON text and write it to out.
105    * <p>
106    * If this object is a Map or a List, and it's also a JSONStreamAware or a
107    * JSONAware, JSONStreamAware or JSONAware will be considered firstly.
108    * <p>
109    * DO NOT call this method from writeJSONString(Writer) of a class that
110    * implements both JSONStreamAware and (Map or List) with "this" as the first
111    * parameter, use JSONObject.writeJSONString(Map, Writer) or
112    * JSONArray.writeJSONString(List, Writer) instead.
113    *
114    * @see org.json.simple.JSONObject#writeJSONString(Map, Writer)
115    * @see org.json.simple.JSONArray#writeJSONString(List, Writer)
116    *
117    * @param value
118    * @param writer
119    */
 
120  8288 toggle public static void writeJSONString(Object value, Writer out)
121    throws IOException
122    {
123  8288 if (value == null)
124    {
125  0 out.write("null");
126  0 return;
127    }
128   
129  8288 if (value instanceof String)
130    {
131  8288 out.write('\"');
132  8288 out.write(escape((String) value));
133  8288 out.write('\"');
134  8288 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    * Convert an object to JSON text.
250    * <p>
251    * If this object is a Map or a List, and it's also a JSONAware, JSONAware
252    * will be considered firstly.
253    * <p>
254    * DO NOT call this method from toJSONString() of a class that implements both
255    * JSONAware and Map or List with "this" as the parameter, use
256    * JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead.
257    *
258    * @see org.json.simple.JSONObject#toJSONString(Map)
259    * @see org.json.simple.JSONArray#toJSONString(List)
260    *
261    * @param value
262    * @return JSON text, or "null" if value is null or it's an NaN or an INF
263    * number.
264    */
 
265  0 toggle 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    // This should never happen for a StringWriter
276  0 throw new RuntimeException(e);
277    }
278    }
279   
280    /**
281    * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
282    * (U+0000 through U+001F).
283    *
284    * @param s
285    * @return
286    */
 
287  8288 toggle public static String escape(String s)
288    {
289  8288 if (s == null)
290  0 return null;
291  8288 StringBuffer sb = new StringBuffer();
292  8288 escape(s, sb);
293  8288 return sb.toString();
294    }
295   
296    /**
297    * @param s
298    * - Must not be null.
299    * @param sb
300    */
 
301  8288 toggle static void escape(String s, StringBuffer sb)
302    {
303  8288 final int len = s.length();
304  150664 for (int i = 0; i < len; i++)
305    {
306  142376 char ch = s.charAt(i);
307  142376 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  142376 default:
334    // Reference: http://www.unicode.org/versions/Unicode5.1.0/
335  142376 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  142376 sb.append(ch);
350    }
351    }
352    } // for
353    }
354   
355    }