json-simple/vendor/json-simple-1.1.1/src/main/java/org/json/simple/JSONValue.java

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