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