-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathJSON.java
More file actions
executable file
·348 lines (338 loc) · 8.97 KB
/
JSON.java
File metadata and controls
executable file
·348 lines (338 loc) · 8.97 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package javaforce;
/**
*
* JSON parser (JavaScript Object Notation)
*
* https://en.wikipedia.org/wiki/JSON
*
* Does NOT Support multi-dimension arrays.
*
*/
import java.util.*;
import java.io.*;
/*
example:
{
"key": "value",
"key2" : ["v1", "v2"],
"key3" : {
"key" : "value",
"etc" : "value"
}
"key4" : [ "v1", "v2" ],
"key5" : [ { "t1": "v1"}, {"t2": "v2", "t2b": "v2b"}]
}
*/
public class JSON {
public static boolean debug = false;
public static class Element {
public String key;
public String value;
public ArrayList<Element> children = new ArrayList<Element>();
public Element getChild(String name) {
for(Element child : children) {
if (child.key.equals(name)) {
return child;
}
}
if (debug) JFLog.log("JSON:Child not found:" + name);
return null;
}
public Element getChild(int idx) {
return children.get(idx);
}
public int getChildCount() {
return children.size();
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append(key + "=" + value);
if (children.size() > 0) {
sb.append("[");
boolean first = true;
for(Element child : children) {
if (first) {
first = false;
} else {
sb.append(",");
}
sb.append(child.toString());
}
sb.append("]");
}
sb.append("}\r\n");
return sb.toString();
}
public void print() {
System.out.println(toString());
}
}
/** Parses a JSON string. */
public static Element parse(String str) throws Exception {
Element root = new Element();
root.key = "root";
JSON json = new JSON();
json.json = str.trim().toCharArray();
json.len = json.json.length;
json.readElement(root);
return root;
}
/** Parses a JSON string from InputStream. */
public static Element parseStream(InputStream is) throws Exception {
try {
byte[] data = is.readAllBytes();
return parse(new String(data, "UTF-8"));
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/** Parses a JSON string from a File. */
public static Element parseFile(String file) throws Exception {
try {
FileInputStream fis = new FileInputStream(file);
Element root = parseStream(fis);
fis.close();
return root;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
private char[] json;
private int len;
private int pos;
private void trim() {
while (pos < len && Character.isWhitespace(json[pos])) {
pos++;
}
}
private void readKey(Element e) throws Exception {
if (debug) JFLog.log("readKey:pos=" + pos);
boolean quote = false;
e.key = "";
while (true) {
char ch = json[pos++];
if (ch == '\"') {
if (quote) {
quote = false;
} else {
if (e.key.length() > 0) throw new Exception("bad key name:" + e.key + ":pos=" + pos);
quote = true;
}
continue;
}
if (quote) {
e.key += ch;
} else {
switch (ch) {
case ' ':
case '\r':
case '\n':
case '\t':
case ',':
if (e.key.length() > 0) return;
break;
case '}':
pos--;
return;
case ':':
if (e.key.length() == 0) throw new Exception("no key name:pos=" + pos);
pos--;
return;
case '{':
case '[':
case ']':
throw new Exception("bad key name:" + ch + ":" + e.key + ":pos=" + pos);
default:
e.key += ch;
}
}
}
}
private void readArray(Element parent) throws Exception {
if (debug) JFLog.log("readArray [");
char open = readToken();
if (open != '[') {
throw new Exception("bad array:pos=" + pos);
}
while (true) {
trim();
char ch = json[pos];
if (ch == ']') break;
Element child = new Element();
child.key = "";
readValue(child, true);
if ((child.value.length() > 0) || (child.children.size() > 0)) {
if (child.value.length() > 0) {
if (debug) JFLog.log("value=" + child.value);
}
parent.children.add(child);
}
char next = readToken();
if (next == ']') break;
if (next == ',') continue;
throw new Exception("bad array:pos=" + pos);
}
if (debug) JFLog.log("] //end of array");
}
private void readValue(Element e, boolean array) throws Exception {
if (debug) JFLog.log("readValue:array=" + array + ":pos=" + pos);
boolean quote = false, escape = false;
e.value = "";
while (true) {
if (!quote) trim();
char ch = json[pos++];
if ((ch == '\"') && (!escape)) {
if (quote) {
return;
} else {
if (e.value.length() > 0) throw new Exception("bad value:pos=" + pos);
quote = true;
}
continue;
}
if ((ch == '\\') && (!escape)) {
escape = true;
continue;
}
if (quote) {
if (escape) {
switch (ch) {
case 'n': e.value += "\n"; break;
case 'r': e.value += "\r"; break;
case 't': e.value += "\t"; break;
case 'b': e.value += "\b"; break;
case 'f': e.value += "\f"; break;
default: e.value += ch; break;
}
escape = false;
} else {
e.value += ch;
}
} else {
switch (ch) {
case ',':
pos--;
return;
case ':':
if (e.value.length() > 0) throw new Exception("bad value:pos=" + pos);
break;
case '{':
if (e.value.length() > 0) throw new Exception("bad value:pos=" + pos);
pos--;
readElement(e);
return;
case '}':
if (array) throw new Exception("bad array:pos=" + pos);
pos--;
return;
case '[':
if (e.value.length() > 0) throw new Exception("bad value:pos=" + pos);
pos--;
readArray(e);
return;
case ']':
if (!array) throw new Exception("bad array:pos=" + pos);
pos--;
return;
default: {
if (escape) {
switch (ch) {
case 'n': e.value += "\n"; break;
case 'r': e.value += "\r"; break;
case 't': e.value += "\t"; break;
case 'b': e.value += "\b"; break;
case 'f': e.value += "\f"; break;
default: e.value += ch; break;
}
escape = false;
} else {
e.value += ch;
}
}
}
}
}
}
private char readToken() throws Exception {
trim();
return json[pos++];
}
private void readElement(Element e) throws Exception {
//str = { ... }
if (debug) JFLog.log("readElement {");
char open = readToken();
if (open != '{') {
throw new Exception("bad element:pos=" + pos);
}
while (pos < len) {
trim();
char ch = json[pos];
if (ch == '}') break;
Element child = new Element();
readKey(child);
if (debug) JFLog.log("key=" + child.key);
char eq = readToken();
if (eq != ':') {
throw new Exception("bad element:pos=" + pos);
}
readValue(child, false);
if (debug) JFLog.log("value=" + child.value);
e.children.add(child);
char next = readToken();
if (next == '}') break;
if (next == ',') continue;
throw new Exception("bad element:next=" + next + ":pos=" + pos);
}
if (debug) JFLog.log("} //end of element");
}
/** Escape string to valid json text. */
public static String escape(String in) {
if (in == null) return "null";
StringBuilder sb = new StringBuilder();
char ca[] = in.toCharArray();
for(char ch : ca) {
if (ch < ' ') {
sb.append(String.format("\\u%04x", (int)ch)); //UTF-16
} else if (ch == '"') {
sb.append("\\\"");
} else if (ch == '\\') {
sb.append("\\\\");
} else {
sb.append(ch);
}
}
return sb.toString();
}
/* //remove JSON to XML support for now
public static XML toXML(Element e) {
XML xml = new XML();
xml.root.setName(e.key);
xml.root.setContent(e.value);
addXML(e, xml, xml.root);
return xml;
}
private static void addXML(Element e, XML xml, XML.XMLTag tag) {
for(int a=0;a<e.children.size();a++) {
Element c = e.children.get(a);
XML.XMLTag child = xml.addTag(tag, c.key, "", c.value);
addXML(c, xml, child);
}
}
*/
public static void main(String[] args) {
try {
if (args.length > 1) {
if (args[1].equals("debug")) {
JSON.debug = true;
}
}
Element test = JSON.parseFile(args[0]);
test.print();
} catch (Exception e) {
JFLog.log(e);
}
}
}