json-simple/tags/1.1.1-1/src/main/java/org/json/simple/JSONObject.java

Code
Comments
Other
Rev Date Author Line
50 26 Feb 19 nicklas 1 /*
50 26 Feb 19 nicklas 2  * $Id: JSONObject.java,v 1.1 2006/04/15 14:10:48 platform Exp $
50 26 Feb 19 nicklas 3  * Created on 2006-4-10
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.Writer;
50 26 Feb 19 nicklas 9 import java.util.HashMap;
50 26 Feb 19 nicklas 10 import java.util.Iterator;
50 26 Feb 19 nicklas 11 import java.util.Map;
50 26 Feb 19 nicklas 12
50 26 Feb 19 nicklas 13 /**
50 26 Feb 19 nicklas 14  * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
50 26 Feb 19 nicklas 15  * 
50 26 Feb 19 nicklas 16  * @author FangYidong<fangyidong@yahoo.com.cn>
50 26 Feb 19 nicklas 17  */
57 26 Feb 19 nicklas 18 public class JSONObject extends HashMap<String, Object> implements Map<String, Object>, JSONAware, JSONStreamAware{
50 26 Feb 19 nicklas 19   
50 26 Feb 19 nicklas 20   private static final long serialVersionUID = -503443796854799292L;
50 26 Feb 19 nicklas 21   
50 26 Feb 19 nicklas 22   
50 26 Feb 19 nicklas 23   public JSONObject() {
50 26 Feb 19 nicklas 24     super();
50 26 Feb 19 nicklas 25   }
50 26 Feb 19 nicklas 26
50 26 Feb 19 nicklas 27   /**
50 26 Feb 19 nicklas 28    * Allows creation of a JSONObject from a Map. After that, both the
50 26 Feb 19 nicklas 29    * generated JSONObject and the Map can be modified independently.
50 26 Feb 19 nicklas 30    * 
50 26 Feb 19 nicklas 31    * @param map
50 26 Feb 19 nicklas 32    */
57 26 Feb 19 nicklas 33   @SuppressWarnings({ "unchecked", "rawtypes" })
50 26 Feb 19 nicklas 34   public JSONObject(Map map) {
50 26 Feb 19 nicklas 35     super(map);
50 26 Feb 19 nicklas 36   }
50 26 Feb 19 nicklas 37
50 26 Feb 19 nicklas 38
50 26 Feb 19 nicklas 39     /**
50 26 Feb 19 nicklas 40      * Encode a map into JSON text and write it to out.
50 26 Feb 19 nicklas 41      * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
50 26 Feb 19 nicklas 42      * 
50 26 Feb 19 nicklas 43      * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
50 26 Feb 19 nicklas 44      * 
50 26 Feb 19 nicklas 45      * @param map
50 26 Feb 19 nicklas 46      * @param out
50 26 Feb 19 nicklas 47      */
57 26 Feb 19 nicklas 48   @SuppressWarnings("rawtypes")
50 26 Feb 19 nicklas 49   public static void writeJSONString(Map map, Writer out) throws IOException {
50 26 Feb 19 nicklas 50     if(map == null){
50 26 Feb 19 nicklas 51       out.write("null");
50 26 Feb 19 nicklas 52       return;
50 26 Feb 19 nicklas 53     }
50 26 Feb 19 nicklas 54     
50 26 Feb 19 nicklas 55     boolean first = true;
50 26 Feb 19 nicklas 56     Iterator iter=map.entrySet().iterator();
50 26 Feb 19 nicklas 57     
50 26 Feb 19 nicklas 58         out.write('{');
50 26 Feb 19 nicklas 59     while(iter.hasNext()){
50 26 Feb 19 nicklas 60             if(first)
50 26 Feb 19 nicklas 61                 first = false;
50 26 Feb 19 nicklas 62             else
50 26 Feb 19 nicklas 63                 out.write(',');
50 26 Feb 19 nicklas 64       Map.Entry entry=(Map.Entry)iter.next();
50 26 Feb 19 nicklas 65             out.write('\"');
50 26 Feb 19 nicklas 66             out.write(escape(String.valueOf(entry.getKey())));
50 26 Feb 19 nicklas 67             out.write('\"');
50 26 Feb 19 nicklas 68             out.write(':');
50 26 Feb 19 nicklas 69       JSONValue.writeJSONString(entry.getValue(), out);
50 26 Feb 19 nicklas 70     }
50 26 Feb 19 nicklas 71     out.write('}');
50 26 Feb 19 nicklas 72   }
50 26 Feb 19 nicklas 73
55 26 Feb 19 nicklas 74   @Override
50 26 Feb 19 nicklas 75   public void writeJSONString(Writer out) throws IOException{
50 26 Feb 19 nicklas 76     writeJSONString(this, out);
50 26 Feb 19 nicklas 77   }
50 26 Feb 19 nicklas 78   
50 26 Feb 19 nicklas 79   /**
50 26 Feb 19 nicklas 80    * Convert a map to JSON text. The result is a JSON object. 
50 26 Feb 19 nicklas 81    * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
50 26 Feb 19 nicklas 82    * 
50 26 Feb 19 nicklas 83    * @see org.json.simple.JSONValue#toJSONString(Object)
50 26 Feb 19 nicklas 84    * 
50 26 Feb 19 nicklas 85    * @param map
50 26 Feb 19 nicklas 86    * @return JSON text, or "null" if map is null.
50 26 Feb 19 nicklas 87    */
57 26 Feb 19 nicklas 88   @SuppressWarnings("rawtypes")
50 26 Feb 19 nicklas 89   public static String toJSONString(Map map){
50 26 Feb 19 nicklas 90     if(map == null)
50 26 Feb 19 nicklas 91       return "null";
50 26 Feb 19 nicklas 92     
50 26 Feb 19 nicklas 93         StringBuffer sb = new StringBuffer();
50 26 Feb 19 nicklas 94         boolean first = true;
50 26 Feb 19 nicklas 95     Iterator iter=map.entrySet().iterator();
50 26 Feb 19 nicklas 96     
50 26 Feb 19 nicklas 97         sb.append('{');
50 26 Feb 19 nicklas 98     while(iter.hasNext()){
50 26 Feb 19 nicklas 99             if(first)
50 26 Feb 19 nicklas 100                 first = false;
50 26 Feb 19 nicklas 101             else
50 26 Feb 19 nicklas 102                 sb.append(',');
50 26 Feb 19 nicklas 103             
50 26 Feb 19 nicklas 104       Map.Entry entry=(Map.Entry)iter.next();
50 26 Feb 19 nicklas 105       toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
50 26 Feb 19 nicklas 106     }
50 26 Feb 19 nicklas 107         sb.append('}');
50 26 Feb 19 nicklas 108     return sb.toString();
50 26 Feb 19 nicklas 109   }
50 26 Feb 19 nicklas 110   
55 26 Feb 19 nicklas 111   @Override
50 26 Feb 19 nicklas 112   public String toJSONString(){
50 26 Feb 19 nicklas 113     return toJSONString(this);
50 26 Feb 19 nicklas 114   }
50 26 Feb 19 nicklas 115   
50 26 Feb 19 nicklas 116   private static String toJSONString(String key,Object value, StringBuffer sb){
50 26 Feb 19 nicklas 117     sb.append('\"');
50 26 Feb 19 nicklas 118         if(key == null)
50 26 Feb 19 nicklas 119             sb.append("null");
50 26 Feb 19 nicklas 120         else
50 26 Feb 19 nicklas 121             JSONValue.escape(key, sb);
50 26 Feb 19 nicklas 122     sb.append('\"').append(':');
50 26 Feb 19 nicklas 123     
50 26 Feb 19 nicklas 124     sb.append(JSONValue.toJSONString(value));
50 26 Feb 19 nicklas 125     
50 26 Feb 19 nicklas 126     return sb.toString();
50 26 Feb 19 nicklas 127   }
50 26 Feb 19 nicklas 128   
55 26 Feb 19 nicklas 129   @Override
50 26 Feb 19 nicklas 130   public String toString(){
50 26 Feb 19 nicklas 131     return toJSONString();
50 26 Feb 19 nicklas 132   }
50 26 Feb 19 nicklas 133
50 26 Feb 19 nicklas 134   public static String toString(String key,Object value){
50 26 Feb 19 nicklas 135         StringBuffer sb = new StringBuffer();
50 26 Feb 19 nicklas 136     toJSONString(key, value, sb);
50 26 Feb 19 nicklas 137         return sb.toString();
50 26 Feb 19 nicklas 138   }
50 26 Feb 19 nicklas 139   
50 26 Feb 19 nicklas 140   /**
50 26 Feb 19 nicklas 141    * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
50 26 Feb 19 nicklas 142    * It's the same as JSONValue.escape() only for compatibility here.
50 26 Feb 19 nicklas 143    * 
50 26 Feb 19 nicklas 144    * @see org.json.simple.JSONValue#escape(String)
50 26 Feb 19 nicklas 145    * 
50 26 Feb 19 nicklas 146    * @param s
50 26 Feb 19 nicklas 147    * @return
50 26 Feb 19 nicklas 148    */
50 26 Feb 19 nicklas 149   public static String escape(String s){
50 26 Feb 19 nicklas 150     return JSONValue.escape(s);
50 26 Feb 19 nicklas 151   }
50 26 Feb 19 nicklas 152 }