mobile/android/thirdparty/org/json/simple/JSONValue.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
michael@0 3 * Created on 2006-4-15
michael@0 4 */
michael@0 5 package org.json.simple;
michael@0 6
michael@0 7 import java.io.IOException;
michael@0 8 import java.io.Reader;
michael@0 9 import java.io.StringReader;
michael@0 10 import java.io.Writer;
michael@0 11 import java.util.List;
michael@0 12 import java.util.Map;
michael@0 13
michael@0 14 import org.json.simple.parser.JSONParser;
michael@0 15 import org.json.simple.parser.ParseException;
michael@0 16
michael@0 17
michael@0 18 /**
michael@0 19 * @author FangYidong<fangyidong@yahoo.com.cn>
michael@0 20 */
michael@0 21 public class JSONValue {
michael@0 22 /**
michael@0 23 * Parse JSON text into java object from the input source.
michael@0 24 * Please use parseWithException() if you don't want to ignore the exception.
michael@0 25 *
michael@0 26 * @see org.json.simple.parser.JSONParser#parse(Reader)
michael@0 27 * @see #parseWithException(Reader)
michael@0 28 *
michael@0 29 * @param in
michael@0 30 * @return Instance of the following:
michael@0 31 * org.json.simple.JSONObject,
michael@0 32 * org.json.simple.JSONArray,
michael@0 33 * java.lang.String,
michael@0 34 * java.lang.Number,
michael@0 35 * java.lang.Boolean,
michael@0 36 * null
michael@0 37 *
michael@0 38 */
michael@0 39 public static Object parse(Reader in){
michael@0 40 try{
michael@0 41 JSONParser parser=new JSONParser();
michael@0 42 return parser.parse(in);
michael@0 43 }
michael@0 44 catch(Exception e){
michael@0 45 return null;
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 public static Object parse(String s){
michael@0 50 StringReader in=new StringReader(s);
michael@0 51 return parse(in);
michael@0 52 }
michael@0 53
michael@0 54 /**
michael@0 55 * Parse JSON text into java object from the input source.
michael@0 56 *
michael@0 57 * @see org.json.simple.parser.JSONParser
michael@0 58 *
michael@0 59 * @param in
michael@0 60 * @return Instance of the following:
michael@0 61 * org.json.simple.JSONObject,
michael@0 62 * org.json.simple.JSONArray,
michael@0 63 * java.lang.String,
michael@0 64 * java.lang.Number,
michael@0 65 * java.lang.Boolean,
michael@0 66 * null
michael@0 67 *
michael@0 68 * @throws IOException
michael@0 69 * @throws ParseException
michael@0 70 */
michael@0 71 public static Object parseWithException(Reader in) throws IOException, ParseException{
michael@0 72 JSONParser parser=new JSONParser();
michael@0 73 return parser.parse(in);
michael@0 74 }
michael@0 75
michael@0 76 public static Object parseWithException(String s) throws ParseException{
michael@0 77 JSONParser parser=new JSONParser();
michael@0 78 return parser.parse(s);
michael@0 79 }
michael@0 80
michael@0 81 /**
michael@0 82 * Encode an object into JSON text and write it to out.
michael@0 83 * <p>
michael@0 84 * 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 85 * <p>
michael@0 86 * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with
michael@0 87 * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead.
michael@0 88 *
michael@0 89 * @see org.json.simple.JSONObject#writeJSONString(Map, Writer)
michael@0 90 * @see org.json.simple.JSONArray#writeJSONString(List, Writer)
michael@0 91 *
michael@0 92 * @param value
michael@0 93 * @param writer
michael@0 94 */
michael@0 95 public static void writeJSONString(Object value, Writer out) throws IOException {
michael@0 96 if(value == null){
michael@0 97 out.write("null");
michael@0 98 return;
michael@0 99 }
michael@0 100
michael@0 101 if(value instanceof String){
michael@0 102 out.write('\"');
michael@0 103 out.write(escape((String)value));
michael@0 104 out.write('\"');
michael@0 105 return;
michael@0 106 }
michael@0 107
michael@0 108 if(value instanceof Double){
michael@0 109 if(((Double)value).isInfinite() || ((Double)value).isNaN())
michael@0 110 out.write("null");
michael@0 111 else
michael@0 112 out.write(value.toString());
michael@0 113 return;
michael@0 114 }
michael@0 115
michael@0 116 if(value instanceof Float){
michael@0 117 if(((Float)value).isInfinite() || ((Float)value).isNaN())
michael@0 118 out.write("null");
michael@0 119 else
michael@0 120 out.write(value.toString());
michael@0 121 return;
michael@0 122 }
michael@0 123
michael@0 124 if(value instanceof Number){
michael@0 125 out.write(value.toString());
michael@0 126 return;
michael@0 127 }
michael@0 128
michael@0 129 if(value instanceof Boolean){
michael@0 130 out.write(value.toString());
michael@0 131 return;
michael@0 132 }
michael@0 133
michael@0 134 if((value instanceof JSONStreamAware)){
michael@0 135 ((JSONStreamAware)value).writeJSONString(out);
michael@0 136 return;
michael@0 137 }
michael@0 138
michael@0 139 if((value instanceof JSONAware)){
michael@0 140 out.write(((JSONAware)value).toJSONString());
michael@0 141 return;
michael@0 142 }
michael@0 143
michael@0 144 if(value instanceof Map){
michael@0 145 JSONObject.writeJSONString((Map)value, out);
michael@0 146 return;
michael@0 147 }
michael@0 148
michael@0 149 if(value instanceof List){
michael@0 150 JSONArray.writeJSONString((List)value, out);
michael@0 151 return;
michael@0 152 }
michael@0 153
michael@0 154 out.write(value.toString());
michael@0 155 }
michael@0 156
michael@0 157 /**
michael@0 158 * Convert an object to JSON text.
michael@0 159 * <p>
michael@0 160 * If this object is a Map or a List, and it's also a JSONAware, JSONAware will be considered firstly.
michael@0 161 * <p>
michael@0 162 * DO NOT call this method from toJSONString() of a class that implements both JSONAware and Map or List with
michael@0 163 * "this" as the parameter, use JSONObject.toJSONString(Map) or JSONArray.toJSONString(List) instead.
michael@0 164 *
michael@0 165 * @see org.json.simple.JSONObject#toJSONString(Map)
michael@0 166 * @see org.json.simple.JSONArray#toJSONString(List)
michael@0 167 *
michael@0 168 * @param value
michael@0 169 * @return JSON text, or "null" if value is null or it's an NaN or an INF number.
michael@0 170 */
michael@0 171 public static String toJSONString(Object value){
michael@0 172 if(value == null)
michael@0 173 return "null";
michael@0 174
michael@0 175 if(value instanceof String)
michael@0 176 return "\""+escape((String)value)+"\"";
michael@0 177
michael@0 178 if(value instanceof Double){
michael@0 179 if(((Double)value).isInfinite() || ((Double)value).isNaN())
michael@0 180 return "null";
michael@0 181 else
michael@0 182 return value.toString();
michael@0 183 }
michael@0 184
michael@0 185 if(value instanceof Float){
michael@0 186 if(((Float)value).isInfinite() || ((Float)value).isNaN())
michael@0 187 return "null";
michael@0 188 else
michael@0 189 return value.toString();
michael@0 190 }
michael@0 191
michael@0 192 if(value instanceof Number)
michael@0 193 return value.toString();
michael@0 194
michael@0 195 if(value instanceof Boolean)
michael@0 196 return value.toString();
michael@0 197
michael@0 198 if((value instanceof JSONAware))
michael@0 199 return ((JSONAware)value).toJSONString();
michael@0 200
michael@0 201 if(value instanceof Map)
michael@0 202 return JSONObject.toJSONString((Map)value);
michael@0 203
michael@0 204 if(value instanceof List)
michael@0 205 return JSONArray.toJSONString((List)value);
michael@0 206
michael@0 207 return value.toString();
michael@0 208 }
michael@0 209
michael@0 210 /**
michael@0 211 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
michael@0 212 * @param s
michael@0 213 * @return
michael@0 214 */
michael@0 215 public static String escape(String s){
michael@0 216 if(s==null)
michael@0 217 return null;
michael@0 218 StringBuffer sb = new StringBuffer();
michael@0 219 escape(s, sb);
michael@0 220 return sb.toString();
michael@0 221 }
michael@0 222
michael@0 223 /**
michael@0 224 * @param s - Must not be null.
michael@0 225 * @param sb
michael@0 226 */
michael@0 227 static void escape(String s, StringBuffer sb) {
michael@0 228 for(int i=0;i<s.length();i++){
michael@0 229 char ch=s.charAt(i);
michael@0 230 switch(ch){
michael@0 231 case '"':
michael@0 232 sb.append("\\\"");
michael@0 233 break;
michael@0 234 case '\\':
michael@0 235 sb.append("\\\\");
michael@0 236 break;
michael@0 237 case '\b':
michael@0 238 sb.append("\\b");
michael@0 239 break;
michael@0 240 case '\f':
michael@0 241 sb.append("\\f");
michael@0 242 break;
michael@0 243 case '\n':
michael@0 244 sb.append("\\n");
michael@0 245 break;
michael@0 246 case '\r':
michael@0 247 sb.append("\\r");
michael@0 248 break;
michael@0 249 case '\t':
michael@0 250 sb.append("\\t");
michael@0 251 break;
michael@0 252 case '/':
michael@0 253 sb.append("\\/");
michael@0 254 break;
michael@0 255 default:
michael@0 256 //Reference: http://www.unicode.org/versions/Unicode5.1.0/
michael@0 257 if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
michael@0 258 String ss=Integer.toHexString(ch);
michael@0 259 sb.append("\\u");
michael@0 260 for(int k=0;k<4-ss.length();k++){
michael@0 261 sb.append('0');
michael@0 262 }
michael@0 263 sb.append(ss.toUpperCase());
michael@0 264 }
michael@0 265 else{
michael@0 266 sb.append(ch);
michael@0 267 }
michael@0 268 }
michael@0 269 }//for
michael@0 270 }
michael@0 271
michael@0 272 }

mercurial