forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleJson.cs
More file actions
837 lines (795 loc) · 19 KB
/
Copy pathSimpleJson.cs
File metadata and controls
837 lines (795 loc) · 19 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
using SimpleJson.Reflection;
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization;
using System.Text;
namespace SimpleJson
{
[GeneratedCode("simple-json", "1.0.0")]
internal static class SimpleJson
{
private const int TOKEN_NONE = 0;
private const int TOKEN_CURLY_OPEN = 1;
private const int TOKEN_CURLY_CLOSE = 2;
private const int TOKEN_SQUARED_OPEN = 3;
private const int TOKEN_SQUARED_CLOSE = 4;
private const int TOKEN_COLON = 5;
private const int TOKEN_COMMA = 6;
private const int TOKEN_STRING = 7;
private const int TOKEN_NUMBER = 8;
private const int TOKEN_TRUE = 9;
private const int TOKEN_FALSE = 10;
private const int TOKEN_NULL = 11;
private const int BUILDER_CAPACITY = 2000;
private static IJsonSerializerStrategy _currentJsonSerializerStrategy;
private static PocoJsonSerializerStrategy _pocoJsonSerializerStrategy;
public static IJsonSerializerStrategy CurrentJsonSerializerStrategy
{
get
{
IJsonSerializerStrategy arg_18_0;
if ((arg_18_0 = SimpleJson._currentJsonSerializerStrategy) == null)
{
arg_18_0 = (SimpleJson._currentJsonSerializerStrategy = SimpleJson.PocoJsonSerializerStrategy);
}
return arg_18_0;
}
set
{
SimpleJson._currentJsonSerializerStrategy = value;
}
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static PocoJsonSerializerStrategy PocoJsonSerializerStrategy
{
get
{
PocoJsonSerializerStrategy arg_18_0;
if ((arg_18_0 = SimpleJson._pocoJsonSerializerStrategy) == null)
{
arg_18_0 = (SimpleJson._pocoJsonSerializerStrategy = new PocoJsonSerializerStrategy());
}
return arg_18_0;
}
}
public static object DeserializeObject(string json)
{
object result;
if (SimpleJson.TryDeserializeObject(json, out result))
{
return result;
}
throw new SerializationException("Invalid JSON string");
}
public static bool TryDeserializeObject(string json, out object obj)
{
bool result = true;
if (json != null)
{
char[] json2 = json.ToCharArray();
int num = 0;
obj = SimpleJson.ParseValue(json2, ref num, ref result);
}
else
{
obj = null;
}
return result;
}
public static object DeserializeObject(string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy)
{
object obj = SimpleJson.DeserializeObject(json);
return (type != null && (obj == null || !ReflectionUtils.IsAssignableFrom(obj.GetType(), type))) ? (jsonSerializerStrategy ?? SimpleJson.CurrentJsonSerializerStrategy).DeserializeObject(obj, type) : obj;
}
public static object DeserializeObject(string json, Type type)
{
return SimpleJson.DeserializeObject(json, type, null);
}
public static T DeserializeObject<T>(string json, IJsonSerializerStrategy jsonSerializerStrategy)
{
return (T)((object)SimpleJson.DeserializeObject(json, typeof(T), jsonSerializerStrategy));
}
public static T DeserializeObject<T>(string json)
{
return (T)((object)SimpleJson.DeserializeObject(json, typeof(T), null));
}
public static string SerializeObject(object json, IJsonSerializerStrategy jsonSerializerStrategy)
{
StringBuilder stringBuilder = new StringBuilder(2000);
bool flag = SimpleJson.SerializeValue(jsonSerializerStrategy, json, stringBuilder);
return (!flag) ? null : stringBuilder.ToString();
}
public static string SerializeObject(object json)
{
return SimpleJson.SerializeObject(json, SimpleJson.CurrentJsonSerializerStrategy);
}
public static string EscapeToJavascriptString(string jsonString)
{
string result;
if (string.IsNullOrEmpty(jsonString))
{
result = jsonString;
}
else
{
StringBuilder stringBuilder = new StringBuilder();
int i = 0;
while (i < jsonString.Length)
{
char c = jsonString[i++];
if (c == '\\')
{
int num = jsonString.Length - i;
if (num >= 2)
{
char c2 = jsonString[i];
if (c2 == '\\')
{
stringBuilder.Append('\\');
i++;
}
else if (c2 == '"')
{
stringBuilder.Append("\"");
i++;
}
else if (c2 == 't')
{
stringBuilder.Append('\t');
i++;
}
else if (c2 == 'b')
{
stringBuilder.Append('\b');
i++;
}
else if (c2 == 'n')
{
stringBuilder.Append('\n');
i++;
}
else if (c2 == 'r')
{
stringBuilder.Append('\r');
i++;
}
}
}
else
{
stringBuilder.Append(c);
}
}
result = stringBuilder.ToString();
}
return result;
}
private static IDictionary<string, object> ParseObject(char[] json, ref int index, ref bool success)
{
IDictionary<string, object> dictionary = new JsonObject();
SimpleJson.NextToken(json, ref index);
bool flag = false;
IDictionary<string, object> result;
while (!flag)
{
int num = SimpleJson.LookAhead(json, index);
if (num != 0)
{
if (num == 6)
{
SimpleJson.NextToken(json, ref index);
}
else
{
if (num == 2)
{
SimpleJson.NextToken(json, ref index);
result = dictionary;
return result;
}
string key = SimpleJson.ParseString(json, ref index, ref success);
if (!success)
{
success = false;
result = null;
return result;
}
num = SimpleJson.NextToken(json, ref index);
if (num != 5)
{
success = false;
result = null;
return result;
}
object value = SimpleJson.ParseValue(json, ref index, ref success);
if (!success)
{
success = false;
result = null;
return result;
}
dictionary[key] = value;
}
continue;
}
success = false;
result = null;
return result;
}
result = dictionary;
return result;
}
private static JsonArray ParseArray(char[] json, ref int index, ref bool success)
{
JsonArray jsonArray = new JsonArray();
SimpleJson.NextToken(json, ref index);
bool flag = false;
JsonArray result;
while (!flag)
{
int num = SimpleJson.LookAhead(json, index);
if (num != 0)
{
if (num == 6)
{
SimpleJson.NextToken(json, ref index);
}
else
{
if (num == 4)
{
SimpleJson.NextToken(json, ref index);
break;
}
object item = SimpleJson.ParseValue(json, ref index, ref success);
if (!success)
{
result = null;
return result;
}
jsonArray.Add(item);
}
continue;
}
success = false;
result = null;
return result;
}
result = jsonArray;
return result;
}
private static object ParseValue(char[] json, ref int index, ref bool success)
{
object result;
switch (SimpleJson.LookAhead(json, index))
{
case 1:
result = SimpleJson.ParseObject(json, ref index, ref success);
return result;
case 3:
result = SimpleJson.ParseArray(json, ref index, ref success);
return result;
case 7:
result = SimpleJson.ParseString(json, ref index, ref success);
return result;
case 8:
result = SimpleJson.ParseNumber(json, ref index, ref success);
return result;
case 9:
SimpleJson.NextToken(json, ref index);
result = true;
return result;
case 10:
SimpleJson.NextToken(json, ref index);
result = false;
return result;
case 11:
SimpleJson.NextToken(json, ref index);
result = null;
return result;
}
success = false;
result = null;
return result;
}
private static string ParseString(char[] json, ref int index, ref bool success)
{
StringBuilder stringBuilder = new StringBuilder(2000);
SimpleJson.EatWhitespace(json, ref index);
char c = json[index++];
bool flag = false;
string result;
while (!flag)
{
if (index == json.Length)
{
break;
}
c = json[index++];
if (c == '"')
{
flag = true;
break;
}
if (c == '\\')
{
if (index == json.Length)
{
break;
}
c = json[index++];
if (c == '"')
{
stringBuilder.Append('"');
}
else if (c == '\\')
{
stringBuilder.Append('\\');
}
else if (c == '/')
{
stringBuilder.Append('/');
}
else if (c == 'b')
{
stringBuilder.Append('\b');
}
else if (c == 'f')
{
stringBuilder.Append('\f');
}
else if (c == 'n')
{
stringBuilder.Append('\n');
}
else if (c == 'r')
{
stringBuilder.Append('\r');
}
else if (c == 't')
{
stringBuilder.Append('\t');
}
else if (c == 'u')
{
int num = json.Length - index;
if (num >= 4)
{
uint num2;
if (!(success = uint.TryParse(new string(json, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out num2)))
{
result = "";
}
else
{
if (55296u > num2 || num2 > 56319u)
{
stringBuilder.Append(SimpleJson.ConvertFromUtf32((int)num2));
index += 4;
continue;
}
index += 4;
num = json.Length - index;
if (num >= 6)
{
uint num3;
if (new string(json, index, 2) == "\\u" && uint.TryParse(new string(json, index + 2, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out num3))
{
if (56320u <= num3 && num3 <= 57343u)
{
stringBuilder.Append((char)num2);
stringBuilder.Append((char)num3);
index += 6;
continue;
}
}
}
success = false;
result = "";
}
return result;
}
break;
}
}
else
{
stringBuilder.Append(c);
}
}
if (!flag)
{
success = false;
result = null;
return result;
}
result = stringBuilder.ToString();
return result;
}
private static string ConvertFromUtf32(int utf32)
{
if (utf32 < 0 || utf32 > 1114111)
{
throw new ArgumentOutOfRangeException("utf32", "The argument must be from 0 to 0x10FFFF.");
}
if (55296 <= utf32 && utf32 <= 57343)
{
throw new ArgumentOutOfRangeException("utf32", "The argument must not be in surrogate pair range.");
}
string result;
if (utf32 < 65536)
{
result = new string((char)utf32, 1);
}
else
{
utf32 -= 65536;
result = new string(new char[]
{
(char)((utf32 >> 10) + 55296),
(char)(utf32 % 1024 + 56320)
});
}
return result;
}
private static object ParseNumber(char[] json, ref int index, ref bool success)
{
SimpleJson.EatWhitespace(json, ref index);
int lastIndexOfNumber = SimpleJson.GetLastIndexOfNumber(json, index);
int length = lastIndexOfNumber - index + 1;
string text = new string(json, index, length);
object result;
if (text.IndexOf(".", StringComparison.OrdinalIgnoreCase) != -1 || text.IndexOf("e", StringComparison.OrdinalIgnoreCase) != -1)
{
double num;
success = double.TryParse(new string(json, index, length), NumberStyles.Any, CultureInfo.InvariantCulture, out num);
result = num;
}
else
{
long num2;
success = long.TryParse(new string(json, index, length), NumberStyles.Any, CultureInfo.InvariantCulture, out num2);
result = num2;
}
index = lastIndexOfNumber + 1;
return result;
}
private static int GetLastIndexOfNumber(char[] json, int index)
{
int i;
for (i = index; i < json.Length; i++)
{
if ("0123456789+-.eE".IndexOf(json[i]) == -1)
{
break;
}
}
return i - 1;
}
private static void EatWhitespace(char[] json, ref int index)
{
while (index < json.Length)
{
if (" \t\n\r\b\f".IndexOf(json[index]) == -1)
{
break;
}
index++;
}
}
private static int LookAhead(char[] json, int index)
{
int num = index;
return SimpleJson.NextToken(json, ref num);
}
private static int NextToken(char[] json, ref int index)
{
SimpleJson.EatWhitespace(json, ref index);
int result;
if (index != json.Length)
{
char c = json[index];
index++;
switch (c)
{
case ',':
result = 6;
return result;
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
result = 8;
return result;
case '.':
case '/':
IL_69:
switch (c)
{
case '[':
result = 3;
return result;
case '\\':
IL_7E:
switch (c)
{
case '{':
result = 1;
return result;
case '|':
IL_93:
if (c != '"')
{
index--;
int num = json.Length - index;
if (num >= 5)
{
if (json[index] == 'f' && json[index + 1] == 'a' && json[index + 2] == 'l' && json[index + 3] == 's' && json[index + 4] == 'e')
{
index += 5;
result = 10;
return result;
}
}
if (num >= 4)
{
if (json[index] == 't' && json[index + 1] == 'r' && json[index + 2] == 'u' && json[index + 3] == 'e')
{
index += 4;
result = 9;
return result;
}
}
if (num >= 4)
{
if (json[index] == 'n' && json[index + 1] == 'u' && json[index + 2] == 'l' && json[index + 3] == 'l')
{
index += 4;
result = 11;
return result;
}
}
result = 0;
return result;
}
result = 7;
return result;
case '}':
result = 2;
return result;
}
goto IL_93;
case ']':
result = 4;
return result;
}
goto IL_7E;
case ':':
result = 5;
return result;
}
goto IL_69;
}
result = 0;
return result;
}
private static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, object value, StringBuilder builder)
{
bool flag = true;
string text = value as string;
if (text != null)
{
flag = SimpleJson.SerializeString(text, builder);
}
else
{
IDictionary<string, object> dictionary = value as IDictionary<string, object>;
if (dictionary != null)
{
flag = SimpleJson.SerializeObject(jsonSerializerStrategy, dictionary.Keys, dictionary.Values, builder);
}
else
{
IDictionary<string, string> dictionary2 = value as IDictionary<string, string>;
if (dictionary2 != null)
{
flag = SimpleJson.SerializeObject(jsonSerializerStrategy, dictionary2.Keys, dictionary2.Values, builder);
}
else
{
IEnumerable enumerable = value as IEnumerable;
if (enumerable != null)
{
flag = SimpleJson.SerializeArray(jsonSerializerStrategy, enumerable, builder);
}
else if (SimpleJson.IsNumeric(value))
{
flag = SimpleJson.SerializeNumber(value, builder);
}
else if (value is bool)
{
builder.Append((!(bool)value) ? "false" : "true");
}
else if (value == null)
{
builder.Append("null");
}
else
{
object value2;
flag = jsonSerializerStrategy.TrySerializeNonPrimitiveObject(value, out value2);
if (flag)
{
SimpleJson.SerializeValue(jsonSerializerStrategy, value2, builder);
}
}
}
}
}
return flag;
}
private static bool SerializeObject(IJsonSerializerStrategy jsonSerializerStrategy, IEnumerable keys, IEnumerable values, StringBuilder builder)
{
builder.Append("{");
IEnumerator enumerator = keys.GetEnumerator();
IEnumerator enumerator2 = values.GetEnumerator();
bool flag = true;
bool result;
while (enumerator.MoveNext() && enumerator2.MoveNext())
{
object current = enumerator.Current;
object current2 = enumerator2.Current;
if (!flag)
{
builder.Append(",");
}
string text = current as string;
if (text != null)
{
SimpleJson.SerializeString(text, builder);
}
else if (!SimpleJson.SerializeValue(jsonSerializerStrategy, current2, builder))
{
result = false;
return result;
}
builder.Append(":");
if (SimpleJson.SerializeValue(jsonSerializerStrategy, current2, builder))
{
flag = false;
continue;
}
result = false;
return result;
}
builder.Append("}");
result = true;
return result;
}
private static bool SerializeArray(IJsonSerializerStrategy jsonSerializerStrategy, IEnumerable anArray, StringBuilder builder)
{
builder.Append("[");
bool flag = true;
IEnumerator enumerator = anArray.GetEnumerator();
bool result;
try
{
while (enumerator.MoveNext())
{
object current = enumerator.Current;
if (!flag)
{
builder.Append(",");
}
if (!SimpleJson.SerializeValue(jsonSerializerStrategy, current, builder))
{
result = false;
return result;
}
flag = false;
}
}
finally
{
IDisposable disposable;
if ((disposable = (enumerator as IDisposable)) != null)
{
disposable.Dispose();
}
}
builder.Append("]");
result = true;
return result;
}
private static bool SerializeString(string aString, StringBuilder builder)
{
builder.Append("\"");
char[] array = aString.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char c = array[i];
if (c == '"')
{
builder.Append("\\\"");
}
else if (c == '\\')
{
builder.Append("\\\\");
}
else if (c == '\b')
{
builder.Append("\\b");
}
else if (c == '\f')
{
builder.Append("\\f");
}
else if (c == '\n')
{
builder.Append("\\n");
}
else if (c == '\r')
{
builder.Append("\\r");
}
else if (c == '\t')
{
builder.Append("\\t");
}
else
{
builder.Append(c);
}
}
builder.Append("\"");
return true;
}
private static bool SerializeNumber(object number, StringBuilder builder)
{
if (number is long)
{
builder.Append(((long)number).ToString(CultureInfo.InvariantCulture));
}
else if (number is ulong)
{
builder.Append(((ulong)number).ToString(CultureInfo.InvariantCulture));
}
else if (number is int)
{
builder.Append(((int)number).ToString(CultureInfo.InvariantCulture));
}
else if (number is uint)
{
builder.Append(((uint)number).ToString(CultureInfo.InvariantCulture));
}
else if (number is decimal)
{
builder.Append(((decimal)number).ToString(CultureInfo.InvariantCulture));
}
else if (number is float)
{
builder.Append(((float)number).ToString(CultureInfo.InvariantCulture));
}
else
{
builder.Append(Convert.ToDouble(number, CultureInfo.InvariantCulture).ToString("r", CultureInfo.InvariantCulture));
}
return true;
}
private static bool IsNumeric(object value)
{
return value is sbyte || value is byte || value is short || value is ushort || value is int || value is uint || value is long || value is ulong || value is float || value is double || value is decimal;
}
}
}