|
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.Writer; |
|
11 import java.util.List; |
|
12 import java.util.Map; |
|
13 |
|
14 import org.json.simple.parser.JSONParser; |
|
15 import org.json.simple.parser.ParseException; |
|
16 |
|
17 |
|
18 /** |
|
19 * @author FangYidong<fangyidong@yahoo.com.cn> |
|
20 */ |
|
21 public class JSONValue { |
|
22 /** |
|
23 * Parse JSON text into java object from the input source. |
|
24 * Please use parseWithException() if you don't want to ignore the exception. |
|
25 * |
|
26 * @see org.json.simple.parser.JSONParser#parse(Reader) |
|
27 * @see #parseWithException(Reader) |
|
28 * |
|
29 * @param in |
|
30 * @return Instance of the following: |
|
31 * org.json.simple.JSONObject, |
|
32 * org.json.simple.JSONArray, |
|
33 * java.lang.String, |
|
34 * java.lang.Number, |
|
35 * java.lang.Boolean, |
|
36 * null |
|
37 * |
|
38 */ |
|
39 public static Object parse(Reader in){ |
|
40 try{ |
|
41 JSONParser parser=new JSONParser(); |
|
42 return parser.parse(in); |
|
43 } |
|
44 catch(Exception e){ |
|
45 return null; |
|
46 } |
|
47 } |
|
48 |
|
49 public static Object parse(String s){ |
|
50 StringReader in=new StringReader(s); |
|
51 return parse(in); |
|
52 } |
|
53 |
|
54 /** |
|
55 * Parse JSON text into java object from the input source. |
|
56 * |
|
57 * @see org.json.simple.parser.JSONParser |
|
58 * |
|
59 * @param in |
|
60 * @return Instance of the following: |
|
61 * org.json.simple.JSONObject, |
|
62 * org.json.simple.JSONArray, |
|
63 * java.lang.String, |
|
64 * java.lang.Number, |
|
65 * java.lang.Boolean, |
|
66 * null |
|
67 * |
|
68 * @throws IOException |
|
69 * @throws ParseException |
|
70 */ |
|
71 public static Object parseWithException(Reader in) throws IOException, ParseException{ |
|
72 JSONParser parser=new JSONParser(); |
|
73 return parser.parse(in); |
|
74 } |
|
75 |
|
76 public static Object parseWithException(String s) throws ParseException{ |
|
77 JSONParser parser=new JSONParser(); |
|
78 return parser.parse(s); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Encode an object into JSON text and write it to out. |
|
83 * <p> |
|
84 * If this object is a Map or a List, and it's also a JSONStreamAware or a JSONAware, JSONStreamAware or JSONAware will be considered firstly. |
|
85 * <p> |
|
86 * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with |
|
87 * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead. |
|
88 * |
|
89 * @see org.json.simple.JSONObject#writeJSONString(Map, Writer) |
|
90 * @see org.json.simple.JSONArray#writeJSONString(List, Writer) |
|
91 * |
|
92 * @param value |
|
93 * @param writer |
|
94 */ |
|
95 public static void writeJSONString(Object value, Writer out) throws IOException { |
|
96 if(value == null){ |
|
97 out.write("null"); |
|
98 return; |
|
99 } |
|
100 |
|
101 if(value instanceof String){ |
|
102 out.write('\"'); |
|
103 out.write(escape((String)value)); |
|
104 out.write('\"'); |
|
105 return; |
|
106 } |
|
107 |
|
108 if(value instanceof Double){ |
|
109 if(((Double)value).isInfinite() || ((Double)value).isNaN()) |
|
110 out.write("null"); |
|
111 else |
|
112 out.write(value.toString()); |
|
113 return; |
|
114 } |
|
115 |
|
116 if(value instanceof Float){ |
|
117 if(((Float)value).isInfinite() || ((Float)value).isNaN()) |
|
118 out.write("null"); |
|
119 else |
|
120 out.write(value.toString()); |
|
121 return; |
|
122 } |
|
123 |
|
124 if(value instanceof Number){ |
|
125 out.write(value.toString()); |
|
126 return; |
|
127 } |
|
128 |
|
129 if(value instanceof Boolean){ |
|
130 out.write(value.toString()); |
|
131 return; |
|
132 } |
|
133 |
|
134 if((value instanceof JSONStreamAware)){ |
|
135 ((JSONStreamAware)value).writeJSONString(out); |
|
136 return; |
|
137 } |
|
138 |
|
139 if((value instanceof JSONAware)){ |
|
140 out.write(((JSONAware)value).toJSONString()); |
|
141 return; |
|
142 } |
|
143 |
|
144 if(value instanceof Map){ |
|
145 JSONObject.writeJSONString((Map)value, out); |
|
146 return; |
|
147 } |
|
148 |
|
149 if(value instanceof List){ |
|
150 JSONArray.writeJSONString((List)value, out); |
|
151 return; |
|
152 } |
|
153 |
|
154 out.write(value.toString()); |
|
155 } |
|
156 |
|
157 /** |
|
158 * Convert an object to JSON text. |
|
159 * <p> |
|
160 * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly. |
|
161 * <p> |
|
162 * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with |
|
163 * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead. |
|
164 * |
|
165 * @see org.json.simple.JSONObject#toJSONString(Map) |
|
166 * @see org.json.simple.JSONArray#toJSONString(List) |
|
167 * |
|
168 * @param value |
|
169 * @return JSON text, or "null" if value is null or it's an NaN or an INF number. |
|
170 */ |
|
171 public static String toJSONString(Object value){ |
|
172 if(value == null) |
|
173 return "null"; |
|
174 |
|
175 if(value instanceof String) |
|
176 return "\""+escape((String)value)+"\""; |
|
177 |
|
178 if(value instanceof Double){ |
|
179 if(((Double)value).isInfinite() || ((Double)value).isNaN()) |
|
180 return "null"; |
|
181 else |
|
182 return value.toString(); |
|
183 } |
|
184 |
|
185 if(value instanceof Float){ |
|
186 if(((Float)value).isInfinite() || ((Float)value).isNaN()) |
|
187 return "null"; |
|
188 else |
|
189 return value.toString(); |
|
190 } |
|
191 |
|
192 if(value instanceof Number) |
|
193 return value.toString(); |
|
194 |
|
195 if(value instanceof Boolean) |
|
196 return value.toString(); |
|
197 |
|
198 if((value instanceof JSONAware)) |
|
199 return ((JSONAware)value).toJSONString(); |
|
200 |
|
201 if(value instanceof Map) |
|
202 return JSONObject.toJSONString((Map)value); |
|
203 |
|
204 if(value instanceof List) |
|
205 return JSONArray.toJSONString((List)value); |
|
206 |
|
207 return value.toString(); |
|
208 } |
|
209 |
|
210 /** |
|
211 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). |
|
212 * @param s |
|
213 * @return |
|
214 */ |
|
215 public static String escape(String s){ |
|
216 if(s==null) |
|
217 return null; |
|
218 StringBuffer sb = new StringBuffer(); |
|
219 escape(s, sb); |
|
220 return sb.toString(); |
|
221 } |
|
222 |
|
223 /** |
|
224 * @param s - Must not be null. |
|
225 * @param sb |
|
226 */ |
|
227 static void escape(String s, StringBuffer sb) { |
|
228 for(int i=0;i<s.length();i++){ |
|
229 char ch=s.charAt(i); |
|
230 switch(ch){ |
|
231 case '"': |
|
232 sb.append("\\\""); |
|
233 break; |
|
234 case '\\': |
|
235 sb.append("\\\\"); |
|
236 break; |
|
237 case '\b': |
|
238 sb.append("\\b"); |
|
239 break; |
|
240 case '\f': |
|
241 sb.append("\\f"); |
|
242 break; |
|
243 case '\n': |
|
244 sb.append("\\n"); |
|
245 break; |
|
246 case '\r': |
|
247 sb.append("\\r"); |
|
248 break; |
|
249 case '\t': |
|
250 sb.append("\\t"); |
|
251 break; |
|
252 case '/': |
|
253 sb.append("\\/"); |
|
254 break; |
|
255 default: |
|
256 //Reference: http://www.unicode.org/versions/Unicode5.1.0/ |
|
257 if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){ |
|
258 String ss=Integer.toHexString(ch); |
|
259 sb.append("\\u"); |
|
260 for(int k=0;k<4-ss.length();k++){ |
|
261 sb.append('0'); |
|
262 } |
|
263 sb.append(ss.toUpperCase()); |
|
264 } |
|
265 else{ |
|
266 sb.append(ch); |
|
267 } |
|
268 } |
|
269 }//for |
|
270 } |
|
271 |
|
272 } |