Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

Little fix to solve memory issues when parsing large json. #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions NSScanner+BSJSONAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,31 @@ - (BOOL)scanJSONObject:(NSDictionary **)dictionary
NSString *key = nil;
id value;
[self scanJSONWhiteSpace];

// add an autorelease pool to fix memory issues
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

int counter = 0;
while (([self scanJSONString:&key]) && ([self scanJSONKeyValueSeparator]) && ([self scanJSONValue:&value])) {
[jsonKeyValues setObject:value forKey:key];
[self scanJSONWhiteSpace];

//release the pool once every 500 iterations (500 is just a wild guess, should do more tests to find out the best value)
if (counter%500==0) {
[pool drain];
pool = [[NSAutoreleasePool alloc] init];
}
counter++;

// check to see if the character at scan location is a value separator. If it is, do nothing.
if ([[[self string] substringWithRange:NSMakeRange([self scanLocation], 1)] isEqualToString:jsonValueSeparatorString]) {
[self scanJSONValueSeparator];
}

}
//always release the pool at the end of the loop
[pool drain];

if ([self scanJSONObjectEndString]) {
// whether or not we found a key-val pair, we found open and close brackets - completing an object
result = YES;
Expand Down