Skip to content

Commit

Permalink
Modify the JSON parser to properly handle UTF-8 input
Browse files Browse the repository at this point in the history
  • Loading branch information
c99koder committed Feb 28, 2013
1 parent cd26891 commit d310fa3
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions SBJson/SBJsonStreamParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ - (SBJsonStreamParserStatus)parse:(NSData *)data_ {
}
}

- (unichar)decodeHexQuad:(char *)quad {
- (unichar)decodeHexQuad:(NSString *)quad offset:(int)offset {
unichar ch = 0;
for (NSUInteger i = 0; i < 4; i++) {
int c = quad[i];
int c = [quad characterAtIndex:i+offset];
ch *= 16;
switch (c) {
case '0' ... '9': ch += c - '0'; break;
Expand All @@ -292,12 +292,13 @@ - (unichar)decodeHexQuad:(char *)quad {
}

- (NSString*)decodeStringToken:(char*)bytes length:(NSUInteger)len {
NSString *input = [[NSString alloc] initWithBytes:bytes length:len encoding:NSUTF8StringEncoding];
NSMutableString *string = [NSMutableString stringWithCapacity:len];

for (NSUInteger i = 0; i < len;) {
switch (bytes[i]) {
for (NSUInteger i = 0; i < input.length;) {
switch ([input characterAtIndex:i]) {
case '\\': {
switch (bytes[++i]) {
switch ([input characterAtIndex:++i]) {
case '"': [string appendString:@"\""]; i++; break;
case '/': [string appendString:@"/"]; i++; break;
case '\\': [string appendString:@"\\"]; i++; break;
Expand All @@ -307,11 +308,11 @@ - (NSString*)decodeStringToken:(char*)bytes length:(NSUInteger)len {
case 'r': [string appendString:@"\r"]; i++; break;
case 't': [string appendString:@"\t"]; i++; break;
case 'u': {
unichar hi = [self decodeHexQuad:bytes + i + 1];
unichar hi = [self decodeHexQuad:input offset:i+1];
i += 5;
if (SBStringIsSurrogateHighCharacter(hi)) {
// Skip past \u that we know is there..
unichar lo = [self decodeHexQuad:bytes + i + 2];
unichar lo = [self decodeHexQuad:input offset:i+2];
i += 6;
[string appendFormat:@"%C%C", hi, lo];
} else {
Expand All @@ -323,7 +324,7 @@ - (NSString*)decodeStringToken:(char*)bytes length:(NSUInteger)len {
}
break;
}
default: [string appendFormat:@"%c", bytes[i++]]; break;
default: [string appendFormat:@"%C", [input characterAtIndex:i++]]; break;
}
}
return string;
Expand Down

0 comments on commit d310fa3

Please sign in to comment.