forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.java
More file actions
executable file
·288 lines (279 loc) · 7.63 KB
/
Copy pathJSON.java
File metadata and controls
executable file
·288 lines (279 loc) · 7.63 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
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;
}
}
return null;
}
public String toString() {
return key + "=" + value;
}
}
/** Parses a JSON string. */
public static Element parse(String str) throws Exception {
Element root = new Element();
root.key = "root";
readElement(root, str.trim());
return root;
}
public static Element parseFile(String file) throws Exception {
try {
FileInputStream fis = new FileInputStream(file);
byte[] data = fis.readAllBytes();
fis.close();
return parse(new String(data, "UTF-8"));
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
/* reads key : returns str left over */
private static String readKey(Element e, String str) throws Exception {
if (debug) JFLog.log("readKey");
boolean quote = false;
int pos = 0;
e.key = "";
while (true) {
char ch = str.charAt(pos++);
if (ch == '\"') {
if (quote) {
quote = false;
} else {
if (e.key.length() > 0) throw new Exception("bad key name");
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 str.substring(pos);
break;
case '}':
return str.substring(pos-1);
case ':':
if (e.key.length() == 0) throw new Exception("no key name");
return str.substring(pos);
case '{':
case '[':
case ']':
throw new Exception("bad key name:" + ch + ":" + e.key + ":" + str.substring(pos, pos+10));
default:
e.key += ch;
}
}
}
}
private static String readArray(Element e, String key, String str) throws Exception {
if (debug) JFLog.log("readArray [");
while (true) {
str = str.trim();
if (str.startsWith("]")) {
if (debug) JFLog.log("]");
return str.substring(1);
}
Element child = new Element();
child.key = key; //repeat key for each array element
str = readValue(child, str, true);
if ((child.value.length() > 0) || (child.children.size() > 0)) {
if (debug) JFLog.log(child.key + "=" + child.value);
e.children.add(child);
}
}
}
/* reads value : returns str left over */
private static String readValue(Element e, String str, boolean array) throws Exception {
if (debug) JFLog.log("readValue");
boolean quote = false, escape = false;
int pos = 0;
e.value = "";
while (true) {
char ch = str.charAt(pos++);
if ((ch == '\"') && (!escape)) {
if (quote) {
return str.substring(pos);
} else {
if (e.value.length() > 0) throw new Exception("bad value");
quote = true;
}
continue;
}
if ((ch == '\\') && (!escape)) {
escape = true;
}
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 ' ':
case '\r':
case '\n':
case '\t':
case ',':
if (e.value.length() > 0) {
return str.substring(pos);
}
break;
case ':':
if (e.value.length() > 0) throw new Exception("bad value");
break;
case '{':
if (e.value.length() > 0) throw new Exception("bad value");
return readElement(e, str.substring(pos-1));
case '}':
if (e.value.length() > 0) {
return str.substring(pos-1);
}
throw new Exception("bad value");
case '[':
if (e.value.length() > 0) throw new Exception("bad value");
return str.substring(pos-1);
case ']':
if (!array) throw new Exception("bad array");
return str.substring(pos-1);
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 static String readOpen(String str) throws Exception {
int pos = 0;
while (true) {
char ch = str.charAt(pos++);
switch (ch) {
case '{': return str.substring(pos);
case ' ':
case 9:
continue;
}
throw new Exception("bad json string");
}
}
private static String readElement(Element e, String str) throws Exception {
//str = { ... }
if (debug) JFLog.log("readTuple {");
str = readOpen(str);
while (str.length() > 0) {
if (str.startsWith("}")) {
if (debug) JFLog.log("}");
return str.substring(1);
}
if (debug) JFLog.log("char=" + str.charAt(0));
Element child = new Element();
str = readKey(child, str);
if (debug) JFLog.log("key=" + child.key);
str = readValue(child, str, false);
if (str.startsWith("[")) {
str = readArray(e, child.key, str.substring(1));
continue;
}
if (debug) JFLog.log("value=" + child.value);
e.children.add(child);
}
return str;
}
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 {
JSON.debug = true;
JSON.parseFile(args[0]);
} catch (Exception e) {
JFLog.log(e);
}
}
}