michael@0: /* michael@0: * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $ michael@0: * Created on 2006-4-15 michael@0: */ michael@0: package org.json.simple; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.Reader; michael@0: import java.io.StringReader; michael@0: import java.io.Writer; michael@0: import java.util.List; michael@0: import java.util.Map; michael@0: michael@0: import org.json.simple.parser.JSONParser; michael@0: import org.json.simple.parser.ParseException; michael@0: michael@0: michael@0: /** michael@0: * @author FangYidong michael@0: */ michael@0: public class JSONValue { michael@0: /** michael@0: * Parse JSON text into java object from the input source. michael@0: * Please use parseWithException() if you don't want to ignore the exception. michael@0: * michael@0: * @see org.json.simple.parser.JSONParser#parse(Reader) michael@0: * @see #parseWithException(Reader) michael@0: * michael@0: * @param in michael@0: * @return Instance of the following: michael@0: * org.json.simple.JSONObject, michael@0: * org.json.simple.JSONArray, michael@0: * java.lang.String, michael@0: * java.lang.Number, michael@0: * java.lang.Boolean, michael@0: * null michael@0: * michael@0: */ michael@0: public static Object parse(Reader in){ michael@0: try{ michael@0: JSONParser parser=new JSONParser(); michael@0: return parser.parse(in); michael@0: } michael@0: catch(Exception e){ michael@0: return null; michael@0: } michael@0: } michael@0: michael@0: public static Object parse(String s){ michael@0: StringReader in=new StringReader(s); michael@0: return parse(in); michael@0: } michael@0: michael@0: /** michael@0: * Parse JSON text into java object from the input source. michael@0: * michael@0: * @see org.json.simple.parser.JSONParser michael@0: * michael@0: * @param in michael@0: * @return Instance of the following: michael@0: * org.json.simple.JSONObject, michael@0: * org.json.simple.JSONArray, michael@0: * java.lang.String, michael@0: * java.lang.Number, michael@0: * java.lang.Boolean, michael@0: * null michael@0: * michael@0: * @throws IOException michael@0: * @throws ParseException michael@0: */ michael@0: public static Object parseWithException(Reader in) throws IOException, ParseException{ michael@0: JSONParser parser=new JSONParser(); michael@0: return parser.parse(in); michael@0: } michael@0: michael@0: public static Object parseWithException(String s) throws ParseException{ michael@0: JSONParser parser=new JSONParser(); michael@0: return parser.parse(s); michael@0: } michael@0: michael@0: /** michael@0: * Encode an object into JSON text and write it to out. michael@0: *

michael@0: * 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. michael@0: *

michael@0: * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with michael@0: * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead. michael@0: * michael@0: * @see org.json.simple.JSONObject#writeJSONString(Map, Writer) michael@0: * @see org.json.simple.JSONArray#writeJSONString(List, Writer) michael@0: * michael@0: * @param value michael@0: * @param writer michael@0: */ michael@0: public static void writeJSONString(Object value, Writer out) throws IOException { michael@0: if(value == null){ michael@0: out.write("null"); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof String){ michael@0: out.write('\"'); michael@0: out.write(escape((String)value)); michael@0: out.write('\"'); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof Double){ michael@0: if(((Double)value).isInfinite() || ((Double)value).isNaN()) michael@0: out.write("null"); michael@0: else michael@0: out.write(value.toString()); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof Float){ michael@0: if(((Float)value).isInfinite() || ((Float)value).isNaN()) michael@0: out.write("null"); michael@0: else michael@0: out.write(value.toString()); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof Number){ michael@0: out.write(value.toString()); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof Boolean){ michael@0: out.write(value.toString()); michael@0: return; michael@0: } michael@0: michael@0: if((value instanceof JSONStreamAware)){ michael@0: ((JSONStreamAware)value).writeJSONString(out); michael@0: return; michael@0: } michael@0: michael@0: if((value instanceof JSONAware)){ michael@0: out.write(((JSONAware)value).toJSONString()); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof Map){ michael@0: JSONObject.writeJSONString((Map)value, out); michael@0: return; michael@0: } michael@0: michael@0: if(value instanceof List){ michael@0: JSONArray.writeJSONString((List)value, out); michael@0: return; michael@0: } michael@0: michael@0: out.write(value.toString()); michael@0: } michael@0: michael@0: /** michael@0: * Convert an object to JSON text. michael@0: *

michael@0: * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly. michael@0: *

michael@0: * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with michael@0: * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead. michael@0: * michael@0: * @see org.json.simple.JSONObject#toJSONString(Map) michael@0: * @see org.json.simple.JSONArray#toJSONString(List) michael@0: * michael@0: * @param value michael@0: * @return JSON text, or "null" if value is null or it's an NaN or an INF number. michael@0: */ michael@0: public static String toJSONString(Object value){ michael@0: if(value == null) michael@0: return "null"; michael@0: michael@0: if(value instanceof String) michael@0: return "\""+escape((String)value)+"\""; michael@0: michael@0: if(value instanceof Double){ michael@0: if(((Double)value).isInfinite() || ((Double)value).isNaN()) michael@0: return "null"; michael@0: else michael@0: return value.toString(); michael@0: } michael@0: michael@0: if(value instanceof Float){ michael@0: if(((Float)value).isInfinite() || ((Float)value).isNaN()) michael@0: return "null"; michael@0: else michael@0: return value.toString(); michael@0: } michael@0: michael@0: if(value instanceof Number) michael@0: return value.toString(); michael@0: michael@0: if(value instanceof Boolean) michael@0: return value.toString(); michael@0: michael@0: if((value instanceof JSONAware)) michael@0: return ((JSONAware)value).toJSONString(); michael@0: michael@0: if(value instanceof Map) michael@0: return JSONObject.toJSONString((Map)value); michael@0: michael@0: if(value instanceof List) michael@0: return JSONArray.toJSONString((List)value); michael@0: michael@0: return value.toString(); michael@0: } michael@0: michael@0: /** michael@0: * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). michael@0: * @param s michael@0: * @return michael@0: */ michael@0: public static String escape(String s){ michael@0: if(s==null) michael@0: return null; michael@0: StringBuffer sb = new StringBuffer(); michael@0: escape(s, sb); michael@0: return sb.toString(); michael@0: } michael@0: michael@0: /** michael@0: * @param s - Must not be null. michael@0: * @param sb michael@0: */ michael@0: static void escape(String s, StringBuffer sb) { michael@0: for(int i=0;i='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){ michael@0: String ss=Integer.toHexString(ch); michael@0: sb.append("\\u"); michael@0: for(int k=0;k<4-ss.length();k++){ michael@0: sb.append('0'); michael@0: } michael@0: sb.append(ss.toUpperCase()); michael@0: } michael@0: else{ michael@0: sb.append(ch); michael@0: } michael@0: } michael@0: }//for michael@0: } michael@0: michael@0: }