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
·221 lines (211 loc) · 5.86 KB
/
Copy pathJSON.java
File metadata and controls
executable file
·221 lines (211 loc) · 5.86 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
package javaforce;
/**
JSON parser (JavaScript Object Notation)
Does NOT Support multi-dimension arrays.
*/
import java.util.*;
/*
example:
{
"key": "value",
"key2" : ["v1", "v2"],
"key3" : {
"key" : "value",
"etc" : "value"
}
"key4" : [ "v1", "v2" ]
}
*/
public class JSON {
public static class Element {
public String key;
public String value;
public ArrayList<Element> children = new ArrayList<Element>();
}
/** Parses a JSON string. */
public static Element parse(String str) throws Exception {
Element root = new Element();
root.key = "root";
parseElement(root, str.trim());
return root;
}
/* reads key : returns str left over */
private static String readKey(Element e, String str) throws Exception {
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);
default:
e.key += ch;
}
}
}
}
private static String readArray(Element e, String key, String str) throws Exception {
while (true) {
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)) {
// JFLog.log(child.key + "=" + child.value);
e.children.add(child);
}
if (str.startsWith("]")) return str.substring(1);
}
}
/* reads value : returns str left over */
private static String readValue(Element e, String str, boolean array) throws Exception {
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 parseElement(e, str.substring(pos-1));
case '}':
if (e.value.length() > 0) {
return str.substring(pos);
}
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 parseElement(Element e, String str) throws Exception {
//str = { ... }
str = readOpen(str);
while (str.length() > 0) {
Element child = new Element();
str = readKey(child, str);
if (str.startsWith("}")) return str.substring(1);
str = readValue(child, str, false);
if (str.startsWith("[")) {
str = readArray(e, child.key, str.substring(1));
continue;
}
// JFLog.log(child.key + "=" + child.value);
e.children.add(child);
}
return str;
}
/* //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);
}
}
*/
}