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)}
45 public static Object parse(Reader in){
47 JSONParser parser=new JSONParser();
48 return parser.parse(in);
56 * Parse JSON text into java object from the given string.
57 * Please use parseWithException() if you don't want to ignore the exception.
59 * @see org.json.simple.parser.JSONParser#parse(Reader)
60 * @see #parseWithException(Reader)
63 * @return Instance of the following:
64 * org.json.simple.JSONObject,
65 * org.json.simple.JSONArray,
71 * @deprecated this method may throw an {@code Error} instead of returning
72 * {@code null}; please use {@link JSONValue#parseWithException(String)}
76 public static Object parse(String s){
77 StringReader in=new StringReader(s);
82 * Parse JSON text into java object from the input source.
84 * @see org.json.simple.parser.JSONParser
87 * @return Instance of the following:
88 * org.json.simple.JSONObject,
89 * org.json.simple.JSONArray,
96 * @throws ParseException
98 public static Object parseWithException(Reader in) throws IOException, ParseException{
99 JSONParser parser=new JSONParser();
100 return parser.parse(in);
103 public static Object parseWithException(String s) throws ParseException{
104 JSONParser parser=new JSONParser();
105 return parser.parse(s);
109 * Encode an object into JSON text and write it to out.
111 * 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.
113 * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with
114 * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead.
116 * @see org.json.simple.JSONObject#writeJSONString(Map, Writer)
117 * @see org.json.simple.JSONArray#writeJSONString(List, Writer)
122 public static void writeJSONString(Object value, Writer out) throws IOException {
128 if(value instanceof String){
130 out.write(escape((String)value));
135 if(value instanceof Double){
136 if(((Double)value).isInfinite() || ((Double)value).isNaN())
139 out.write(value.toString());
143 if(value instanceof Float){
144 if(((Float)value).isInfinite() || ((Float)value).isNaN())
147 out.write(value.toString());
151 if(value instanceof Number){
152 out.write(value.toString());
156 if(value instanceof Boolean){
157 out.write(value.toString());
161 if((value instanceof JSONStreamAware)){
162 ((JSONStreamAware)value).writeJSONString(out);
166 if((value instanceof JSONAware)){
167 out.write(((JSONAware)value).toJSONString());
171 if(value instanceof Map){
172 JSONObject.writeJSONString((Map)value, out);
176 if(value instanceof Collection){
177 JSONArray.writeJSONString((Collection)value, out);
181 if(value instanceof byte[]){
182 JSONArray.writeJSONString((byte[])value, out);
186 if(value instanceof short[]){
187 JSONArray.writeJSONString((short[])value, out);
191 if(value instanceof int[]){
192 JSONArray.writeJSONString((int[])value, out);
196 if(value instanceof long[]){
197 JSONArray.writeJSONString((long[])value, out);
201 if(value instanceof float[]){
202 JSONArray.writeJSONString((float[])value, out);
206 if(value instanceof double[]){
207 JSONArray.writeJSONString((double[])value, out);
211 if(value instanceof boolean[]){
212 JSONArray.writeJSONString((boolean[])value, out);
216 if(value instanceof char[]){
217 JSONArray.writeJSONString((char[])value, out);
221 if(value instanceof Object[]){
222 JSONArray.writeJSONString((Object[])value, out);
226 out.write(value.toString());
230 * Convert an object to JSON text.
232 * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly.
234 * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with
235 * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead.
237 * @see org.json.simple.JSONObject#toJSONString(Map)
238 * @see org.json.simple.JSONArray#toJSONString(List)
241 * @return JSON text, or "null" if value is null or it's an NaN or an INF number.
243 public static String toJSONString(Object value){
244 final StringWriter writer = new StringWriter();
247 writeJSONString(value, writer);
248 return writer.toString();
249 } catch(IOException e){
250 // This should never happen for a StringWriter
251 throw new RuntimeException(e);
256 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
260 public static String escape(String s){
263 StringBuffer sb = new StringBuffer();
265 return sb.toString();
269 * @param s - Must not be null.
272 static void escape(String s, StringBuffer sb) {
273 final int len = s.length();
274 for(int i=0;i<len;i++){
302 //Reference: http://www.unicode.org/versions/Unicode5.1.0/
303 if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
304 String ss=Integer.toHexString(ch);
306 for(int k=0;k<4-ss.length();k++){
309 sb.append(ss.toUpperCase());