-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONReader.java
More file actions
223 lines (200 loc) · 6.49 KB
/
JSONReader.java
File metadata and controls
223 lines (200 loc) · 6.49 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//package org.stringtree.json;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JSONReader {
protected static final Object OBJECT_END = new Object();
protected static final Object ARRAY_END = new Object();
protected static final Object COLON = new Object();
protected static final Object COMMA = new Object();
public static final int FIRST = 0;
public static final int CURRENT = 1;
public static final int NEXT = 2;
protected static Map<Character, Character> escapes = new HashMap<Character, Character>();
static {
escapes.put(Character.valueOf('"'), Character.valueOf('"'));
escapes.put(Character.valueOf('\\'), Character.valueOf('\\'));
escapes.put(Character.valueOf('/'), Character.valueOf('/'));
escapes.put(Character.valueOf('b'), Character.valueOf('\b'));
escapes.put(Character.valueOf('f'), Character.valueOf('\f'));
escapes.put(Character.valueOf('n'), Character.valueOf('\n'));
escapes.put(Character.valueOf('r'), Character.valueOf('\r'));
escapes.put(Character.valueOf('t'), Character.valueOf('\t'));
}
protected CharacterIterator it;
protected char c;
protected Object token;
protected StringBuffer buf = new StringBuffer();
protected char next() {
c = it.next();
return c;
}
protected void skipWhiteSpace() {
while (Character.isWhitespace(c)) {
next();
}
}
public Object read(CharacterIterator ci, int start) {
it = ci;
switch (start) {
case FIRST:
c = it.first();
break;
case CURRENT:
c = it.current();
break;
case NEXT:
c = it.next();
break;
}
return read();
}
public Object read(CharacterIterator it) {
return read(it, NEXT);
}
public Object read(String string) {
return read(new StringCharacterIterator(string), FIRST);
}
protected Object read() {
skipWhiteSpace();
char ch = c;
next();
switch (ch) {
case '"': token = string(); break;
case '[': token = array(); break;
case ']': token = ARRAY_END; break;
case ',': token = COMMA; break;
case '{': token = object(); break;
case '}': token = OBJECT_END; break;
case ':': token = COLON; break;
case 't':
next(); next(); next(); // assumed r-u-e
token = Boolean.TRUE;
break;
case'f':
next(); next(); next(); next(); // assumed a-l-s-e
token = Boolean.FALSE;
break;
case 'n':
next(); next(); next(); // assumed u-l-l
token = null;
break;
default:
c = it.previous();
if (Character.isDigit(c) || c == '-') {
token = number();
}
}
// System.out.println("token: " + token); // enable this line to see the token stream
return token;
}
protected Object object() {
Map<Object, Object> ret = new LinkedHashMap<Object, Object>();
Object key = read();
while (token != OBJECT_END) {
read(); // should be a colon
if (token != OBJECT_END) {
ret.put(key, read());
if (read() == COMMA) {
key = read();
}
}
}
return ret;
}
protected Object array() {
List<Object> ret = new ArrayList<Object>();
Object value = read();
while (token != ARRAY_END) {
ret.add(value);
if (read() == COMMA) {
value = read();
}
}
return ret;
}
protected Object number() {
int length = 0;
boolean isFloatingPoint = false;
buf.setLength(0);
if (c == '-') {
add();
}
length += addDigits();
if (c == '.') {
add();
length += addDigits();
isFloatingPoint = true;
}
if (c == 'e' || c == 'E') {
add();
if (c == '+' || c == '-') {
add();
}
addDigits();
isFloatingPoint = true;
}
String s = buf.toString();
return isFloatingPoint
? (length < 17) ? (Object)Double.valueOf(s) : new BigDecimal(s)
: (length < 19) ? (Object)Long.valueOf(s) : new BigInteger(s);
}
protected int addDigits() {
int ret;
for (ret = 0; Character.isDigit(c); ++ret) {
add();
}
return ret;
}
protected Object string() {
buf.setLength(0);
while (c != '"') {
if (c == '\\') {
next();
if (c == 'u') {
add(unicode());
} else {
Object value = escapes.get(Character.valueOf(c));
if (value != null) {
add(((Character) value).charValue());
}
}
} else {
add();
}
}
next();
return buf.toString();
}
protected void add(char cc) {
buf.append(cc);
next();
}
protected void add() {
add(c);
}
protected char unicode() {
int value = 0;
for (int i = 0; i < 4; ++i) {
switch (next()) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = (value << 4) + c - '0';
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
value = (value << 4) + (c - 'a') + 10;
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
value = (value << 4) + (c - 'A') + 10;
break;
}
}
return (char) value;
}
}