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