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