-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONWriter.java
204 lines (181 loc) · 6.29 KB
/
JSONWriter.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//package org.stringtree.json;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;
public class JSONWriter {
private StringBuffer buf = new StringBuffer();
private Stack<Object> calls = new Stack<Object>();
boolean emitClassName = true;
public JSONWriter(boolean emitClassName) {
this.emitClassName = emitClassName;
}
public JSONWriter() {
this(true);
}
public String write(Object object) {
buf.setLength(0);
value(object);
return buf.toString();
}
public String write(long n) {
return String.valueOf(n);
}
public String write(double d) {
return String.valueOf(d);
}
public String write(char c) {
return "\"" + c + "\"";
}
public String write(boolean b) {
return String.valueOf(b);
}
@SuppressWarnings("unchecked")
private void value(Object object) {
if (object == null || cyclic(object)) {
add("null");
} else {
calls.push(object);
if (object instanceof Class) string(object);
else if (object instanceof Boolean) bool(((Boolean) object).booleanValue());
else if (object instanceof Number) add(object);
else if (object instanceof String) string(object);
else if (object instanceof Character) string(object);
else if (object instanceof Map) map((Map)object);
else if (object.getClass().isArray()) array(object);
else if (object instanceof Iterator) array((Iterator)object);
else if (object instanceof Collection) array(((Collection)object).iterator());
else if (object instanceof JSONSerializable) value(((JSONSerializable)object).jsonSerialize());
else bean(object);
calls.pop();
}
}
private boolean cyclic(Object object) {
Iterator<Object> it = calls.iterator();
while (it.hasNext()) {
Object called = it.next();
if (object == called) return true;
}
return false;
}
private void bean(Object object) {
add("{");
BeanInfo info;
boolean addedSomething = false;
try {
info = Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
String name = prop.getName();
Method accessor = prop.getReadMethod();
if ((emitClassName==true || !"class".equals(name)) && accessor != null) {
if (!accessor.isAccessible()) accessor.setAccessible(true);
Object value = accessor.invoke(object, (Object[])null);
if (addedSomething) add(',');
add(name, value);
addedSomething = true;
}
}
Field[] ff = object.getClass().getFields();
for (int i = 0; i < ff.length; ++i) {
Field field = ff[i];
if (addedSomething) add(',');
add(field.getName(), field.get(object));
addedSomething = true;
}
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.getCause().printStackTrace();
ite.printStackTrace();
} catch (IntrospectionException ie) {
ie.printStackTrace();
}
add("}");
}
private void add(String name, Object value) {
add('"');
add(name);
add("\":");
value(value);
}
private void map(Map<String,Object> map) {
add("{");
Iterator<Map.Entry<String,Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<?,?> e = (Map.Entry<?,?>) it.next();
value(e.getKey());
add(":");
value(e.getValue());
if (it.hasNext()) add(',');
}
add("}");
}
private void array(Iterator<?> it) {
add("[");
while (it.hasNext()) {
value(it.next());
if (it.hasNext()) add(",");
}
add("]");
}
private void array(Object object) {
add("[");
int length = Array.getLength(object);
for (int i = 0; i < length; ++i) {
value(Array.get(object, i));
if (i < length - 1) add(',');
}
add("]");
}
private void bool(boolean b) {
add(b ? "true" : "false");
}
private void string(Object obj) {
add('"');
CharacterIterator it = new StringCharacterIterator(obj.toString());
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
if (c == '"') add("\\\"");
else if (c == '\\') add("\\\\");
else if (c == '/') add("\\/");
else if (c == '\b') add("\\b");
else if (c == '\f') add("\\f");
else if (c == '\n') add("\\n");
else if (c == '\r') add("\\r");
else if (c == '\t') add("\\t");
else if (Character.isISOControl(c)) {
unicode(c);
} else {
add(c);
}
}
add('"');
}
private void add(Object obj) {
buf.append(obj);
}
private void add(char c) {
buf.append(c);
}
static char[] hex = "0123456789ABCDEF".toCharArray();
private void unicode(char c) {
add("\\u");
int n = c;
for (int i = 0; i < 4; ++i) {
int digit = (n & 0xf000) >> 12;
add(hex[digit]);
n <<= 4;
}
}
}