forked from xlmnxp/Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson.alusus
286 lines (255 loc) · 9.02 KB
/
Json.alusus
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
import "Srl/Array.alusus";
import "Srl/String.alusus";
import "Srl/StringBuilder.alusus";
import "Srl/Nullable.alusus";
class Json {
use Srl;
def keys: Array[String];
def values: Array[Json];
def rawValue: String;
handler this~init() {}
handler this~init(str: ptr[array[Char]]) this.initialize(str);
handler this~init(json: ref[Json]) {
this = json;
}
handler this = ptr[array[Char]] this.initialize(value);
handler this = ref[Json] {
this.rawValue = value.rawValue
this.keys = value.keys
this.values = value.values
}
handler this.initialize(str: CharsPtr) {
this.rawValue.clear();
this.keys.clear();
this.values.clear();
this.parse(str);
}
handler this.parse(str: CharsPtr) {
while isSpace(str~cnt(0)) str = str + 1;
if str~cnt(0) == '{' {
str = str + 1;
while str~cnt(0) != 0 and str~cnt(0) != '}' {
def commaIndex: ArchInt = findSeparator(str, ',');
if commaIndex == 0 break;
def colonIndex: ArchInt = findSeparator(str, ':');
if colonIndex != 0 {
this.keys.add(parseString(str, colonIndex));
this.values.add(Json(str + colonIndex + 1));
}
str = str + commaIndex;
if str~cnt(0) == ',' str = str + 1;
}
} else if str~cnt(0) == '[' {
str = str + 1;
while str~cnt(0) != 0 and str~cnt(0) != ']' {
def commaIndex: ArchInt = findSeparator(str, ',');
if commaIndex == 0 break;
this.values.add(Json(str));
str = str + commaIndex;
if str~cnt(0) == ',' str = str + 1;
}
} else {
def i: ArchInt = 0;
def endIndex: ArchInt = 0;
def level: Int = 0;
while not isAtEnd(str~cnt(i), level, false) {
if str~cnt(i) == '\\' { ++i }
else if str~cnt(i) == '"' level = 1 - level;
if not isSpace(str~cnt(i)) endIndex = i + 1;
++i;
}
this.rawValue = String(str, endIndex);
}
}
func findSeparator(str: CharsPtr, separator: Char): ArchInt {
@shared def skipOperators: Array[Char]({ '{', '[', '"', '\'' });
@shared def skipOperatorsClose: Array[Char]({ '}', ']', '"', '\'' });
def stack: array[Char, 500];
def stackLen: Int = 0;
def start: ArchInt = 0;
def i: ArchInt;
for i = 0, not isAtEnd(str~cnt(i), stackLen, separator != ':'), ++i {
if str~cnt(i) == '\\' {
++i;
continue;
}
def opInd: Int;
if stackLen > 0 and str~cnt(i) == stack(stackLen - 1) {
--stackLen;
} else if stackLen == 0 or stack(stackLen - 1) != '"' {
for opInd = 0, opInd < skipOperators.getLength(), opInd++ {
if str~cnt(i) == skipOperators(opInd) {
stack(stackLen++) = skipOperatorsClose(opInd);
// Break if we reach the stack limit to avoid memory violation. This will only happen if the
// JSON file is 500 levels deep, which wouldn't happen in any practical situation.
if stackLen == 500 break 2;
}
}
}
}
return i;
};
func isAtEnd(c: Char, stackSize: Int, acceptColon: Bool): Bool {
if c == 0 return true;
return stackSize == 0 and (c == '}' or c == ']' or c == ',' or (c == ':' and not acceptColon));
}
func isSpace(c: Char): Bool {
return c == ' ' or c == '\n' or c == '\t' or c == '\r'
}
func parseString(input: CharsPtr, len: ArchInt): String {
def result: StringBuilder(len, (len + 1) / 2);
def inStr: Bool = false;
def i: ArchInt;
for i = 0, i < len, ++i {
if inStr {
if input~cnt(i) == '"' break;
if input~cnt(i) == '\\' {
++i;
if input~cnt(i) == 'n' result += '\n'
else if input~cnt(i) == 'r' result += '\r'
else if input~cnt(i) == 't' result += '\t'
else result += input~cnt(i);
} else {
result += input~cnt(i);
}
} else {
if input~cnt(i) == '"' inStr = true;
}
}
return result;
}
handler this.getLength(): Int {
return this.values.getLength();
}
handler this.getKey(i: Int): String {
if i < 0 or i >= this.keys.getLength() return String();
return this.keys(i);
}
handler this(key: CharsPtr): ref[Json] {
@shared def empty: Json;
def i: Int;
for i = 0, i < this.keys.getLength(), ++i {
if this.keys(i) == key {
return this.values(i);
}
}
return empty;
}
handler this(i: Int): ref[Json] {
return this.values(i);
}
handler this~cast[Nullable[String]] {
if this.isNull() return Nullable[String]()
else return Nullable[String](parseString(this.rawValue, this.rawValue.getLength()));
}
handler this~cast[Nullable[Int[64]]] {
if this.isNull() return Nullable[Int[64]]()
else return Nullable[Int[64]](String.parseInt(this.rawValue));
}
handler this~cast[Nullable[Int[32]]] {
if this.isNull() return Nullable[Int[32]]()
else return Nullable[Int[32]](String.parseInt(this.rawValue));
}
handler this~cast[Nullable[Float[64]]] {
if this.isNull() return Nullable[Float[64]]()
else return Nullable[Float[64]](String.parseFloat(this.rawValue));
}
handler this~cast[Nullable[Float[32]]] {
if this.isNull() return Nullable[Float[32]]()
else return Nullable[Float[32]](String.parseFloat(this.rawValue));
}
handler this~cast[Nullable[Bool]] {
if this.isNull() return Nullable[Bool]()
else if this.rawValue == "true" return Nullable[Bool](true)
else return Nullable[Bool](false);
}
handler this.isNull(): Bool {
if this.rawValue == "null" return true
else return false;
}
handler this.isObject(): Bool {
return this.keys.getLength() > 0;
}
handler this.isArray(): Bool {
return this.values.getLength() > 0 && this.keys.getLength() == 0;
}
function escape(str: CharsPtr): String {
def length: ArchInt = 0;
def i: ArchInt = 0;
while str~cnt(i) != 0 {
if str~cnt(i) == '\n'
or str~cnt(i) == '\r'
or str~cnt(i) == '\t'
or str~cnt(i) == '\\'
or str~cnt(i) == '"' length += 2
else ++length;
++i;
}
def res: String;
res.alloc(length);
length = 0;
i = 0;
while str~cnt(i) != 0 {
if str~cnt(i) == '\n' {
res.buf~cnt(length++) = '\\';
res.buf~cnt(length++) = 'n';
} else if str~cnt(i) == '\r' {
res.buf~cnt(length++) = '\\';
res.buf~cnt(length++) = 'r';
} else if str~cnt(i) == '\t' {
res.buf~cnt(length++) = '\\';
res.buf~cnt(length++) = 't';
} else if str~cnt(i) == '\\' {
res.buf~cnt(length++) = '\\';
res.buf~cnt(length++) = '\\';
} else if str~cnt(i) == '"' {
res.buf~cnt(length++) = '\\';
res.buf~cnt(length++) = '"';
} else {
res.buf~cnt(length++) = str~cnt(i);
}
++i;
}
res.buf~cnt(length++) = 0;
return res;
}
}
def JsonStringBuilderMixin: {
@format["jpc"]
handler this.formatJsonCharsPtr(str: ptr[Char]) {
this.append('"');
while str~cnt != 0 {
// For optimization purposes, we'll batch all non-escaped characters in a single append
// operation.
def c: Char;
def len: ArchInt = 0;
while 1 {
c = (str + len)~cnt;
if c == '\n' or c == '\r' or c == '\t' or c == '\\' or c == '"' or c == 0 break;
++len;
}
if len > 0 {
this.append(str~cast[CharsPtr], len);
str = str + len;
}
if c == 0 break;
if c == '\n' {
this.append("\\n");
} else if c == '\r' {
this.append("\\r");
} else if c == '\t' {
this.append("\\t");
} else if c == '\\' {
this.append("\\\\");
} else if c == '"' {
this.append("\\\"");
}
str = str + 1;
}
this.append('"');
}
@format["js"]
handler this.formatJsonCharsPtr(str: String) {
this.formatJsonCharsPtr(str.buf);
}
}