1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/org/json/simple/JSONValue.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,272 @@ 1.4 +/* 1.5 + * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $ 1.6 + * Created on 2006-4-15 1.7 + */ 1.8 +package org.json.simple; 1.9 + 1.10 +import java.io.IOException; 1.11 +import java.io.Reader; 1.12 +import java.io.StringReader; 1.13 +import java.io.Writer; 1.14 +import java.util.List; 1.15 +import java.util.Map; 1.16 + 1.17 +import org.json.simple.parser.JSONParser; 1.18 +import org.json.simple.parser.ParseException; 1.19 + 1.20 + 1.21 +/** 1.22 + * @author FangYidong<fangyidong@yahoo.com.cn> 1.23 + */ 1.24 +public class JSONValue { 1.25 + /** 1.26 + * Parse JSON text into java object from the input source. 1.27 + * Please use parseWithException() if you don't want to ignore the exception. 1.28 + * 1.29 + * @see org.json.simple.parser.JSONParser#parse(Reader) 1.30 + * @see #parseWithException(Reader) 1.31 + * 1.32 + * @param in 1.33 + * @return Instance of the following: 1.34 + * org.json.simple.JSONObject, 1.35 + * org.json.simple.JSONArray, 1.36 + * java.lang.String, 1.37 + * java.lang.Number, 1.38 + * java.lang.Boolean, 1.39 + * null 1.40 + * 1.41 + */ 1.42 + public static Object parse(Reader in){ 1.43 + try{ 1.44 + JSONParser parser=new JSONParser(); 1.45 + return parser.parse(in); 1.46 + } 1.47 + catch(Exception e){ 1.48 + return null; 1.49 + } 1.50 + } 1.51 + 1.52 + public static Object parse(String s){ 1.53 + StringReader in=new StringReader(s); 1.54 + return parse(in); 1.55 + } 1.56 + 1.57 + /** 1.58 + * Parse JSON text into java object from the input source. 1.59 + * 1.60 + * @see org.json.simple.parser.JSONParser 1.61 + * 1.62 + * @param in 1.63 + * @return Instance of the following: 1.64 + * org.json.simple.JSONObject, 1.65 + * org.json.simple.JSONArray, 1.66 + * java.lang.String, 1.67 + * java.lang.Number, 1.68 + * java.lang.Boolean, 1.69 + * null 1.70 + * 1.71 + * @throws IOException 1.72 + * @throws ParseException 1.73 + */ 1.74 + public static Object parseWithException(Reader in) throws IOException, ParseException{ 1.75 + JSONParser parser=new JSONParser(); 1.76 + return parser.parse(in); 1.77 + } 1.78 + 1.79 + public static Object parseWithException(String s) throws ParseException{ 1.80 + JSONParser parser=new JSONParser(); 1.81 + return parser.parse(s); 1.82 + } 1.83 + 1.84 + /** 1.85 + * Encode an object into JSON text and write it to out. 1.86 + * <p> 1.87 + * 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. 1.88 + * <p> 1.89 + * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with 1.90 + * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead. 1.91 + * 1.92 + * @see org.json.simple.JSONObject#writeJSONString(Map, Writer) 1.93 + * @see org.json.simple.JSONArray#writeJSONString(List, Writer) 1.94 + * 1.95 + * @param value 1.96 + * @param writer 1.97 + */ 1.98 + public static void writeJSONString(Object value, Writer out) throws IOException { 1.99 + if(value == null){ 1.100 + out.write("null"); 1.101 + return; 1.102 + } 1.103 + 1.104 + if(value instanceof String){ 1.105 + out.write('\"'); 1.106 + out.write(escape((String)value)); 1.107 + out.write('\"'); 1.108 + return; 1.109 + } 1.110 + 1.111 + if(value instanceof Double){ 1.112 + if(((Double)value).isInfinite() || ((Double)value).isNaN()) 1.113 + out.write("null"); 1.114 + else 1.115 + out.write(value.toString()); 1.116 + return; 1.117 + } 1.118 + 1.119 + if(value instanceof Float){ 1.120 + if(((Float)value).isInfinite() || ((Float)value).isNaN()) 1.121 + out.write("null"); 1.122 + else 1.123 + out.write(value.toString()); 1.124 + return; 1.125 + } 1.126 + 1.127 + if(value instanceof Number){ 1.128 + out.write(value.toString()); 1.129 + return; 1.130 + } 1.131 + 1.132 + if(value instanceof Boolean){ 1.133 + out.write(value.toString()); 1.134 + return; 1.135 + } 1.136 + 1.137 + if((value instanceof JSONStreamAware)){ 1.138 + ((JSONStreamAware)value).writeJSONString(out); 1.139 + return; 1.140 + } 1.141 + 1.142 + if((value instanceof JSONAware)){ 1.143 + out.write(((JSONAware)value).toJSONString()); 1.144 + return; 1.145 + } 1.146 + 1.147 + if(value instanceof Map){ 1.148 + JSONObject.writeJSONString((Map)value, out); 1.149 + return; 1.150 + } 1.151 + 1.152 + if(value instanceof List){ 1.153 + JSONArray.writeJSONString((List)value, out); 1.154 + return; 1.155 + } 1.156 + 1.157 + out.write(value.toString()); 1.158 + } 1.159 + 1.160 + /** 1.161 + * Convert an object to JSON text. 1.162 + * <p> 1.163 + * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly. 1.164 + * <p> 1.165 + * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with 1.166 + * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead. 1.167 + * 1.168 + * @see org.json.simple.JSONObject#toJSONString(Map) 1.169 + * @see org.json.simple.JSONArray#toJSONString(List) 1.170 + * 1.171 + * @param value 1.172 + * @return JSON text, or "null" if value is null or it's an NaN or an INF number. 1.173 + */ 1.174 + public static String toJSONString(Object value){ 1.175 + if(value == null) 1.176 + return "null"; 1.177 + 1.178 + if(value instanceof String) 1.179 + return "\""+escape((String)value)+"\""; 1.180 + 1.181 + if(value instanceof Double){ 1.182 + if(((Double)value).isInfinite() || ((Double)value).isNaN()) 1.183 + return "null"; 1.184 + else 1.185 + return value.toString(); 1.186 + } 1.187 + 1.188 + if(value instanceof Float){ 1.189 + if(((Float)value).isInfinite() || ((Float)value).isNaN()) 1.190 + return "null"; 1.191 + else 1.192 + return value.toString(); 1.193 + } 1.194 + 1.195 + if(value instanceof Number) 1.196 + return value.toString(); 1.197 + 1.198 + if(value instanceof Boolean) 1.199 + return value.toString(); 1.200 + 1.201 + if((value instanceof JSONAware)) 1.202 + return ((JSONAware)value).toJSONString(); 1.203 + 1.204 + if(value instanceof Map) 1.205 + return JSONObject.toJSONString((Map)value); 1.206 + 1.207 + if(value instanceof List) 1.208 + return JSONArray.toJSONString((List)value); 1.209 + 1.210 + return value.toString(); 1.211 + } 1.212 + 1.213 + /** 1.214 + * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). 1.215 + * @param s 1.216 + * @return 1.217 + */ 1.218 + public static String escape(String s){ 1.219 + if(s==null) 1.220 + return null; 1.221 + StringBuffer sb = new StringBuffer(); 1.222 + escape(s, sb); 1.223 + return sb.toString(); 1.224 + } 1.225 + 1.226 + /** 1.227 + * @param s - Must not be null. 1.228 + * @param sb 1.229 + */ 1.230 + static void escape(String s, StringBuffer sb) { 1.231 + for(int i=0;i<s.length();i++){ 1.232 + char ch=s.charAt(i); 1.233 + switch(ch){ 1.234 + case '"': 1.235 + sb.append("\\\""); 1.236 + break; 1.237 + case '\\': 1.238 + sb.append("\\\\"); 1.239 + break; 1.240 + case '\b': 1.241 + sb.append("\\b"); 1.242 + break; 1.243 + case '\f': 1.244 + sb.append("\\f"); 1.245 + break; 1.246 + case '\n': 1.247 + sb.append("\\n"); 1.248 + break; 1.249 + case '\r': 1.250 + sb.append("\\r"); 1.251 + break; 1.252 + case '\t': 1.253 + sb.append("\\t"); 1.254 + break; 1.255 + case '/': 1.256 + sb.append("\\/"); 1.257 + break; 1.258 + default: 1.259 + //Reference: http://www.unicode.org/versions/Unicode5.1.0/ 1.260 + if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){ 1.261 + String ss=Integer.toHexString(ch); 1.262 + sb.append("\\u"); 1.263 + for(int k=0;k<4-ss.length();k++){ 1.264 + sb.append('0'); 1.265 + } 1.266 + sb.append(ss.toUpperCase()); 1.267 + } 1.268 + else{ 1.269 + sb.append(ch); 1.270 + } 1.271 + } 1.272 + }//for 1.273 + } 1.274 + 1.275 +}