2 * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
5 package org.json.simple;
7 import java.io.IOException;
9 import java.io.StringReader;
10 import java.io.StringWriter;
11 import java.io.Writer;
12 import java.util.Collection;
13 // import java.util.List;
16 import org.json.simple.parser.JSONParser;
17 import org.json.simple.parser.ParseException;
21 * @author FangYidong<fangyidong@yahoo.com.cn>
23 public class JSONValue {
25 * Parse JSON text into java object from the input source.
26 * Please use parseWithException() if you don't want to ignore the exception.
28 * @see org.json.simple.parser.JSONParser#parse(Reader)
29 * @see #parseWithException(Reader)
32 * @return Instance of the following:
33 * org.json.simple.JSONObject,
34 * org.json.simple.JSONArray,
40 * @deprecated this method may throw an {@code Error} instead of returning
41 * {@code null}; please use {@link JSONValue#parseWithException(Reader)}
44 public static Object parse(Reader in){
46 JSONParser parser=new JSONParser();
47 return parser.parse(in);
55 * Parse JSON text into java object from the given string.
56 * Please use parseWithException() if you don't want to ignore the exception.
58 * @see org.json.simple.parser.JSONParser#parse(Reader)
59 * @see #parseWithException(Reader)
62 * @return Instance of the following:
63 * org.json.simple.JSONObject,
64 * org.json.simple.JSONArray,
70 * @deprecated this method may throw an {@code Error} instead of returning
71 * {@code null}; please use {@link JSONValue#parseWithException(String)}
74 public static Object parse(String s){
75 StringReader in=new StringReader(s);
80 * Parse JSON text into java object from the input source.
82 * @see org.json.simple.parser.JSONParser
85 * @return Instance of the following:
86 * org.json.simple.JSONObject,
87 * org.json.simple.JSONArray,
94 * @throws ParseException
96 public static Object parseWithException(Reader in) throws IOException, ParseException{
97 JSONParser parser=new JSONParser();
98 return parser.parse(in);
101 public static Object parseWithException(String s) throws ParseException{
102 JSONParser parser=new JSONParser();
103 return parser.parse(s);
107 * Encode an object into JSON text and write it to out.
109 * 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.
111 * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with
112 * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead.
114 * @see org.json.simple.JSONObject#writeJSONString(Map, Writer)
115 * @see org.json.simple.JSONArray#writeJSONString(List, Writer)
120 public static void writeJSONString(Object value, Writer out) throws IOException {
126 if(value instanceof String){
128 out.write(escape((String)value));
133 if(value instanceof Double){
134 if(((Double)value).isInfinite() || ((Double)value).isNaN())
137 out.write(value.toString());
141 if(value instanceof Float){
142 if(((Float)value).isInfinite() || ((Float)value).isNaN())
145 out.write(value.toString());
149 if(value instanceof Number){
150 out.write(value.toString());
154 if(value instanceof Boolean){
155 out.write(value.toString());
159 if((value instanceof JSONStreamAware)){
160 ((JSONStreamAware)value).writeJSONString(out);
164 if((value instanceof JSONAware)){
165 out.write(((JSONAware)value).toJSONString());
169 if(value instanceof Map){
170 JSONObject.writeJSONString((Map)value, out);
174 if(value instanceof Collection){
175 JSONArray.writeJSONString((Collection)value, out);
179 if(value instanceof byte[]){
180 JSONArray.writeJSONString((byte[])value, out);
184 if(value instanceof short[]){
185 JSONArray.writeJSONString((short[])value, out);
189 if(value instanceof int[]){
190 JSONArray.writeJSONString((int[])value, out);
194 if(value instanceof long[]){
195 JSONArray.writeJSONString((long[])value, out);
199 if(value instanceof float[]){
200 JSONArray.writeJSONString((float[])value, out);
204 if(value instanceof double[]){
205 JSONArray.writeJSONString((double[])value, out);
209 if(value instanceof boolean[]){
210 JSONArray.writeJSONString((boolean[])value, out);
214 if(value instanceof char[]){
215 JSONArray.writeJSONString((char[])value, out);
219 if(value instanceof Object[]){
220 JSONArray.writeJSONString((Object[])value, out);
224 out.write(value.toString());
228 * Convert an object to JSON text.
230 * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly.
232 * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with
233 * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead.
235 * @see org.json.simple.JSONObject#toJSONString(Map)
236 * @see org.json.simple.JSONArray#toJSONString(List)
239 * @return JSON text, or "null" if value is null or it's an NaN or an INF number.
241 public static String toJSONString(Object value){
242 final StringWriter writer = new StringWriter();
245 writeJSONString(value, writer);
246 return writer.toString();
247 } catch(IOException e){
248 // This should never happen for a StringWriter
249 throw new RuntimeException(e);
254 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
258 public static String escape(String s){
261 StringBuffer sb = new StringBuffer();
263 return sb.toString();
267 * @param s - Must not be null.
270 static void escape(String s, StringBuffer sb) {
271 final int len = s.length();
272 for(int i=0;i<len;i++){
300 //Reference: http://www.unicode.org/versions/Unicode5.1.0/
301 if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
302 String ss=Integer.toHexString(ch);
304 for(int k=0;k<4-ss.length();k++){
307 sb.append(ss.toUpperCase());