50 |
26 Feb 19 |
nicklas |
1 |
/* |
50 |
26 Feb 19 |
nicklas |
* $Id: JSONObject.java,v 1.1 2006/04/15 14:10:48 platform Exp $ |
50 |
26 Feb 19 |
nicklas |
* 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 |
* 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 |
* @author FangYidong<fangyidong@yahoo.com.cn> |
50 |
26 Feb 19 |
nicklas |
17 |
*/ |
50 |
26 Feb 19 |
nicklas |
18 |
public class JSONObject extends HashMap implements Map, 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 |
* Allows creation of a JSONObject from a Map. After that, both the |
50 |
26 Feb 19 |
nicklas |
* generated JSONObject and the Map can be modified independently. |
50 |
26 Feb 19 |
nicklas |
30 |
* |
50 |
26 Feb 19 |
nicklas |
* @param map |
50 |
26 Feb 19 |
nicklas |
32 |
*/ |
50 |
26 Feb 19 |
nicklas |
33 |
public JSONObject(Map map) { |
50 |
26 Feb 19 |
nicklas |
34 |
super(map); |
50 |
26 Feb 19 |
nicklas |
35 |
} |
50 |
26 Feb 19 |
nicklas |
36 |
|
50 |
26 Feb 19 |
nicklas |
37 |
|
50 |
26 Feb 19 |
nicklas |
38 |
/** |
50 |
26 Feb 19 |
nicklas |
* Encode a map into JSON text and write it to out. |
50 |
26 Feb 19 |
nicklas |
* 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 |
41 |
* |
50 |
26 Feb 19 |
nicklas |
* @see org.json.simple.JSONValue#writeJSONString(Object, Writer) |
50 |
26 Feb 19 |
nicklas |
43 |
* |
50 |
26 Feb 19 |
nicklas |
* @param map |
50 |
26 Feb 19 |
nicklas |
* @param out |
50 |
26 Feb 19 |
nicklas |
46 |
*/ |
50 |
26 Feb 19 |
nicklas |
47 |
public static void writeJSONString(Map map, Writer out) throws IOException { |
50 |
26 Feb 19 |
nicklas |
48 |
if(map == null){ |
50 |
26 Feb 19 |
nicklas |
49 |
out.write("null"); |
50 |
26 Feb 19 |
nicklas |
50 |
return; |
50 |
26 Feb 19 |
nicklas |
51 |
} |
50 |
26 Feb 19 |
nicklas |
52 |
|
50 |
26 Feb 19 |
nicklas |
53 |
boolean first = true; |
50 |
26 Feb 19 |
nicklas |
54 |
Iterator iter=map.entrySet().iterator(); |
50 |
26 Feb 19 |
nicklas |
55 |
|
50 |
26 Feb 19 |
nicklas |
56 |
out.write('{'); |
50 |
26 Feb 19 |
nicklas |
57 |
while(iter.hasNext()){ |
50 |
26 Feb 19 |
nicklas |
58 |
if(first) |
50 |
26 Feb 19 |
nicklas |
59 |
first = false; |
50 |
26 Feb 19 |
nicklas |
60 |
else |
50 |
26 Feb 19 |
nicklas |
61 |
out.write(','); |
50 |
26 Feb 19 |
nicklas |
62 |
Map.Entry entry=(Map.Entry)iter.next(); |
50 |
26 Feb 19 |
nicklas |
63 |
out.write('\"'); |
50 |
26 Feb 19 |
nicklas |
64 |
out.write(escape(String.valueOf(entry.getKey()))); |
50 |
26 Feb 19 |
nicklas |
65 |
out.write('\"'); |
50 |
26 Feb 19 |
nicklas |
66 |
out.write(':'); |
50 |
26 Feb 19 |
nicklas |
67 |
JSONValue.writeJSONString(entry.getValue(), out); |
50 |
26 Feb 19 |
nicklas |
68 |
} |
50 |
26 Feb 19 |
nicklas |
69 |
out.write('}'); |
50 |
26 Feb 19 |
nicklas |
70 |
} |
50 |
26 Feb 19 |
nicklas |
71 |
|
50 |
26 Feb 19 |
nicklas |
72 |
public void writeJSONString(Writer out) throws IOException{ |
50 |
26 Feb 19 |
nicklas |
73 |
writeJSONString(this, out); |
50 |
26 Feb 19 |
nicklas |
74 |
} |
50 |
26 Feb 19 |
nicklas |
75 |
|
50 |
26 Feb 19 |
nicklas |
76 |
/** |
50 |
26 Feb 19 |
nicklas |
* Convert a map to JSON text. The result is a JSON object. |
50 |
26 Feb 19 |
nicklas |
* If this map is also a JSONAware, JSONAware specific behaviours will be omitted at this top level. |
50 |
26 Feb 19 |
nicklas |
79 |
* |
50 |
26 Feb 19 |
nicklas |
* @see org.json.simple.JSONValue#toJSONString(Object) |
50 |
26 Feb 19 |
nicklas |
81 |
* |
50 |
26 Feb 19 |
nicklas |
* @param map |
50 |
26 Feb 19 |
nicklas |
* @return JSON text, or "null" if map is null. |
50 |
26 Feb 19 |
nicklas |
84 |
*/ |
50 |
26 Feb 19 |
nicklas |
85 |
public static String toJSONString(Map map){ |
50 |
26 Feb 19 |
nicklas |
86 |
if(map == null) |
50 |
26 Feb 19 |
nicklas |
87 |
return "null"; |
50 |
26 Feb 19 |
nicklas |
88 |
|
50 |
26 Feb 19 |
nicklas |
89 |
StringBuffer sb = new StringBuffer(); |
50 |
26 Feb 19 |
nicklas |
90 |
boolean first = true; |
50 |
26 Feb 19 |
nicklas |
91 |
Iterator iter=map.entrySet().iterator(); |
50 |
26 Feb 19 |
nicklas |
92 |
|
50 |
26 Feb 19 |
nicklas |
93 |
sb.append('{'); |
50 |
26 Feb 19 |
nicklas |
94 |
while(iter.hasNext()){ |
50 |
26 Feb 19 |
nicklas |
95 |
if(first) |
50 |
26 Feb 19 |
nicklas |
96 |
first = false; |
50 |
26 Feb 19 |
nicklas |
97 |
else |
50 |
26 Feb 19 |
nicklas |
98 |
sb.append(','); |
50 |
26 Feb 19 |
nicklas |
99 |
|
50 |
26 Feb 19 |
nicklas |
100 |
Map.Entry entry=(Map.Entry)iter.next(); |
50 |
26 Feb 19 |
nicklas |
101 |
toJSONString(String.valueOf(entry.getKey()),entry.getValue(), sb); |
50 |
26 Feb 19 |
nicklas |
102 |
} |
50 |
26 Feb 19 |
nicklas |
103 |
sb.append('}'); |
50 |
26 Feb 19 |
nicklas |
104 |
return sb.toString(); |
50 |
26 Feb 19 |
nicklas |
105 |
} |
50 |
26 Feb 19 |
nicklas |
106 |
|
50 |
26 Feb 19 |
nicklas |
107 |
public String toJSONString(){ |
50 |
26 Feb 19 |
nicklas |
108 |
return toJSONString(this); |
50 |
26 Feb 19 |
nicklas |
109 |
} |
50 |
26 Feb 19 |
nicklas |
110 |
|
50 |
26 Feb 19 |
nicklas |
111 |
private static String toJSONString(String key,Object value, StringBuffer sb){ |
50 |
26 Feb 19 |
nicklas |
112 |
sb.append('\"'); |
50 |
26 Feb 19 |
nicklas |
113 |
if(key == null) |
50 |
26 Feb 19 |
nicklas |
114 |
sb.append("null"); |
50 |
26 Feb 19 |
nicklas |
115 |
else |
50 |
26 Feb 19 |
nicklas |
116 |
JSONValue.escape(key, sb); |
50 |
26 Feb 19 |
nicklas |
117 |
sb.append('\"').append(':'); |
50 |
26 Feb 19 |
nicklas |
118 |
|
50 |
26 Feb 19 |
nicklas |
119 |
sb.append(JSONValue.toJSONString(value)); |
50 |
26 Feb 19 |
nicklas |
120 |
|
50 |
26 Feb 19 |
nicklas |
121 |
return sb.toString(); |
50 |
26 Feb 19 |
nicklas |
122 |
} |
50 |
26 Feb 19 |
nicklas |
123 |
|
50 |
26 Feb 19 |
nicklas |
124 |
public String toString(){ |
50 |
26 Feb 19 |
nicklas |
125 |
return toJSONString(); |
50 |
26 Feb 19 |
nicklas |
126 |
} |
50 |
26 Feb 19 |
nicklas |
127 |
|
50 |
26 Feb 19 |
nicklas |
128 |
public static String toString(String key,Object value){ |
50 |
26 Feb 19 |
nicklas |
129 |
StringBuffer sb = new StringBuffer(); |
50 |
26 Feb 19 |
nicklas |
130 |
toJSONString(key, value, sb); |
50 |
26 Feb 19 |
nicklas |
131 |
return sb.toString(); |
50 |
26 Feb 19 |
nicklas |
132 |
} |
50 |
26 Feb 19 |
nicklas |
133 |
|
50 |
26 Feb 19 |
nicklas |
134 |
/** |
50 |
26 Feb 19 |
nicklas |
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). |
50 |
26 Feb 19 |
nicklas |
* It's the same as JSONValue.escape() only for compatibility here. |
50 |
26 Feb 19 |
nicklas |
137 |
* |
50 |
26 Feb 19 |
nicklas |
* @see org.json.simple.JSONValue#escape(String) |
50 |
26 Feb 19 |
nicklas |
139 |
* |
50 |
26 Feb 19 |
nicklas |
* @param s |
50 |
26 Feb 19 |
nicklas |
* @return |
50 |
26 Feb 19 |
nicklas |
142 |
*/ |
50 |
26 Feb 19 |
nicklas |
143 |
public static String escape(String s){ |
50 |
26 Feb 19 |
nicklas |
144 |
return JSONValue.escape(s); |
50 |
26 Feb 19 |
nicklas |
145 |
} |
50 |
26 Feb 19 |
nicklas |
146 |
} |