Skip to content

Commit

Permalink
Fix for mattgemmell#9, YAJL parser handles double values and recursiv…
Browse files Browse the repository at this point in the history
…e arrays
  • Loading branch information
stevestreza committed May 2, 2010
1 parent 7568a61 commit 22c17d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
17 changes: 15 additions & 2 deletions MGTwitterStatusesYAJLParser.m
Expand Up @@ -8,7 +8,7 @@

#import "MGTwitterStatusesYAJLParser.h"

#define DEBUG_PARSING 0
#define DEBUG_PARSING 1

@implementation MGTwitterStatusesYAJLParser

Expand All @@ -21,7 +21,12 @@ - (void)addValue:(id)value forKey:(NSString *)key
}

NSMutableDictionary *lastDictionary = [_dictionaries lastObject];
[lastDictionary setObject:value forKey:key];
if([[lastDictionary objectForKey:key] isKindOfClass:[NSArray class]]){
NSMutableArray *array = [lastDictionary objectForKey:key];
[array addObject:value];
}else{
[lastDictionary setObject:value forKey:key];
}

#if DEBUG_PARSING
NSLog(@"parsed item: %@ = %@ (%@)", key, value, NSStringFromClass([value class]));
Expand Down Expand Up @@ -90,6 +95,11 @@ - (void)endDictionary

- (void)startArrayWithKey:(NSString *)key
{
arrayDepth++;

NSMutableArray *newArray = [NSMutableArray array];
[self addValue:newArray forKey:key];

#if DEBUG_PARSING
NSLog(@"status: array start = %@", key);
#endif
Expand All @@ -100,6 +110,9 @@ - (void)endArray
#if DEBUG_PARSING
NSLog(@"status: array end");
#endif

arrayDepth--;
[self clearCurrentKey];
}

- (void)dealloc
Expand Down
1 change: 1 addition & 0 deletions MGTwitterYAJLParser.h
Expand Up @@ -25,6 +25,7 @@
MGTwitterEngineDeliveryOptions deliveryOptions;

yajl_handle _handle;
NSUInteger arrayDepth;
}

+ (id)parserWithJSON:(NSData *)theJSON
Expand Down
34 changes: 24 additions & 10 deletions MGTwitterYAJLParser.m
Expand Up @@ -34,8 +34,7 @@ int process_yajl_boolean(void * ctx, int boolVal)
{
[self addValue:[NSNumber numberWithBool:(BOOL)boolVal] forKey:currentKey];

[currentKey release];
currentKey = nil;
[self clearCurrentKey];
}

return 1;
Expand All @@ -49,13 +48,18 @@ int process_yajl_number(void *ctx, const char *numberVal, unsigned int numberLen
{
NSString *stringValue = [[NSString alloc] initWithBytesNoCopy:(void *)numberVal length:numberLen encoding:NSUTF8StringEncoding freeWhenDone:NO];

NSNumber *longLongValue = [NSNumber numberWithLongLong:[stringValue longLongValue]];
[self addValue:longLongValue forKey:currentKey];
// if there's a decimal, assume it's a double
if([stringValue rangeOfString:@"."].location != NSNotFound){
NSNumber *doubleValue = [NSNumber numberWithDouble:[stringValue doubleValue]];
[self addValue:doubleValue forKey:currentKey];
}else{
NSNumber *longLongValue = [NSNumber numberWithLongLong:[stringValue longLongValue]];
[self addValue:longLongValue forKey:currentKey];
}

[stringValue release];

[currentKey release];
currentKey = nil;
[self clearCurrentKey];
}

return 1;
Expand Down Expand Up @@ -98,19 +102,18 @@ int process_yajl_string(void *ctx, const unsigned char * stringVal, unsigned int
[self addValue:value forKey:currentKey];
}

[currentKey release];
currentKey = nil;
[self clearCurrentKey];
}

return 1;
}

int process_yajl_map_key(void *ctx, const unsigned char * stringVal, unsigned int stringLen)
{
id self = (id)ctx;
if (currentKey)
{
[currentKey release];
currentKey = nil;
[self clearCurrentKey];
}

currentKey = [[NSString alloc] initWithBytes:stringVal length:stringLen encoding:NSUTF8StringEncoding];
Expand Down Expand Up @@ -317,15 +320,26 @@ - (void)startArrayWithKey:(NSString *)key
// default implementation -- override in subclasses

NSLog(@"array start = %@", key);

arrayDepth++;
}

- (void)endArray
{
// default implementation -- override in subclasses

NSLog(@"array end");

arrayDepth--;
[self clearCurrentKey];
}

- (void)clearCurrentKey{
if(arrayDepth == 0){
[currentKey release];
currentKey = nil;
}
}

#pragma mark Delegate callbacks

Expand Down

0 comments on commit 22c17d0

Please sign in to comment.