Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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;
7 import java.io.IOException;
8 import java.io.Writer;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.Map;
13 /**
14 * A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
15 *
16 * @author FangYidong<fangyidong@yahoo.com.cn>
17 */
18 public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAware{
19 private static final long serialVersionUID = -503443796854799292L;
21 /**
22 * Encode a map into JSON text and write it to out.
23 * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
24 *
25 * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
26 *
27 * @param map
28 * @param out
29 */
30 public static void writeJSONString(Map map, Writer out) throws IOException {
31 if(map == null){
32 out.write("null");
33 return;
34 }
36 boolean first = true;
37 Iterator iter=map.entrySet().iterator();
39 out.write('{');
40 while(iter.hasNext()){
41 if(first)
42 first = false;
43 else
44 out.write(',');
45 Map.Entry entry=(Map.Entry)iter.next();
46 out.write('\"');
47 out.write(escape(String.valueOf(entry.getKey())));
48 out.write('\"');
49 out.write(':');
50 JSONValue.writeJSONString(entry.getValue(), out);
51 }
52 out.write('}');
53 }
55 public void writeJSONString(Writer out) throws IOException{
56 writeJSONString(this, out);
57 }
59 /**
60 * Convert a map to JSON text. The result is a JSON object.
61 * If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level.
62 *
63 * @see org.json.simple.JSONValue#toJSONString(Object)
64 *
65 * @param map
66 * @return JSON text, or "null" if map is null.
67 */
68 public static String toJSONString(Map map){
69 if(map == null)
70 return "null";
72 StringBuffer sb = new StringBuffer();
73 boolean first = true;
74 Iterator iter=map.entrySet().iterator();
76 sb.append('{');
77 while(iter.hasNext()){
78 if(first)
79 first = false;
80 else
81 sb.append(',');
83 Map.Entry entry=(Map.Entry)iter.next();
84 toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb);
85 }
86 sb.append('}');
87 return sb.toString();
88 }
90 public String toJSONString(){
91 return toJSONString(this);
92 }
94 private static String toJSONString(String key,Object value, StringBuffer sb){
95 sb.append('\"');
96 if(key == null)
97 sb.append("null");
98 else
99 JSONValue.escape(key, sb);
100 sb.append('\"').append(':');
102 sb.append(JSONValue.toJSONString(value));
104 return sb.toString();
105 }
107 public String toString(){
108 return toJSONString();
109 }
111 public static String toString(String key,Object value){
112 StringBuffer sb = new StringBuffer();
113 toJSONString(key, value, sb);
114 return sb.toString();
115 }
117 /**
118 * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
119 * It's the same as JSONValue.escape() only for compatibility here.
120 *
121 * @see org.json.simple.JSONValue#escape(String)
122 *
123 * @param s
124 * @return
125 */
126 public static String escape(String s){
127 return JSONValue.escape(s);
128 }
129 }