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