-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonParser.cs
483 lines (438 loc) · 18 KB
/
JsonParser.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace JsonParser
{
public class JsonParser
{
private readonly BinaryReader _binaryReader;
private const char OpenCurlyChar = '{';
private const char CloseCurlyChar = '}';
private const char OpenSquareChar = '[';
private const char CloseSquareChar = ']';
private const char ColonChar = ':';
private const char CommaChar = ',';
private const char QuoteChar = '"';
private const char DotChar = '.';
private const char SpaceChar = ' ';
private const char TabChar = '\t';
private const char CRChar = '\r';
private const char LFChar = '\n';
private const char SubChar = '-';
private const char AddChar = '+';
private const char BackslashChar = '\\';
private const long _true_token = 6774539739450702579;
private const long _false_token = 7096547112153268318;
private const long _null_token = 6774539739450526444;
public JsonParser(BinaryReader binaryReader)
{
_binaryReader = binaryReader;
}
public long ReadToken(out long tokenLength, out long tokenHash, out bool isString, out char initialChar)
{
var tokenPosition = 0L;
tokenLength = 0L;
tokenHash = 0L;
isString = false;
initialChar = default;
var hasToContinue = true;
int peek;
while (hasToContinue)
{
peek = _binaryReader.PeekChar();
if (peek >= 0)
{
var character = (char)peek;
switch (character)
{
case SpaceChar:
case TabChar:
case CRChar:
case LFChar:
{
_binaryReader.Read();
break;
}
default:
hasToContinue = false;
break;
}
}
else
{
hasToContinue = false;
}
}
peek = _binaryReader.PeekChar();
if (peek >= 0)
{
var character = (char)peek;
switch (character)
{
case OpenCurlyChar:
case CloseCurlyChar:
case OpenSquareChar:
case CloseSquareChar:
case ColonChar:
case CommaChar:
{
tokenPosition = _binaryReader.BaseStream.Position;
initialChar = (char)_binaryReader.Read();
tokenLength = _binaryReader.BaseStream.Position - tokenPosition;
break;
}
case QuoteChar:
{
isString = true;
initialChar = (char)peek;
tokenHash = HashUtils.GetHashInitialValue();
tokenPosition = _binaryReader.BaseStream.Position + 1;
var lastPeek = peek;
for (; ; )
{
_binaryReader.Read();
peek = _binaryReader.PeekChar();
if (peek == QuoteChar && lastPeek != BackslashChar)
{
break;
}
tokenHash = HashUtils.GetHash(tokenHash, peek);
lastPeek = peek;
}
_binaryReader.Read();
tokenLength = _binaryReader.BaseStream.Position - tokenPosition - 1;
break;
}
default:
{
initialChar = (char)peek;
tokenHash = HashUtils.GetHashInitialValue();
tokenPosition = _binaryReader.BaseStream.Position;
int lastPeek;
do
{
lastPeek = peek;
tokenHash = HashUtils.GetHash(tokenHash, peek);
_binaryReader.Read();
peek = _binaryReader.PeekChar();
} while (
peek != OpenCurlyChar &&
peek != CloseCurlyChar &&
peek != OpenSquareChar &&
peek != CloseSquareChar &&
peek != ColonChar &&
(peek != CommaChar && lastPeek != BackslashChar) &&
peek != QuoteChar &&
peek != SpaceChar &&
peek != TabChar &&
peek != CRChar &&
peek != LFChar
);
tokenLength = _binaryReader.BaseStream.Position - tokenPosition;
break;
}
}
}
return tokenPosition;
}
public void ParseValues(ref JsonValue parentValue, bool insideArray)
{
parseValue:
var tokenPosition = ReadToken(out var tokenLength, out var tokenHash, out var tokenIsString, out var tokenChar);
switch (tokenChar)
{
case OpenCurlyChar:
{
var value = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, JsonValueType.Object);
ParseKeysAndValues(ref value);
parentValue.AddChild(0, value);
break;
}
case OpenSquareChar:
{
var value = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, JsonValueType.Array);
ParseValues(ref value, true);
parentValue.AddChild(0, value);
break;
}
default:
{
if (tokenIsString)
{
var value = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, JsonValueType.String);
parentValue.AddChild(0, value);
break;
}
if (char.IsDigit(tokenChar) || tokenChar == DotChar || tokenChar == SubChar || tokenChar == AddChar)
{
var valueType = JsonValueType.Number;
var value = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, valueType);
parentValue.AddChild(0, value);
}
else
{
JsonValueType valueType;
switch (tokenHash)
{
case _true_token:
valueType = JsonValueType.True;
break;
case _false_token:
valueType = JsonValueType.False;
break;
case _null_token:
valueType = JsonValueType.Null;
break;
default:
valueType = JsonValueType.Unknown;
break;
}
var value = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, valueType);
parentValue.AddChild(0, value);
}
break;
}
}
if (insideArray)
{
tokenPosition = ReadToken(out tokenLength, out tokenHash, out tokenIsString, out tokenChar);
if (tokenChar == CommaChar)
{
goto parseValue;
}
if (tokenChar != CloseSquareChar)
{
throw new Exception("Expecting: square close");
}
}
}
private void ParseKeysAndValues(ref JsonValue parentValue)
{
parseValue:
var tokenPosition = ReadToken(out var tokenLength, out var tokenHash, out var tokenIsString, out var tokenChar);
if (tokenChar == CloseCurlyChar)
{
return;
}
var colonPosition = ReadToken(out var colonLength, out var colonHash, out var colonIsString, out var colonChar);
if (colonChar != ColonChar)
{
throw new Exception("Expecting: colon");
}
var value = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, JsonValueType.String);
ParseValues(ref value, false);
parentValue.AddChild(tokenHash, value);
tokenPosition = ReadToken(out tokenLength, out tokenHash, out tokenIsString, out tokenChar);
if (tokenChar == CommaChar)
{
goto parseValue;
}
if (tokenChar != CloseCurlyChar)
{
throw new Exception("Expecting: curly close");
}
}
public JsonValue ParseRootValue()
{
var tokenPosition = ReadToken(out var tokenLength, out var tokenHash, out var tokenString, out var tokenChar);
if (tokenChar != OpenCurlyChar)
{
throw new Exception("Expecting: object");
}
var rootValue = new JsonValue(_binaryReader, (int)tokenPosition, (int)tokenLength, JsonValueType.Object);
ParseKeysAndValues(ref rootValue);
return rootValue;
}
public enum JsonValueType
{
Object,
Array,
Number,
String,
True,
False,
Null,
Unknown
}
public struct JsonValue : IEnumerable<JsonValue>
{
public BinaryReader BinaryReader { get; }
public int Position { get; private set; }
public int ValueLength { get; private set; }
public JsonValueType Type { get; }
public bool Valid => ValueLength > 0;
private readonly Dictionary<long, int> _hashes;
private readonly List<JsonValue> _children;
public JsonValue(BinaryReader binaryReader, int position, int valueLength, JsonValueType type)
{
BinaryReader = binaryReader;
Position = position;
ValueLength = valueLength;
Type = type;
_children = new List<JsonValue>();
_hashes = new Dictionary<long, int>();
}
public int Count => _children.Count;
public void AddChild(long hash, JsonValue value)
{
if (_children != null)
{
_children.Add(value);
if (hash != 0 && _hashes != null && !_hashes.ContainsKey(hash))
{
_hashes.Add(hash, _children.Count - 1);
}
}
}
private JsonValue GetChildWithHash(long hash)
{
if (_children != null && _hashes != null && _hashes.TryGetValue(hash, out var index))
{
return _children[index];
}
return default;
}
public JsonValue GetChildAtIndex(int index)
{
return _children?[index] ?? default;
}
public JsonValue GetChildWithKey(long hash)
{
return GetChildWithHash(hash).GetChildAtIndex(0);
}
public override string ToString()
{
var sb = new StringBuilder();
using (var enumerator = GetCharEnumerator())
{
while (enumerator.MoveNext())
{
sb.Append(enumerator.Current);
}
}
return sb.ToString();
}
public IEnumerator<JsonValue> GetEnumerator()
{
return new JsonValueEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public JsonByteEnumerator GetByteEnumerator()
{
return new JsonByteEnumerator(this);
}
public JsonCharEnumerator GetCharEnumerator()
{
return new JsonCharEnumerator(this);
}
public JsonValue AddOffset(int offset)
{
Position += offset;
ValueLength -= offset;
return this;
}
public override int GetHashCode()
{
var hashCode = BinaryReader.GetHashCode();
hashCode = (hashCode * 397) ^ Position;
hashCode = (hashCode * 397) ^ (int)Type;
return hashCode;
}
public int CopyTo(char[] buffer)
{
BinaryReader.BaseStream.Position = Position;
return BinaryReader.Read(buffer, 0, ValueLength);
}
public struct JsonValueEnumerator : IEnumerator<JsonValue>
{
private JsonValue _jsonValue;
private int _position;
public JsonValueEnumerator(JsonParser.JsonValue jsonValue)
{
_jsonValue = jsonValue;
_position = -1;
}
public bool MoveNext()
{
return ++_position < _jsonValue.Count;
}
public void Reset()
{
_position = -1;
}
object IEnumerator.Current => Current;
public JsonValue Current => _jsonValue.GetChildAtIndex(_position);
public void Dispose()
{
}
}
public struct JsonByteEnumerator : IEnumerator<byte>, IDisposable
{
private readonly JsonValue _jsonValue;
private int _position;
private byte _byte;
private readonly long _initialPosition;
public JsonByteEnumerator(JsonParser.JsonValue jsonValue)
{
_jsonValue = jsonValue;
_initialPosition = _jsonValue.BinaryReader.BaseStream.Position;
_jsonValue.BinaryReader.BaseStream.Position = _jsonValue.Position;
_position = 0;
_byte = 0;
}
public bool MoveNext()
{
_byte = _jsonValue.BinaryReader.ReadByte();
return _position++ < _jsonValue.ValueLength;
}
public void Reset()
{
_position = 0;
_byte = 0;
}
object IEnumerator.Current => Current;
public byte Current => _byte;
public void Dispose()
{
_jsonValue.BinaryReader.BaseStream.Position = _initialPosition;
}
}
public struct JsonCharEnumerator : IEnumerator<char>, IDisposable
{
private readonly JsonValue _jsonValue;
private int _position;
private int _char;
private readonly long _initialPosition;
public JsonCharEnumerator(JsonParser.JsonValue jsonValue)
{
_jsonValue = jsonValue;
_initialPosition = _jsonValue.BinaryReader.BaseStream.Position;
_jsonValue.BinaryReader.BaseStream.Position = _jsonValue.Position;
_position = 0;
_char = -1;
}
public bool MoveNext()
{
_char = _jsonValue.BinaryReader.Read();
return _char > -1 && _position++ < _jsonValue.ValueLength;
}
public void Reset()
{
_position = 0;
_char = -1;
}
object IEnumerator.Current => Current;
public char Current => (char)_char;
public void Dispose()
{
_jsonValue.BinaryReader.BaseStream.Position = _initialPosition;
}
}
}
}
}