-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTinyJSON.cs
2843 lines (2593 loc) · 60.9 KB
/
TinyJSON.cs
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
namespace TinyJSON
{
public sealed partial class JSON
{
public abstract class Object : IEnumerable<string>
{
protected abstract IEnumerator<string> Keys();
public abstract Type Type(string key);
public abstract bool Get(string key, ref bool b);
public abstract bool Get(string key, ref int i);
public abstract bool Get(string key, ref double d);
public abstract bool Get(string key, ref string s);
public abstract bool Get(string key, ref Object o);
public abstract bool Get(string key, ref Array a);
public Value this[string key]
{
get
{
switch (Type(key))
{
case JSON.Type.Bool:
{
bool b = false;
if (Get(key, ref b))
return b;
}
break;
case JSON.Type.Int:
{
int i = 0;
if (Get(key, ref i))
return i;
}
break;
case JSON.Type.Double:
{
double d = 0;
if (Get(key, ref d))
return d;
}
break;
case JSON.Type.String:
{
string s = null;
if (Get(key, ref s))
return s;
}
break;
case JSON.Type.Object:
{
Object o = null;
if (Get(key, ref o))
return o;
}
break;
case JSON.Type.Array:
{
Array a = null;
if (Get(key, ref a))
return a;
}
break;
}
return Value.Null;
}
}
public IEnumerator<string> GetEnumerator()
{
return Keys();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public abstract class Array : IEnumerable<Value>
{
public abstract int Count { get; }
public abstract Type Type(int index);
public abstract bool Get(int index, ref bool b);
public abstract bool Get(int index, ref int i);
public abstract bool Get(int index, ref double d);
public abstract bool Get(int index, ref string s);
public abstract bool Get(int index, ref Object o);
public abstract bool Get(int index, ref Array a);
public Value this[int index]
{
get
{
switch (Type(index))
{
case JSON.Type.Bool:
{
bool b = false;
if (Get(index, ref b))
return b;
}
break;
case JSON.Type.Int:
{
int i = 0;
if (Get(index, ref i))
return i;
}
break;
case JSON.Type.Double:
{
double d = 0;
if (Get(index, ref d))
return d;
}
break;
case JSON.Type.String:
{
string s = null;
if (Get(index, ref s))
return s;
}
break;
case JSON.Type.Object:
{
Object o = null;
if (Get(index, ref o))
return o;
}
break;
case JSON.Type.Array:
{
Array a = null;
if (Get(index, ref a))
return a;
}
break;
}
return Value.Null;
}
}
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<Value> IEnumerable<Value>.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public struct Enumerator : IEnumerator<Value>
{
private readonly Array array;
private int index;
internal Enumerator(Array array)
{
this.array = array;
this.index = -1;
}
public void Dispose() {}
public bool MoveNext()
{
if (array == null)
return false;
if (++index >= array.Count)
return false;
return true;
}
public void Reset()
{
index = -1;
}
public Value Current
{
get { return array[index]; }
}
object IEnumerator.Current
{
get { return Current; }
}
}
}
public struct Value
{
public Type Type { get; private set; }
private bool b;
private int i;
private double d;
private string s;
private Object o;
private Array a;
public static readonly Value Null = new Value {Type = Type.Null};
public static implicit operator Value(bool b)
{
return new Value {Type = Type.Bool, b = b};
}
public static implicit operator Value(bool? b)
{
return b.HasValue ? new Value {Type = Type.Bool, b = b.Value} : Null;
}
public static implicit operator Value(int i)
{
return new Value {Type = Type.Int, i = i};
}
public static implicit operator Value(int? i)
{
return i.HasValue ? new Value {Type = Type.Int, i = i.Value} : Null;
}
public static implicit operator Value(double d)
{
return new Value {Type = Type.Double, d = d};
}
public static implicit operator Value(double? d)
{
return d.HasValue ? new Value {Type = Type.Double, d = d.Value} : Null;
}
public static implicit operator Value(string s)
{
return s != null ? new Value {Type = Type.String, s = s} : Null;
}
public static implicit operator Value(Object o)
{
return o != null ? new Value {Type = Type.Object, o = o} : Null;
}
public static implicit operator Value(Array a)
{
return a != null ? new Value {Type = Type.Array, a = a} : Null;
}
public static explicit operator bool(Value v)
{
bool? result = v.AsBool();
if (result.HasValue)
return result.Value;
throw new InvalidCastException();
}
public static explicit operator int(Value v)
{
int? result = v.AsInt();
if (result.HasValue)
return result.Value;
throw new InvalidCastException();
}
public static explicit operator double(Value v)
{
double? result = v.AsDouble();
if (result.HasValue)
return result.Value;
throw new InvalidCastException();
}
public static explicit operator string(Value v)
{
if (v.Type == Type.Null)
return null;
string result = v.AsString();
if (result != null)
return result;
throw new InvalidCastException();
}
public static explicit operator Object(Value v)
{
if (v.Type == Type.Null)
return null;
Object result = v.AsObject();
if (result != null)
return result;
throw new InvalidCastException();
}
public static explicit operator Array(Value v)
{
if (v.Type == Type.Null)
return null;
Array result = v.AsArray();
if (result != null)
return result;
throw new InvalidCastException();
}
public bool? AsBool()
{
if (Type == Type.Bool)
return b;
return null;
}
public int? AsInt()
{
if (Type == Type.Int)
return i;
double r = Math.Round(d);
if (Type == Type.Double && Math.Abs(d - r) < double.Epsilon && r >= int.MinValue && r <= int.MaxValue)
return (int)r;
return null;
}
public double? AsDouble()
{
if (Type == Type.Int)
return i;
if (Type == Type.Double)
return d;
return null;
}
public string AsString()
{
return Type == Type.String ? s : null;
}
public Object AsObject()
{
return Type == Type.Object ? o : null;
}
public Array AsArray()
{
return Type == Type.Array ? a : null;
}
}
public enum Type
{
Null,
Bool,
Int,
Double,
String,
Object,
Array,
}
#region 自定义事件接口
public interface Handler
{
bool StartArray();
bool StartTable();
bool EndArray();
bool EndTable();
bool Key(string key);
bool Null();
bool Bool(bool b);
bool String(string s);
bool Double(double d);
bool Int(int i);
bool Flush();
}
public interface Writer
{
bool Write(Handler handler);
}
public delegate void ValueAction(Value value);
public delegate bool WriteAction(Handler handler);
#endregion
#region 选项
public struct ParseOptions
{
public bool strict;
public bool comment;
public bool eof;
}
public struct DumpOptions
{
public bool pretty;
public bool escape;
}
#endregion
#region 默认接口
public static Handler DefaultHandler(ValueAction action)
{
return new NodeHandler {result = action};
}
public static Writer DefaultWriter(Value value)
{
return new NodeWriter(value);
}
public static Object From(Dictionary<string, Value> dict)
{
return new NodeObject(dict);
}
public static Array From(List<Value> list)
{
return new NodeArray(list);
}
#endregion
#region 对外解析接口
public bool Parse(TextReader reader, Handler handler, ParseOptions options)
{
input = reader;
token = input.Read();
if (!Skip())
return false;
if (!ReadValue(handler))
return false;
if (!handler.Flush())
return false;
return !options.eof || Flush();
}
public bool Parse(TextReader reader, Handler handler)
{
return Parse(reader, handler, new ParseOptions {strict = false, comment = true, eof = false});
}
public bool Parse(string text, Handler handler, ParseOptions options)
{
return Parse(new StringReader(text), handler, options);
}
public bool Parse(string text, Handler handler)
{
return Parse(new StringReader(text), handler, new ParseOptions {strict = false, comment = true, eof = true});
}
public Value? Parse(TextReader reader, ParseOptions options)
{
NodeHandler handler = new NodeHandler();
return Parse(reader, handler, options) ? handler.root : null;
}
public Value? Parse(TextReader reader)
{
NodeHandler handler = new NodeHandler();
return Parse(reader, handler) ? handler.root : null;
}
public Value? Parse(string text, ParseOptions options)
{
return Parse(new StringReader(text), options);
}
public Value? Parse(string text)
{
return Parse(new StringReader(text));
}
#endregion
#region 对外序列化接口
private Buffer normalbuffer;
private Buffer prettybuffer;
public bool Dump(Writer writer, Handler handler)
{
return writer.Write(handler);
}
public bool Dump(TextWriter output, Writer writer, DumpOptions options)
{
Buffer buffer;
if (options.pretty)
{
if (prettybuffer == null)
prettybuffer = new PrettyBuffer();
buffer = prettybuffer;
}
else
{
if (normalbuffer == null)
normalbuffer = new Buffer();
buffer = normalbuffer;
}
buffer.escape = options.escape;
buffer.writer = output;
buffer.Reset();
bool result = Dump(writer, buffer);
buffer.writer = null;
return result;
}
public bool Dump(TextWriter output, Writer writer)
{
return Dump(output, writer, new DumpOptions {pretty = false, escape = false});
}
public string Dump(Writer writer, DumpOptions options)
{
StringWriter output = new StringWriter();
return Dump(output, writer, options) ? output.ToString() : null;
}
public string Dump(Writer writer)
{
StringWriter output = new StringWriter();
return Dump(output, writer) ? output.ToString() : null;
}
public bool Dump(WriteAction writer, Handler handler)
{
return new ActionWriter(writer).Write(handler);
}
public bool Dump(TextWriter output, WriteAction writer, DumpOptions options)
{
return Dump(output, new ActionWriter(writer), options);
}
public bool Dump(TextWriter output, WriteAction writer)
{
return Dump(output, new ActionWriter(writer));
}
public string Dump(WriteAction writer, DumpOptions options)
{
return Dump(new ActionWriter(writer), options);
}
public string Dump(WriteAction writer)
{
return Dump(new ActionWriter(writer));
}
public bool Dump(Value value, Handler handler)
{
return new NodeWriter(value).Write(handler);
}
public bool Dump(Value? value, Handler handler)
{
return value.HasValue && new NodeWriter(value.Value).Write(handler);
}
public bool Dump(TextWriter output, Value value, DumpOptions options)
{
return Dump(output, new NodeWriter(value), options);
}
public bool Dump(TextWriter output, Value? value, DumpOptions options)
{
return value.HasValue && Dump(output, new NodeWriter(value.Value), options);
}
public bool Dump(TextWriter output, Value value)
{
return Dump(output, new NodeWriter(value));
}
public bool Dump(TextWriter output, Value? value)
{
return value.HasValue && Dump(output, new NodeWriter(value.Value));
}
public string Dump(Value value, DumpOptions options)
{
return Dump(new NodeWriter(value), options);
}
public string Dump(Value? value, DumpOptions options)
{
return value.HasValue ? Dump(new NodeWriter(value.Value), options) : null;
}
public string Dump(Value value)
{
return Dump(new NodeWriter(value));
}
public string Dump(Value? value)
{
return value.HasValue ? Dump(new NodeWriter(value.Value)) : null;
}
#endregion
#region 解析实现
private int token;
private TextReader input;
private StringBuilder builder;
private bool ReadValue(Handler handler)
{
switch (token)
{
case '{':
return ReadTable(handler);
case '[':
return ReadArray(handler);
case 't':
case 'f':
return ReadBool(handler);
case 'n':
return ReadNull(handler);
case '"':
case '\'':
return ReadString(handler);
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return ReadNumber(handler);
case '/':
Next();
switch (token)
{
case '/':
return ReadCommentLine();
case '*':
return ReadComments();
}
return false;
}
return false;
}
private bool ReadTable(Handler handler)
{
if (!handler.StartTable())
return false;
while (true)
{
Next();
if (!Skip())
return false;
if (token == '}')
{
Next();
if (!handler.EndTable())
return false;
break;
}
if (!ReadKey(handler))
return false;
if (!Skip())
return false;
if (token != ':')
return false;
Next();
if (!Skip())
return false;
if (!ReadValue(handler))
return false;
if (!Skip())
return false;
if (token == '}')
{
Next();
if (!handler.EndTable())
return false;
break;
}
if (token != ',')
return false;
}
return true;
}
private bool ReadArray(Handler handler)
{
if (!handler.StartArray())
return false;
while (true)
{
Next();
if (!Skip())
return false;
if (token == ']')
{
Next();
if (!handler.EndArray())
return false;
break;
}
if (!ReadValue(handler))
return false;
if (!Skip())
return false;
if (token == ']')
{
Next();
if (!handler.EndArray())
return false;
break;
}
if (token != ',')
return false;
}
return true;
}
private bool ReadBool(Handler handler)
{
if (token == 't')
{
if (Next() == 'r' && Next() == 'u' && Next() == 'e')
{
Next();
return handler.Bool(true);
}
}
else
{
if (Next() == 'a' && Next() == 'l' && Next() == 's' && Next() == 'e')
{
Next();
return handler.Bool(false);
}
}
return false;
}
private bool ReadNull(Handler handler)
{
if (Next() == 'u' && Next() == 'l' && Next() == 'l')
{
Next();
return handler.Null();
}
return false;
}
private bool ReadNumber(Handler handler)
{
bool minus = false;
if (token == '-')
{
minus = true;
Next();
}
if (token < '0' || token > '9')
return false;
uint u = 0;
double d = 0;
bool isfloat = false;
while (token >= '0' && token <= '9')
{
if (isfloat)
{
d = d * 10 + (token - '0');
}
else
{
uint uu = (uint)(token - '0');
if (u > int.MaxValue / 10 || (u == int.MaxValue / 10 && uu > int.MaxValue % 10))
{
isfloat = true;
d = u;
continue;
}
u = u * 10 + uu;
}
Next();
}
int dot = 0;
if (token == '.')
{
if (!isfloat)
{
isfloat = true;
d = u;
}
Next();
while (token >= '0' && token <= '9')
{
d = d * 10 + (token - '0');
--dot;
Next();
}
if (dot == 0)
return false;
}
int exp = 0;
if (token == 'E' || token == 'e')
{
if (!isfloat)
{
isfloat = true;
d = u;
}
bool expMinus = false;
Next();
if (token == '+')
{
Next();
}
else if (token == '-')
{
expMinus = true;
Next();
}
if (token < '0' || token > '9')
return false;
while (token >= '0' && token <= '9')
{
exp = exp * 10 + (token - '0');
Next();
}
if (expMinus)
exp = -exp;
}
return isfloat ? handler.Double((minus ? -d : d) * Pow10(dot + exp)) : handler.Int(minus ? (int)-u : (int)u);
}
private string ReadString()
{
char symbol = (char)token;
if (builder == null)
builder = new StringBuilder();
else
builder.Length = 0;
while (true)
{
Next();
if (token == symbol)
break;
if (token == -1)
return null;
if (token == '\\')
{
Next();
switch (token)
{
case 'b':
builder.Append('\b');
break;
case 'f':
builder.Append('\f');
break;
case 'n':
builder.Append('\n');
break;
case 'r':
builder.Append('\r');
break;
case 't':
builder.Append('\t');
break;
case '"':
builder.Append('"');
break;
case '\'':
builder.Append('\'');
break;
case '\\':
builder.Append('\\');
break;
case '/':
builder.Append('/');
break;
case 'u':
{
int unicode = 0;
for (int i = 0; i < 4; ++i)
{
int c = Next();
if (c >= 'a')
c -= 'a' - 10;
else if (c >= 'A')
c -= 'A' - 10;
else if (c >= '0' && c <= '9')
c -= '0';
else
return null;
unicode <<= 4;
unicode |= c;
}
builder.Append((char)unicode);
}
break;
default:
return null;
}
}
else
{
builder.Append((char)token);
}
}
Next();
return builder.ToString();
}
private bool ReadString(Handler handler)
{
string str = ReadString();
return str != null && handler.String(str);
}
private bool ReadKey(Handler handler)
{
if (token == '"' || token == '\'')
{
string str = ReadString();
return str != null && handler.Key(str);
}
if (token == '_' || (token >= 'a' && token <= 'z') || (token >= 'A' && token <= 'Z'))
{
if (builder == null)
builder = new StringBuilder();
else
builder.Length = 0;
while (true)
{
builder.Append((char)token);
Next();
if (token == '_' || (token >= 'a' && token <= 'z') || (token >= 'A' && token <= 'Z') || (token >= '0' && token <= '9'))
continue;
break;
}
return handler.Key(builder.ToString());
}
return false;
}
private bool ReadCommentLine()
{
while (true)
{
Next();
if (token == '\n')
{
Next();
return true;
}
if (token == -1)
return true;
}
}
private bool ReadComments()
{
while (true)
{
Next();
if (token == '*')
{
Next();
if (token == '/')
{
Next();
return true;
}
}
if (token == -1)
return false;
}
}
private bool Flush()
{
if (!Skip())
return false;
input = null;
return token == -1 || token == '\0';
}
private bool Skip()
{
while (token == ' ' || token == '\n' || token == '\r' || token == '\t')