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