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.Locale;
14 // import java.util.List;
17 import org.json.simple.parser.JSONParser;
18 import org.json.simple.parser.ParseException;
21 * @author FangYidong<fangyidong@yahoo.com.cn>
23 public class JSONValue
26 * Parse JSON text into java object from the input source. Please use
27 * parseWithException() if you don't want to ignore the exception.
29 * @see org.json.simple.parser.JSONParser#parse(Reader)
30 * @see #parseWithException(Reader)
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
37 * @deprecated this method may throw an {@code Error} instead of returning
38 * {@code null}; please use
39 * {@link JSONValue#parseWithException(Reader)} instead
42 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. Please use
56 * 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: org.json.simple.JSONObject,
63 * org.json.simple.JSONArray, java.lang.String, java.lang.Number,
64 * java.lang.Boolean, null
66 * @deprecated this method may throw an {@code Error} instead of returning
67 * {@code null}; please use
68 * {@link JSONValue#parseWithException(String)} instead
71 public static Object parse(String s)
73 StringReader in = new StringReader(s);
78 * Parse JSON text into java object from the input source.
80 * @see org.json.simple.parser.JSONParser
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
88 * @throws ParseException
90 public static Object parseWithException(Reader in)
91 throws IOException, ParseException
93 JSONParser parser = new JSONParser();
94 return parser.parse(in);
97 public static Object parseWithException(String s) throws ParseException
99 JSONParser parser = new JSONParser();
100 return parser.parse(s);
104 * Encode an object into JSON text and write it to out.
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.
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.
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)
129 if (value instanceof String)
132 out.write(escape((String) value));
137 if (value instanceof Double)
139 if (((Double) value).isInfinite() || ((Double) value).isNaN())
142 out.write(value.toString());
146 if (value instanceof Float)
148 if (((Float) value).isInfinite() || ((Float) value).isNaN())
151 out.write(value.toString());
155 if (value instanceof Number)
157 out.write(value.toString());
161 if (value instanceof Boolean)
163 out.write(value.toString());
167 if ((value instanceof JSONStreamAware))
169 ((JSONStreamAware) value).writeJSONString(out);
173 if ((value instanceof JSONAware))
175 out.write(((JSONAware) value).toJSONString());
179 if (value instanceof Map)
181 JSONObject.writeJSONString((Map) value, out);
185 if (value instanceof Collection)
187 JSONArray.writeJSONString((Collection) value, out);
191 if (value instanceof byte[])
193 JSONArray.writeJSONString((byte[]) value, out);
197 if (value instanceof short[])
199 JSONArray.writeJSONString((short[]) value, out);
203 if (value instanceof int[])
205 JSONArray.writeJSONString((int[]) value, out);
209 if (value instanceof long[])
211 JSONArray.writeJSONString((long[]) value, out);
215 if (value instanceof float[])
217 JSONArray.writeJSONString((float[]) value, out);
221 if (value instanceof double[])
223 JSONArray.writeJSONString((double[]) value, out);
227 if (value instanceof boolean[])
229 JSONArray.writeJSONString((boolean[]) value, out);
233 if (value instanceof char[])
235 JSONArray.writeJSONString((char[]) value, out);
239 if (value instanceof Object[])
241 JSONArray.writeJSONString((Object[]) value, out);
245 out.write(value.toString());
249 * Convert an object to JSON text.
251 * If this object is a Map or a List, and it's also a JSONAware, JSONAware
252 * will be considered firstly.
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.
258 * @see org.json.simple.JSONObject#toJSONString(Map)
259 * @see org.json.simple.JSONArray#toJSONString(List)
262 * @return JSON text, or "null" if value is null or it's an NaN or an INF
265 public static String toJSONString(Object value)
267 final StringWriter writer = new StringWriter();
271 writeJSONString(value, writer);
272 return writer.toString();
273 } catch (IOException e)
275 // This should never happen for a StringWriter
276 throw new RuntimeException(e);
281 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
282 * (U+0000 through U+001F).
287 public static String escape(String s)
291 StringBuffer sb = new StringBuffer();
293 return sb.toString();
298 * - Must not be null.
301 static void escape(String s, StringBuffer sb)
303 final int len = s.length();
304 for (int i = 0; i < len; i++)
306 char ch = s.charAt(i);
334 // Reference: http://www.unicode.org/versions/Unicode5.1.0/
335 if ((ch >= '\u0000' && ch <= '\u001F')
336 || (ch >= '\u007F' && ch <= '\u009F')
337 || (ch >= '\u2000' && ch <= '\u20FF'))
339 String ss = Integer.toHexString(ch);
341 for (int k = 0; k < 4 - ss.length(); k++)
345 sb.append(ss.toUpperCase(Locale.ROOT));