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;
22 * @author FangYidong<fangyidong@yahoo.com.cn>
24 public class JSONValue {
26 * Parse JSON text into java object from the input source.
27 * Please use 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:
34 * org.json.simple.JSONObject,
35 * org.json.simple.JSONArray,
41 * @deprecated this method may throw an {@code Error} instead of returning
42 * {@code null}; please use {@link JSONValue#parseWithException(Reader)}
46 public static Object parse(Reader in){
48 JSONParser parser=new JSONParser();
49 return parser.parse(in);
57 * Parse JSON text into java object from the given string.
58 * Please use parseWithException() if you don't want to ignore the exception.
60 * @see org.json.simple.parser.JSONParser#parse(Reader)
61 * @see #parseWithException(Reader)
64 * @return Instance of the following:
65 * org.json.simple.JSONObject,
66 * org.json.simple.JSONArray,
72 * @deprecated this method may throw an {@code Error} instead of returning
73 * {@code null}; please use {@link JSONValue#parseWithException(String)}
77 public static Object parse(String s){
78 StringReader in=new StringReader(s);
83 * Parse JSON text into java object from the input source.
85 * @see org.json.simple.parser.JSONParser
88 * @return Instance of the following:
89 * org.json.simple.JSONObject,
90 * org.json.simple.JSONArray,
97 * @throws ParseException
99 public static Object parseWithException(Reader in) throws IOException, ParseException{
100 JSONParser parser=new JSONParser();
101 return parser.parse(in);
104 public static Object parseWithException(String s) throws ParseException{
105 JSONParser parser=new JSONParser();
106 return parser.parse(s);
110 * Encode an object into JSON text and write it to out.
112 * 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.
114 * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with
115 * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead.
117 * @see org.json.simple.JSONObject#writeJSONString(Map, Writer)
118 * @see org.json.simple.JSONArray#writeJSONString(List, Writer)
123 public static void writeJSONString(Object value, Writer out) throws IOException {
129 if(value instanceof String){
131 out.write(escape((String)value));
136 if(value instanceof Double){
137 if(((Double)value).isInfinite() || ((Double)value).isNaN())
140 out.write(value.toString());
144 if(value instanceof Float){
145 if(((Float)value).isInfinite() || ((Float)value).isNaN())
148 out.write(value.toString());
152 if(value instanceof Number){
153 out.write(value.toString());
157 if(value instanceof Boolean){
158 out.write(value.toString());
162 if((value instanceof JSONStreamAware)){
163 ((JSONStreamAware)value).writeJSONString(out);
167 if((value instanceof JSONAware)){
168 out.write(((JSONAware)value).toJSONString());
172 if(value instanceof Map){
173 JSONObject.writeJSONString((Map)value, out);
177 if(value instanceof Collection){
178 JSONArray.writeJSONString((Collection)value, out);
182 if(value instanceof byte[]){
183 JSONArray.writeJSONString((byte[])value, out);
187 if(value instanceof short[]){
188 JSONArray.writeJSONString((short[])value, out);
192 if(value instanceof int[]){
193 JSONArray.writeJSONString((int[])value, out);
197 if(value instanceof long[]){
198 JSONArray.writeJSONString((long[])value, out);
202 if(value instanceof float[]){
203 JSONArray.writeJSONString((float[])value, out);
207 if(value instanceof double[]){
208 JSONArray.writeJSONString((double[])value, out);
212 if(value instanceof boolean[]){
213 JSONArray.writeJSONString((boolean[])value, out);
217 if(value instanceof char[]){
218 JSONArray.writeJSONString((char[])value, out);
222 if(value instanceof Object[]){
223 JSONArray.writeJSONString((Object[])value, out);
227 out.write(value.toString());
231 * Convert an object to JSON text.
233 * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly.
235 * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with
236 * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead.
238 * @see org.json.simple.JSONObject#toJSONString(Map)
239 * @see org.json.simple.JSONArray#toJSONString(List)
242 * @return JSON text, or "null" if value is null or it's an NaN or an INF number.
244 public static String toJSONString(Object value){
245 final StringWriter writer = new StringWriter();
248 writeJSONString(value, writer);
249 return writer.toString();
250 } catch(IOException e){
251 // This should never happen for a StringWriter
252 throw new RuntimeException(e);
257 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
261 public static String escape(String s){
264 StringBuffer sb = new StringBuffer();
266 return sb.toString();
270 * @param s - Must not be null.
273 static void escape(String s, StringBuffer sb) {
274 final int len = s.length();
275 for(int i=0;i<len;i++){
303 //Reference: http://www.unicode.org/versions/Unicode5.1.0/
304 if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
305 String ss=Integer.toHexString(ch);
307 for(int k=0;k<4-ss.length();k++){
310 sb.append(ss.toUpperCase(Locale.ROOT));