forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONParser.cs
More file actions
389 lines (369 loc) · 7.34 KB
/
Copy pathJSONParser.cs
File metadata and controls
389 lines (369 loc) · 7.34 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
namespace UnityEditorInternal
{
internal class JSONParser
{
private string json;
private int line;
private int linechar;
private int len;
private int idx;
private int pctParsed;
private char cur;
private static char[] endcodes = new char[]
{
'\\',
'"'
};
public JSONParser(string jsondata)
{
this.json = jsondata + " ";
this.line = 1;
this.linechar = 1;
this.len = this.json.Length;
this.idx = 0;
this.pctParsed = 0;
}
public static JSONValue SimpleParse(string jsondata)
{
JSONParser jSONParser = new JSONParser(jsondata);
JSONValue result;
try
{
result = jSONParser.Parse();
return result;
}
catch (JSONParseException ex)
{
Debug.LogError(ex.Message);
}
result = new JSONValue(null);
return result;
}
public JSONValue Parse()
{
this.cur = this.json[this.idx];
return this.ParseValue();
}
private char Next()
{
if (this.cur == '\n')
{
this.line++;
this.linechar = 0;
}
this.idx++;
if (this.idx >= this.len)
{
throw new JSONParseException("End of json while parsing at " + this.PosMsg());
}
this.linechar++;
int num = (int)((float)this.idx * 100f / (float)this.len);
if (num != this.pctParsed)
{
this.pctParsed = num;
}
this.cur = this.json[this.idx];
return this.cur;
}
private void SkipWs()
{
string text = " \n\t\r";
while (text.IndexOf(this.cur) != -1)
{
this.Next();
}
}
private string PosMsg()
{
return "line " + this.line.ToString() + ", column " + this.linechar.ToString();
}
private JSONValue ParseValue()
{
this.SkipWs();
char c = this.cur;
switch (c)
{
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
JSONValue result = this.ParseNumber();
return result;
}
case '.':
case '/':
{
IL_4B:
JSONValue result;
if (c == '"')
{
result = this.ParseString();
return result;
}
if (c == '[')
{
result = this.ParseArray();
return result;
}
if (c == 'f' || c == 'n' || c == 't')
{
result = this.ParseConstant();
return result;
}
if (c != '{')
{
throw new JSONParseException("Cannot parse json value starting with '" + this.json.Substring(this.idx, 5) + "' at " + this.PosMsg());
}
result = this.ParseDict();
return result;
}
}
goto IL_4B;
}
private JSONValue ParseArray()
{
this.Next();
this.SkipWs();
List<JSONValue> list = new List<JSONValue>();
while (this.cur != ']')
{
list.Add(this.ParseValue());
this.SkipWs();
if (this.cur == ',')
{
this.Next();
this.SkipWs();
}
}
this.Next();
return new JSONValue(list);
}
private JSONValue ParseDict()
{
this.Next();
this.SkipWs();
Dictionary<string, JSONValue> dictionary = new Dictionary<string, JSONValue>();
while (this.cur != '}')
{
JSONValue jSONValue = this.ParseValue();
if (!jSONValue.IsString())
{
throw new JSONParseException("Key not string type at " + this.PosMsg());
}
this.SkipWs();
if (this.cur != ':')
{
throw new JSONParseException("Missing dict entry delimiter ':' at " + this.PosMsg());
}
this.Next();
dictionary.Add(jSONValue.AsString(), this.ParseValue());
this.SkipWs();
if (this.cur == ',')
{
this.Next();
this.SkipWs();
}
}
this.Next();
return new JSONValue(dictionary);
}
private JSONValue ParseString()
{
string text = "";
this.Next();
while (this.idx < this.len)
{
int num = this.json.IndexOfAny(JSONParser.endcodes, this.idx);
if (num < 0)
{
throw new JSONParseException("missing '\"' to end string at " + this.PosMsg());
}
text += this.json.Substring(this.idx, num - this.idx);
if (this.json[num] == '"')
{
this.cur = this.json[num];
this.idx = num;
break;
}
num++;
if (num >= this.len)
{
throw new JSONParseException("End of json while parsing while parsing string at " + this.PosMsg());
}
char c = this.json[num];
switch (c)
{
case 'r':
text += '\r';
goto IL_29E;
case 's':
IL_E6:
if (c != '"')
{
if (c != '/')
{
if (c != '\\')
{
if (c == 'b')
{
text += '\b';
goto IL_29E;
}
if (c == 'f')
{
text += '\f';
goto IL_29E;
}
if (c != 'n')
{
throw new JSONParseException(string.Concat(new object[]
{
"Invalid escape char '",
c,
"' near ",
this.PosMsg()
}));
}
text += '\n';
goto IL_29E;
}
}
}
text += c;
goto IL_29E;
case 't':
text += '\t';
goto IL_29E;
case 'u':
{
string text2 = "";
if (num + 4 >= this.len)
{
throw new JSONParseException("End of json while parsing while parsing unicode char near " + this.PosMsg());
}
text2 += this.json[num + 1];
text2 += this.json[num + 2];
text2 += this.json[num + 3];
text2 += this.json[num + 4];
try
{
int num2 = int.Parse(text2, NumberStyles.AllowHexSpecifier);
text += (char)num2;
}
catch (FormatException)
{
throw new JSONParseException("Invalid unicode escape char near " + this.PosMsg());
}
num += 4;
goto IL_29E;
}
}
goto IL_E6;
IL_29E:
this.idx = num + 1;
}
if (this.idx >= this.len)
{
throw new JSONParseException("End of json while parsing while parsing string near " + this.PosMsg());
}
this.cur = this.json[this.idx];
this.Next();
return new JSONValue(text);
}
private JSONValue ParseNumber()
{
string text = "";
if (this.cur == '-')
{
text = "-";
this.Next();
}
while (this.cur >= '0' && this.cur <= '9')
{
text += this.cur;
this.Next();
}
if (this.cur == '.')
{
this.Next();
text += '.';
while (this.cur >= '0' && this.cur <= '9')
{
text += this.cur;
this.Next();
}
}
if (this.cur == 'e' || this.cur == 'E')
{
text += "e";
this.Next();
if (this.cur != '-' && this.cur != '+')
{
text += this.cur;
this.Next();
}
while (this.cur >= '0' && this.cur <= '9')
{
text += this.cur;
this.Next();
}
}
JSONValue result;
try
{
float num = Convert.ToSingle(text);
result = new JSONValue(num);
}
catch (Exception)
{
throw new JSONParseException("Cannot convert string to float : '" + text + "' at " + this.PosMsg());
}
return result;
}
private JSONValue ParseConstant()
{
string a = string.Concat(new object[]
{
"",
this.cur,
this.Next(),
this.Next(),
this.Next()
});
this.Next();
JSONValue result;
if (!(a == "true"))
{
if (a == "fals")
{
if (this.cur == 'e')
{
this.Next();
result = new JSONValue(false);
return result;
}
}
else if (a == "null")
{
result = new JSONValue(null);
return result;
}
throw new JSONParseException("Invalid token at " + this.PosMsg());
}
result = new JSONValue(true);
return result;
}
}
}