2
Copyright (C) 2009 Stig Brautaset. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation
12
and/or other materials provided with the distribution.
14
* Neither the name of the author nor the names of its contributors may be used
15
to endorse or promote products derived from this software without specific
16
prior written permission.
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#import "SBJsonParser.h"
32
@interface SBJsonParser ()
34
- (BOOL)scanValue:(NSObject **)o;
36
- (BOOL)scanRestOfArray:(NSMutableArray **)o;
37
- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o;
38
- (BOOL)scanRestOfNull:(NSNull **)o;
39
- (BOOL)scanRestOfFalse:(NSNumber **)o;
40
- (BOOL)scanRestOfTrue:(NSNumber **)o;
41
- (BOOL)scanRestOfString:(NSMutableString **)o;
43
// Cannot manage without looking at the first digit
44
- (BOOL)scanNumber:(NSNumber **)o;
46
- (BOOL)scanHexQuad:(unichar *)x;
47
- (BOOL)scanUnicodeChar:(unichar *)x;
53
#define skipWhitespace(c) while (isspace(*c)) c++
54
#define skipDigits(c) while (isdigit(*c)) c++
57
@implementation SBJsonParser
59
static char ctrl[0x22];
65
for (int i = 1; i < 0x20; i++)
71
@deprecated This exists in order to provide fragment support in older APIs in one more version.
72
It should be removed in the next major version.
74
- (id)fragmentWithString:(id)repr {
75
[self clearErrorTrace];
78
[self addErrorWithCode:EINPUT description:@"Input was 'nil'"];
83
c = [repr UTF8String];
86
if (![self scanValue:&o]) {
90
// We found some valid JSON. But did it also contain something else?
91
if (![self scanIsAtEnd]) {
92
[self addErrorWithCode:ETRAILGARBAGE description:@"Garbage after JSON"];
96
NSAssert1(o, @"Should have a valid object from %@", repr);
100
- (id)objectWithString:(NSString *)repr {
102
id o = [self fragmentWithString:repr];
106
// Check that the object we've found is a valid JSON container.
107
if (![o isKindOfClass:[NSDictionary class]] && ![o isKindOfClass:[NSArray class]]) {
108
[self addErrorWithCode:EFRAGMENT description:@"Valid fragment, but not JSON"];
116
In contrast to the public methods, it is an error to omit the error parameter here.
118
- (BOOL)scanValue:(NSObject **)o
124
return [self scanRestOfDictionary:(NSMutableDictionary **)o];
127
return [self scanRestOfArray:(NSMutableArray **)o];
130
return [self scanRestOfString:(NSMutableString **)o];
133
return [self scanRestOfFalse:(NSNumber **)o];
136
return [self scanRestOfTrue:(NSNumber **)o];
139
return [self scanRestOfNull:(NSNull **)o];
143
c--; // cannot verify number correctly without the first character
144
return [self scanNumber:(NSNumber **)o];
147
[self addErrorWithCode:EPARSENUM description: @"Leading + disallowed in number"];
151
[self addErrorWithCode:EEOF description:@"Unexpected end of string"];
155
[self addErrorWithCode:EPARSE description: @"Unrecognised leading character"];
160
NSAssert(0, @"Should never get here");
164
- (BOOL)scanRestOfTrue:(NSNumber **)o
166
if (!strncmp(c, "rue", 3)) {
168
*o = [NSNumber numberWithBool:YES];
171
[self addErrorWithCode:EPARSE description:@"Expected 'true'"];
175
- (BOOL)scanRestOfFalse:(NSNumber **)o
177
if (!strncmp(c, "alse", 4)) {
179
*o = [NSNumber numberWithBool:NO];
182
[self addErrorWithCode:EPARSE description: @"Expected 'false'"];
186
- (BOOL)scanRestOfNull:(NSNull **)o {
187
if (!strncmp(c, "ull", 3)) {
192
[self addErrorWithCode:EPARSE description: @"Expected 'null'"];
196
- (BOOL)scanRestOfArray:(NSMutableArray **)o {
197
if (maxDepth && ++depth > maxDepth) {
198
[self addErrorWithCode:EDEPTH description: @"Nested too deep"];
202
*o = [NSMutableArray arrayWithCapacity:8];
208
if (*c == ']' && c++) {
213
if (![self scanValue:&v]) {
214
[self addErrorWithCode:EPARSE description:@"Expected value while parsing array"];
221
if (*c == ',' && c++) {
224
[self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in array"];
230
[self addErrorWithCode:EEOF description: @"End of input while parsing array"];
234
- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o
236
if (maxDepth && ++depth > maxDepth) {
237
[self addErrorWithCode:EDEPTH description: @"Nested too deep"];
241
*o = [NSMutableDictionary dictionaryWithCapacity:7];
247
if (*c == '}' && c++) {
252
if (!(*c == '\"' && c++ && [self scanRestOfString:&k])) {
253
[self addErrorWithCode:EPARSE description: @"Object key string expected"];
259
[self addErrorWithCode:EPARSE description: @"Expected ':' separating key and value"];
264
if (![self scanValue:&v]) {
265
NSString *string = [NSString stringWithFormat:@"Object value expected for key: %@", k];
266
[self addErrorWithCode:EPARSE description: string];
270
[*o setObject:v forKey:k];
273
if (*c == ',' && c++) {
276
[self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in object"];
282
[self addErrorWithCode:EEOF description: @"End of input while parsing object"];
286
- (BOOL)scanRestOfString:(NSMutableString **)o
288
*o = [NSMutableString stringWithCapacity:16];
290
// First see if there's a portion we can grab in one go.
291
// Doing this caused a massive speedup on the long string.
292
size_t len = strcspn(c, ctrl);
295
id t = [[NSString alloc] initWithBytesNoCopy:(char*)c
297
encoding:NSUTF8StringEncoding
310
} else if (*c == '\\') {
318
case 'b': uc = '\b'; break;
319
case 'n': uc = '\n'; break;
320
case 'r': uc = '\r'; break;
321
case 't': uc = '\t'; break;
322
case 'f': uc = '\f'; break;
326
if (![self scanUnicodeChar:&uc]) {
327
[self addErrorWithCode:EUNICODE description: @"Broken unicode character"];
333
[self addErrorWithCode:EESCAPE description: [NSString stringWithFormat:@"Illegal escape sequence '0x%x'", uc]];
337
CFStringAppendCharacters((CFMutableStringRef)*o, &uc, 1);
340
} else if (*c < 0x20) {
341
[self addErrorWithCode:ECTRL description: [NSString stringWithFormat:@"Unescaped control character '0x%x'", *c]];
345
NSLog(@"should not be able to get here");
349
[self addErrorWithCode:EEOF description:@"Unexpected EOF while parsing string"];
353
- (BOOL)scanUnicodeChar:(unichar *)x
357
if (![self scanHexQuad:&hi]) {
358
[self addErrorWithCode:EUNICODE description: @"Missing hex quad"];
362
if (hi >= 0xd800) { // high surrogate char?
363
if (hi < 0xdc00) { // yes - expect a low char
365
if (!(*c == '\\' && ++c && *c == 'u' && ++c && [self scanHexQuad:&lo])) {
366
[self addErrorWithCode:EUNICODE description: @"Missing low character in surrogate pair"];
370
if (lo < 0xdc00 || lo >= 0xdfff) {
371
[self addErrorWithCode:EUNICODE description:@"Invalid low surrogate char"];
375
hi = (hi - 0xd800) * 0x400 + (lo - 0xdc00) + 0x10000;
377
} else if (hi < 0xe000) {
378
[self addErrorWithCode:EUNICODE description:@"Invalid high character in surrogate pair"];
387
- (BOOL)scanHexQuad:(unichar *)x
390
for (int i = 0; i < 4; i++) {
393
int d = (uc >= '0' && uc <= '9')
394
? uc - '0' : (uc >= 'a' && uc <= 'f')
395
? (uc - 'a' + 10) : (uc >= 'A' && uc <= 'F')
396
? (uc - 'A' + 10) : -1;
398
[self addErrorWithCode:EUNICODE description:@"Missing hex digit in quad"];
407
- (BOOL)scanNumber:(NSNumber **)o
411
// The logic to test for validity of the number formatting is relicensed
412
// from JSON::XS with permission from its author Marc Lehmann.
413
// (Available at the CPAN: http://search.cpan.org/dist/JSON-XS/ .)
418
if ('0' == *c && c++) {
420
[self addErrorWithCode:EPARSENUM description: @"Leading 0 disallowed in number"];
424
} else if (!isdigit(*c) && c != ns) {
425
[self addErrorWithCode:EPARSENUM description: @"No digits after initial minus"];
433
if ('.' == *c && c++) {
436
[self addErrorWithCode:EPARSENUM description: @"No digits after decimal point"];
443
if ('e' == *c || 'E' == *c) {
446
if ('-' == *c || '+' == *c)
450
[self addErrorWithCode:EPARSENUM description: @"No digits after exponent"];
456
id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns
458
encoding:NSUTF8StringEncoding
461
if (str && (*o = [NSDecimalNumber decimalNumberWithString:str]))
464
[self addErrorWithCode:EPARSENUM description: @"Failed creating decimal instance"];