-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCJSONScanner.m
515 lines (450 loc) · 13.1 KB
/
CJSONScanner.m
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
//
// CJSONScanner.m
// TouchJSON
//
// Created by Jonathan Wight on 12/07/2005.
// Copyright 2005 Toxic Software. All rights reserved.
//
#import "CJSONScanner.h"
#import "NSCharacterSet_Extensions.h"
#import "CDataScanner_Extensions.h"
#if !defined(TREAT_COMMENTS_AS_WHITESPACE)
#define TREAT_COMMENTS_AS_WHITESPACE 0
#endif // !defined(TREAT_COMMENTS_AS_WHITESPACE)
NSString *const kJSONScannerErrorDomain = @"CJSONScannerErrorDomain";
inline static int HexToInt(char inCharacter)
{
int theValues[] = { 0x0 /* 48 '0' */, 0x1 /* 49 '1' */, 0x2 /* 50 '2' */, 0x3 /* 51 '3' */, 0x4 /* 52 '4' */, 0x5 /* 53 '5' */, 0x6 /* 54 '6' */, 0x7 /* 55 '7' */, 0x8 /* 56 '8' */, 0x9 /* 57 '9' */, -1 /* 58 ':' */, -1 /* 59 ';' */, -1 /* 60 '<' */, -1 /* 61 '=' */, -1 /* 62 '>' */, -1 /* 63 '?' */, -1 /* 64 '@' */, 0xa /* 65 'A' */, 0xb /* 66 'B' */, 0xc /* 67 'C' */, 0xd /* 68 'D' */, 0xe /* 69 'E' */, 0xf /* 70 'F' */, -1 /* 71 'G' */, -1 /* 72 'H' */, -1 /* 73 'I' */, -1 /* 74 'J' */, -1 /* 75 'K' */, -1 /* 76 'L' */, -1 /* 77 'M' */, -1 /* 78 'N' */, -1 /* 79 'O' */, -1 /* 80 'P' */, -1 /* 81 'Q' */, -1 /* 82 'R' */, -1 /* 83 'S' */, -1 /* 84 'T' */, -1 /* 85 'U' */, -1 /* 86 'V' */, -1 /* 87 'W' */, -1 /* 88 'X' */, -1 /* 89 'Y' */, -1 /* 90 'Z' */, -1 /* 91 '[' */, -1 /* 92 '\' */, -1 /* 93 ']' */, -1 /* 94 '^' */, -1 /* 95 '_' */, -1 /* 96 '`' */, 0xa /* 97 'a' */, 0xb /* 98 'b' */, 0xc /* 99 'c' */, 0xd /* 100 'd' */, 0xe /* 101 'e' */, 0xf /* 102 'f' */, };
if (inCharacter >= '0' && inCharacter <= 'f')
return(theValues[inCharacter - '0']);
else
return(-1);
}
@interface CJSONScanner ()
- (BOOL)scanNotQuoteCharactersIntoString:(NSString **)outValue;
@end
#pragma mark -
@implementation CJSONScanner
- (id)init
{
if ((self = [super init]) != nil)
{
}
return(self);
}
- (void)dealloc
{
//
[super dealloc];
}
#pragma mark -
- (void)setData:(NSData *)inData
{
NSData *theData = inData;
if (theData && theData.length >= 4)
{
// This code is lame, but it works. Because the first character of any JSON string will always be a (ascii) control character we can work out the Unicode encoding by the bit pattern. See section 3 of http://www.ietf.org/rfc/rfc4627.txt
const char *theChars = theData.bytes;
NSStringEncoding theEncoding = NSUTF8StringEncoding;
if (theChars[0] != 0 && theChars[1] == 0)
{
if (theChars[2] != 0 && theChars[3] == 0)
theEncoding = NSUTF16LittleEndianStringEncoding;
else if (theChars[2] == 0 && theChars[3] == 0)
theEncoding = NSUTF32LittleEndianStringEncoding;
}
else if (theChars[0] == 0 && theChars[2] == 0 && theChars[3] != 0)
{
if (theChars[1] == 0)
theEncoding = NSUTF32BigEndianStringEncoding;
else if (theChars[1] != 0)
theEncoding = NSUTF16BigEndianStringEncoding;
}
if (theEncoding != NSUTF8StringEncoding)
{
NSString *theString = [[NSString alloc] initWithData:theData encoding:theEncoding];
theData = [theString dataUsingEncoding:NSUTF8StringEncoding];
[theString release];
}
}
[super setData:theData];
}
#pragma mark -
- (BOOL)scanJSONObject:(id *)outObject error:(NSError **)outError
{
[self skipWhitespace];
id theObject = NULL;
const unichar C = [self currentCharacter];
switch (C)
{
case 't':
if ([self scanUTF8String:"true" intoString:NULL])
{
theObject = [NSNumber numberWithBool:YES];
}
break;
case 'f':
if ([self scanUTF8String:"false" intoString:NULL])
{
theObject = [NSNumber numberWithBool:NO];
}
break;
case 'n':
if ([self scanUTF8String:"null" intoString:NULL])
{
theObject = [NSNull null];
}
break;
case '\"':
case '\'':
[self scanJSONStringConstant:&theObject error:outError];
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
[self scanJSONNumberConstant:&theObject error:outError];
break;
case '{':
[self scanJSONDictionary:&theObject error:outError];
break;
case '[':
[self scanJSONArray:&theObject error:outError];
break;
default:
break;
}
if (outObject != NULL)
*outObject = theObject;
return(YES);
}
- (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outError
{
NSUInteger theScanLocation = [self scanLocation];
if ([self scanCharacter:'{'] == NO)
{
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan dictionary. Dictionary that does not start with '{' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-1 userInfo:theUserInfo];
}
return(NO);
}
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
while ([self currentCharacter] != '}')
{
[self skipWhitespace];
if ([self currentCharacter] == '}')
break;
NSString *theKey = NULL;
if ([self scanJSONStringConstant:&theKey error:outError] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan dictionary. Failed to scan a key.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-2 userInfo:theUserInfo];
}
[theDictionary release];
return(NO);
}
[self skipWhitespace];
if ([self scanCharacter:':'] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan dictionary. Key was not terminated with a ':' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-3 userInfo:theUserInfo];
}
[theDictionary release];
return(NO);
}
id theValue = NULL;
if ([self scanJSONObject:&theValue error:outError] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan dictionary. Failed to scan a value.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-4 userInfo:theUserInfo];
}
[theDictionary release];
return(NO);
}
[theDictionary setValue:theValue forKey:theKey];
[self skipWhitespace];
if ([self scanCharacter:','] == NO)
{
if ([self currentCharacter] != '}')
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan dictionary. Key value pairs not delimited with a ',' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-5 userInfo:theUserInfo];
}
[theDictionary release];
return(NO);
}
break;
}
else
{
[self skipWhitespace];
if ([self currentCharacter] == '}')
break;
}
}
if ([self scanCharacter:'}'] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan dictionary. Dictionary not terminated by a '}' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-6 userInfo:theUserInfo];
}
[theDictionary release];
return(NO);
}
if (outDictionary != NULL)
*outDictionary = [[theDictionary copy] autorelease];
[theDictionary release];
return(YES);
}
- (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
{
NSUInteger theScanLocation = [self scanLocation];
if ([self scanCharacter:'['] == NO)
{
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan array. Array not started by a '{' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-7 userInfo:theUserInfo];
}
return(NO);
}
NSMutableArray *theArray = [[NSMutableArray alloc] init];
[self skipWhitespace];
while ([self currentCharacter] != ']')
{
NSString *theValue = NULL;
if ([self scanJSONObject:&theValue error:outError] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan array. Could not scan a value.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-8 userInfo:theUserInfo];
}
[theArray release];
return(NO);
}
[theArray addObject:theValue];
[self skipWhitespace];
if ([self scanCharacter:','] == NO)
{
[self skipWhitespace];
if ([self currentCharacter] != ']')
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan array. Array not terminated by a ']' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-9 userInfo:theUserInfo];
}
[theArray release];
return(NO);
}
break;
}
[self skipWhitespace];
}
[self skipWhitespace];
if ([self scanCharacter:']'] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan array. Array not terminated by a ']' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-10 userInfo:theUserInfo];
}
[theArray release];
return(NO);
}
if (outArray != NULL)
*outArray = [[theArray copy] autorelease];
[theArray release];
return(YES);
}
- (BOOL)scanJSONStringConstant:(NSString **)outStringConstant error:(NSError **)outError
{
NSUInteger theScanLocation = [self scanLocation];
[self skipWhitespace]; // TODO - i want to remove this method. But breaks unit tests.
NSMutableString *theString = [[NSMutableString alloc] init];
if ([self scanCharacter:'"'] == NO)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan string constant. String not started by a '\"' character.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-11 userInfo:theUserInfo];
}
[theString release];
return(NO);
}
while ([self scanCharacter:'"'] == NO)
{
NSString *theStringChunk = NULL;
if ([self scanNotQuoteCharactersIntoString:&theStringChunk])
{
[theString appendString:theStringChunk];
}
if ([self scanCharacter:'\\'] == YES)
{
unichar theCharacter = [self scanCharacter];
switch (theCharacter)
{
case '"':
case '\\':
case '/':
break;
case 'b':
theCharacter = '\b';
break;
case 'f':
theCharacter = '\f';
break;
case 'n':
theCharacter = '\n';
break;
case 'r':
theCharacter = '\r';
break;
case 't':
theCharacter = '\t';
break;
case 'u':
{
theCharacter = 0;
int theShift;
for (theShift = 12; theShift >= 0; theShift -= 4)
{
const int theDigit = HexToInt([self scanCharacter]);
if (theDigit == -1)
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan string constant. Unicode character could not be decoded.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-12 userInfo:theUserInfo];
}
[theString release];
return(NO);
}
theCharacter |= (theDigit << theShift);
}
}
break;
default:
{
[self setScanLocation:theScanLocation];
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan string constant. Unknown escape code.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-13 userInfo:theUserInfo];
}
[theString release];
return(NO);
}
break;
}
CFStringAppendCharacters((CFMutableStringRef)theString, &theCharacter, 1);
}
}
if (outStringConstant != NULL)
*outStringConstant = [[theString copy] autorelease];
[theString release];
return(YES);
}
- (BOOL)scanJSONNumberConstant:(NSNumber **)outNumberConstant error:(NSError **)outError
{
NSNumber *theNumber = NULL;
if ([self scanNumber:&theNumber] == YES)
{
if (outNumberConstant != NULL)
*outNumberConstant = theNumber;
return(YES);
}
else
{
if (outError)
{
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Could not scan number constant.", NSLocalizedDescriptionKey,
NULL];
*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-14 userInfo:theUserInfo];
}
return(NO);
}
}
#if TREAT_COMMENTS_AS_WHITESPACE
- (void)skipWhitespace
{
[super skipWhitespace];
[self scanCStyleComment:NULL];
[self scanCPlusPlusStyleComment:NULL];
[super skipWhitespace];
}
#endif // TREAT_COMMENTS_AS_WHITESPACE
#pragma mark -
- (BOOL)scanNotQuoteCharactersIntoString:(NSString **)outValue
{
u_int8_t *P;
for (P = current; P < end && *P != '\"' && *P != '\\'; ++P)
;
if (P == current)
{
return(NO);
}
if (outValue)
{
*outValue = [[[NSString alloc] initWithBytes:current length:P - current encoding:NSUTF8StringEncoding] autorelease];
}
current = P;
return(YES);
}
@end