-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathkeyvaluesjson.cpp
More file actions
714 lines (624 loc) · 16.8 KB
/
keyvaluesjson.cpp
File metadata and controls
714 lines (624 loc) · 16.8 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
//========= Copyright Valve Corporation, All rights reserved. =================//
//
// Read JSON-formatted data into KeyValues
//
//=============================================================================//
#include "tier1/keyvaluesjson.h"
#include "tier1/utlbuffer.h"
#include "tier1/strtools.h"
#include <stdint.h> // INT32_MIN defn
KeyValuesJSONParser::KeyValuesJSONParser( const CUtlBuffer &buf )
{
Init( (const char *)buf.Base(), buf.TellPut() );
}
KeyValuesJSONParser::KeyValuesJSONParser( const char *pszText, int cbSize )
{
Init( pszText, cbSize >= 0 ? cbSize : V_strlen(pszText) );
}
KeyValuesJSONParser::~KeyValuesJSONParser() {}
void KeyValuesJSONParser::Init( const char *pszText, int cbSize )
{
m_szErrMsg[0] = '\0';
m_nLine = 1;
m_cur = pszText;
m_end = pszText+cbSize;
m_eToken = kToken_Null;
NextToken();
}
KeyValues *KeyValuesJSONParser::ParseFile()
{
// A valid JSON object should contain a single object, surrounded by curly braces.
if ( m_eToken == kToken_EOF )
{
V_sprintf_safe( m_szErrMsg, "Input contains no data" );
return NULL;
}
if ( m_eToken == kToken_Err )
return NULL;
if ( m_eToken == '{' )
{
// Parse the the entire file as one big object
KeyValues *pResult = new KeyValues("");
if ( !ParseObject( pResult ) )
{
pResult->deleteThis();
return NULL;
}
if ( m_eToken == kToken_EOF )
return pResult;
pResult->deleteThis();
}
V_sprintf_safe( m_szErrMsg, "%s not expected here. A valid JSON document should be a single object, which begins with '{' and ends with '}'", GetTokenDebugText() );
return NULL;
}
bool KeyValuesJSONParser::ParseObject( KeyValues *pObject )
{
Assert( m_eToken == '{' );
int nOpenDelimLine = m_nLine;
NextToken();
KeyValues *pLastChild = NULL;
while ( m_eToken != '}' )
{
// Parse error?
if ( m_eToken == kToken_Err )
return false;
if ( m_eToken == kToken_EOF )
{
// Actually report the error at the line of the unmatched delimiter.
// There's no need to report the line number of the end of file, that is always
// useless.
m_nLine = nOpenDelimLine;
V_strcpy_safe( m_szErrMsg, "End of input was reached and '{' was not matched by '}'" );
return false;
}
// It must be a string, for the key name
if ( m_eToken != kToken_String )
{
V_sprintf_safe( m_szErrMsg, "%s not expected here; expected string for key name or '}'", GetTokenDebugText() );
return false;
}
KeyValues *pChildValue = new KeyValues( m_vecTokenChars.Base() );
NextToken();
// Expect and eat colon
if ( m_eToken != ':' )
{
V_sprintf_safe( m_szErrMsg, "%s not expected here. Missing ':'?", GetTokenDebugText() );
pChildValue->deleteThis();
return false;
}
NextToken();
// Recursively parse the value
if ( !ParseValue( pChildValue ) )
{
pChildValue->deleteThis();
return false;
}
// Add to parent.
pObject->AddSubkeyUsingKnownLastChild( pChildValue, pLastChild );
pLastChild = pChildValue;
// Eat the comma, if there is one. If no comma,
// then the other thing that could come next
// is the closing brace to close the object
// NOTE: We are allowing the extra comma after the last item
if ( m_eToken == ',' )
{
NextToken();
}
else if ( m_eToken != '}' )
{
V_sprintf_safe( m_szErrMsg, "%s not expected here. Missing ',' or '}'?", GetTokenDebugText() );
return false;
}
}
// Eat closing '}'
NextToken();
// Success
return true;
}
bool KeyValuesJSONParser::ParseArray( KeyValues *pArray )
{
Assert( m_eToken == '[' );
int nOpenDelimLine = m_nLine;
NextToken();
KeyValues *pLastChild = NULL;
int idx = 0;
while ( m_eToken != ']' )
{
// Parse error?
if ( m_eToken == kToken_Err )
return false;
if ( m_eToken == kToken_EOF )
{
// Actually report the error at the line of the unmatched delimiter.
// There's no need to report the line number of the end of file, that is always
// useless.
m_nLine = nOpenDelimLine;
V_strcpy_safe( m_szErrMsg, "End of input was reached and '[' was not matched by ']'" );
return false;
}
// Set a dummy key name based on the index
char szKeyName[ 32 ];
V_sprintf_safe( szKeyName, "%d", idx );
++idx;
KeyValues *pChildValue = new KeyValues( szKeyName );
// Recursively parse the value
if ( !ParseValue( pChildValue ) )
{
pChildValue->deleteThis();
return false;
}
// Add to parent.
pArray->AddSubkeyUsingKnownLastChild( pChildValue, pLastChild );
pLastChild = pChildValue;
// Handle a colon here specially. If one appears, the odds are they
// are trying to put object-like data inside of an array
if ( m_eToken == ':' )
{
V_sprintf_safe( m_szErrMsg, "':' not expected inside an array. ('[]' used when '{}' was intended?)" );
return false;
}
// Eat the comma, if there is one. If no comma,
// then the other thing that could come next
// is the closing brace to close the object
// NOTE: We are allowing the extra comma after the last item
if ( m_eToken == ',' )
{
NextToken();
}
else if ( m_eToken != ']' )
{
V_sprintf_safe( m_szErrMsg, "%s not expected here. Missing ',' or ']'?", GetTokenDebugText() );
return false;
}
}
// Eat closing ']'
NextToken();
// Success
return true;
}
bool KeyValuesJSONParser::ParseValue( KeyValues *pValue )
{
switch ( m_eToken )
{
case '{': return ParseObject( pValue );
case '[': return ParseArray( pValue );
case kToken_String:
pValue->SetString( NULL, m_vecTokenChars.Base() );
NextToken();
return true;
case kToken_NumberInt:
{
const char *pszNum = m_vecTokenChars.Base();
// Negative?
if ( *pszNum == '-' )
{
int64 val64 = V_atoi64( pszNum );
if ( val64 < INT32_MIN )
{
// !KLUDGE! KeyValues cannot support this!
V_sprintf_safe( m_szErrMsg, "%s is out of range for KeyValues, which doesn't support signed 64-bit numbers", pszNum );
return false;
}
pValue->SetInt( NULL, (int)val64 );
}
else
{
uint64 val64 = V_atoui64( pszNum );
if ( val64 > 0x7fffffffU )
{
pValue->SetUint64( NULL, val64 );
}
else
{
pValue->SetInt( NULL, (int)val64 );
}
}
NextToken();
return true;
}
case kToken_NumberFloat:
{
float f = V_atof( m_vecTokenChars.Base() );
pValue->SetFloat( NULL, f );
NextToken();
return true;
}
case kToken_True:
pValue->SetBool( NULL, true );
NextToken();
return true;
case kToken_False:
pValue->SetBool( NULL, false );
NextToken();
return true;
case kToken_Null:
pValue->SetPtr( NULL, NULL );
NextToken();
return true;
case kToken_Err:
return false;
}
V_sprintf_safe( m_szErrMsg, "%s not expected here; missing value?", GetTokenDebugText() );
return false;
}
void KeyValuesJSONParser::NextToken()
{
// Already in terminal state?
if ( m_eToken < 0 )
return;
// Clear token
m_vecTokenChars.SetCount(0);
// Scan until we hit the end of input
while ( m_cur < m_end )
{
// Next character?
char c = *m_cur;
switch (c)
{
// Whitespace? Eat it and keep parsing
case ' ':
case '\t':
++m_cur;
break;
// Newline? Eat it and advance line number
case '\n':
case '\r':
++m_nLine;
++m_cur;
// Eat \r\n or \n\r pair as a single character
if ( m_cur < m_end && *m_cur == ( '\n' + '\r' - c ) )
++m_cur;
break;
// Single-character JSON token?
case ':':
case '{':
case '}':
case '[':
case ']':
case ',':
m_eToken = c;
++m_cur;
return;
// String?
case '\"':
case '\'': // NOTE: We allow strings to be delimited by single quotes, which is not JSON compliant
ParseStringToken();
return;
case '-':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ParseNumberToken();
return;
// Literal "true"
case 't':
if ( m_cur + 4 <= m_end && m_cur[1] == 'r' && m_cur[2] == 'u' && m_cur[3] == 'e' )
{
m_cur += 4;
m_eToken = kToken_True;
return;
}
goto unexpected_char;
// Literal "false"
case 'f':
if ( m_cur + 5 <= m_end && m_cur[1] == 'a' && m_cur[2] == 'l' && m_cur[3] == 's' && m_cur[4] == 'e' )
{
m_cur += 5;
m_eToken = kToken_False;
return;
}
goto unexpected_char;
// Literal "null"
case 'n':
if ( m_cur + 4 <= m_end && m_cur[1] == 'u' && m_cur[2] == 'l' && m_cur[3] == 'l' )
{
m_cur += 4;
m_eToken = kToken_Null;
return;
}
goto unexpected_char;
case '/':
// C++-style comment?
if ( m_cur < m_end && m_cur[1] == '/' )
{
m_cur += 2;
while ( m_cur < m_end && *m_cur != '\n' && *m_cur != '\r' )
++m_cur;
// Leave newline as the next character, we'll handle it above
break;
}
// | fall
// | through
// V
default:
unexpected_char:
if ( V_isprint(c) )
V_sprintf_safe( m_szErrMsg, "Unexpected character 0x%02x ('%c')", (uint8)c, c );
else
V_sprintf_safe( m_szErrMsg, "Unexpected character 0x%02x", (uint8)c );
m_eToken = kToken_Err;
return;
}
}
m_eToken = kToken_EOF;
}
void KeyValuesJSONParser::ParseNumberToken()
{
// Clear token
m_vecTokenChars.SetCount(0);
// Eat leading minus sign
if ( *m_cur == '-' )
{
m_vecTokenChars.AddToTail( '-' );
++m_cur;
}
if ( m_cur >= m_end )
{
V_strcpy_safe( m_szErrMsg, "Unexpected EOF while parsing number" );
m_eToken = kToken_Err;
return;
}
char c = *m_cur;
m_vecTokenChars.AddToTail( c );
bool bHasWholePart = false;
switch ( c )
{
case '0':
// Leading 0 cannot be followed by any more digits, as per JSON spec (and to make sure nobody tries to parse octal).
++m_cur;
bHasWholePart = true;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
bHasWholePart = true;
++m_cur;
// Accumulate digits until we hit a non-digit
while ( m_cur < m_end && *m_cur >= '0' && *m_cur <= '9' )
m_vecTokenChars.AddToTail( *(m_cur++) );
break;
case '.':
// strict JSON doesn't allow a number that starts with a decimal point, but we do
break;
}
// Assume this is integral, unless we hit a decimal point and/or exponent
m_eToken = kToken_NumberInt;
// Fractional portion?
if ( m_cur < m_end && *m_cur == '.' )
{
m_eToken = kToken_NumberFloat;
// Eat decimal point
m_vecTokenChars.AddToTail( *(m_cur++) );
// Accumulate digits until we hit a non-digit
bool bHasFractionPart = false;
while ( m_cur < m_end && *m_cur >= '0' && *m_cur <= '9' )
{
m_vecTokenChars.AddToTail( *(m_cur++) );
bHasFractionPart = true;
}
// Make sure we aren't just a single '.'
if ( !bHasWholePart && !bHasFractionPart )
{
m_vecTokenChars.AddToTail(0);
V_sprintf_safe( m_szErrMsg, "Invalid number starting with '%s'", m_vecTokenChars.Base() );
m_eToken = kToken_Err;
return;
}
}
// Exponent?
if ( m_cur < m_end && ( *m_cur == 'e' || *m_cur == 'E' ) )
{
m_eToken = kToken_NumberFloat;
// Eat 'e'
m_vecTokenChars.AddToTail( *(m_cur++) );
// Optional sign
if ( m_cur < m_end && ( *m_cur == '-' || *m_cur == '+' ) )
m_vecTokenChars.AddToTail( *(m_cur++) );
// Accumulate digits until we hit a non-digit
bool bHasExponentDigit = false;
while ( m_cur < m_end && *m_cur >= '0' && *m_cur <= '9' )
{
m_vecTokenChars.AddToTail( *(m_cur++) );
bHasExponentDigit = true;
}
if ( !bHasExponentDigit )
{
V_strcpy_safe( m_szErrMsg, "Bad exponent in floating point number" );
m_eToken = kToken_Err;
return;
}
}
// OK, We have parsed a valid number.
// Terminate token
m_vecTokenChars.AddToTail( '\0' );
// EOF? That's OK for now, at this lexical parsing level. We'll handle the error
// at the higher parse level, when expecting a comma or closing delimiter
if ( m_cur >= m_end )
return;
// Is the next thing a valid character? This is the most common case.
c = *m_cur;
if ( V_isspace( c ) || c == ',' || c == '}' || c == ']' || c == '/' )
return;
// Handle these guys as "tokens", to provide a slightly more meaningful error message
if ( c == '[' || c == '{' )
return;
// Anything else, treat the whole thing as an invalid numerical constant
if ( V_isprint(c) )
V_sprintf_safe( m_szErrMsg, "Number contains invalid character 0x%02x ('%c')", (uint8)c, c );
else
V_sprintf_safe( m_szErrMsg, "Number contains invalid character 0x%02x", (uint8)c );
m_eToken = kToken_Err;
}
void KeyValuesJSONParser::ParseStringToken()
{
char cDelim = *(m_cur++);
while ( m_cur < m_end )
{
char c = *(m_cur++);
if ( c == '\r' || c == '\n' )
{
V_sprintf_safe( m_szErrMsg, "Hit end of line before closing quote (%c)", c );
m_eToken = kToken_Err;
return;
}
if ( c == cDelim )
{
m_eToken = kToken_String;
m_vecTokenChars.AddToTail( '\0' );
return;
}
// Ordinary character? Just append it
if ( c != '\\' )
{
m_vecTokenChars.AddToTail( c );
continue;
}
// Escaped character.
// End of string? We'll handle it above
if ( m_cur >= m_end )
continue;
// Check table of allowed escape characters
switch (c)
{
case '\\':
case '/':
case '\'':
case '\"': m_vecTokenChars.AddToTail( c ); break;
case 'b': m_vecTokenChars.AddToTail( '\b' ); break;
case 'f': m_vecTokenChars.AddToTail( '\f' ); break;
case 'n': m_vecTokenChars.AddToTail( '\n' ); break;
case 'r': m_vecTokenChars.AddToTail( '\r' ); break;
case 't': m_vecTokenChars.AddToTail( '\t' ); break;
case 'u':
{
// Make sure are followed by exactly 4 hex digits
if ( m_cur + 4 > m_end || !V_isxdigit( m_cur[0] ) || !V_isxdigit( m_cur[1] ) || !V_isxdigit( m_cur[2] ) || !V_isxdigit( m_cur[3] ) )
{
V_sprintf_safe( m_szErrMsg, "\\u must be followed by exactly 4 hex digits" );
m_eToken = kToken_Err;
return;
}
// Parse the codepoint
uchar32 nCodePoint = 0;
for ( int n = 0 ; n < 4 ; ++n )
{
nCodePoint <<= 4;
char chHex = *(m_cur++);
if ( chHex >= '0' && chHex <= '9' )
nCodePoint += chHex - '0';
else if ( chHex >= 'a' && chHex <= 'a' )
nCodePoint += chHex + 0x0a - 'a';
else if ( chHex >= 'A' && chHex <= 'A' )
nCodePoint += chHex + 0x0a - 'A';
else
Assert( false ); // inconceivable, due to above
}
// Encode it in UTF-8
char utf8Encode[8];
int r = Q_UChar32ToUTF8( nCodePoint, utf8Encode );
if ( r < 0 || r > 4 )
{
V_sprintf_safe( m_szErrMsg, "Invalid code point \\u%04x", nCodePoint );
m_eToken = kToken_Err;
return;
}
for ( int i = 0 ; i < r ; ++i )
m_vecTokenChars.AddToTail( utf8Encode[i] );
} break;
default:
if ( V_isprint(c) )
V_sprintf_safe( m_szErrMsg, "Invalid escape character 0x%02x ('\\%c')", (uint8)c, c );
else
V_sprintf_safe( m_szErrMsg, "Invalid escape character 0x%02x", (uint8)c );
m_eToken = kToken_Err;
return;
}
}
V_sprintf_safe( m_szErrMsg, "Hit end of input before closing quote (%c)", cDelim );
m_eToken = kToken_Err;
}
const char *KeyValuesJSONParser::GetTokenDebugText()
{
switch ( m_eToken )
{
case kToken_EOF: return "<EOF>";
case kToken_String: return "<string>";
case kToken_NumberInt:
case kToken_NumberFloat: return "<number>";
case kToken_True: return "'true'";
case kToken_False: return "'false'";
case kToken_Null: return "'null'";
case '{': return "'{'";
case '}': return "'}'";
case '[': return "'['";
case ']': return "']'";
case ':': return "':'";
case ',': return "','";
}
// We shouldn't ever need to ask for a debug string for the error token,
// and anything else is an error
Assert( false );
return "<parse error>";
}
#ifdef _DEBUG
static void JSONTest_ParseValid( const char *pszData )
{
KeyValuesJSONParser parser( pszData );
KeyValues *pFile = parser.ParseFile();
Assert( pFile );
pFile->deleteThis();
}
static void JSONTest_ParseInvalid( const char *pszData, const char *pszExpectedErrMsgSnippet, int nExpectedFailureLine )
{
KeyValuesJSONParser parser( pszData );
KeyValues *pFile = parser.ParseFile();
Assert( pFile == NULL );
Assert( V_stristr( parser.m_szErrMsg, pszExpectedErrMsgSnippet ) != NULL );
Assert( parser.m_nLine == nExpectedFailureLine );
}
void TestKeyValuesJSONParser()
{
JSONTest_ParseValid( "{}" );
JSONTest_ParseValid( R"JSON({
"key": "string_value",
"pos_int32": 123,
"pos_int64": 123456789012,
"neg_int32": -456,
"float": -45.23,
"pos_exponent": 1e30,
"neg_exponent": 1e-16,
"decimal_and_exponent": 1.e+30,
"no_leading_zero": .7, // we support this, even though strict JSON says it's no good
"zero": 0,
"true_value": true,
"false_value": false,
"null_value": null,
"with_escaped": "\r \t \n",
"unicode": "\u1234 \\u12f3",
"array_of_ints": [ 1, 2, 3, -45 ],
"empty_array": [],
"array_with_stuff_inside": [
{}, // this is a comment.
[ 0.45, {}, "hello!" ],
{ "id": 0 },
// Trailing comma above. Comment here
],
})JSON" );
JSONTest_ParseInvalid( "{ \"key\": 123", "missing", 1 );
JSONTest_ParseInvalid( "{ \"key\": 123.4f }", "number", 1 );
}
#endif