Permalink
Browse files

Fix for #9, YAJL parser handles double values and recursive arrays

  • Loading branch information...
1 parent 7568a61 commit 22c17d0fdefb677e17fa2431d13e11fbd9979845 @stevestreza stevestreza committed May 2, 2010
Showing with 40 additions and 12 deletions.
  1. +15 −2 MGTwitterStatusesYAJLParser.m
  2. +1 −0 MGTwitterYAJLParser.h
  3. +24 −10 MGTwitterYAJLParser.m
@@ -8,7 +8,7 @@
#import "MGTwitterStatusesYAJLParser.h"
-#define DEBUG_PARSING 0
+#define DEBUG_PARSING 1
@implementation MGTwitterStatusesYAJLParser
@@ -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]));
@@ -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
@@ -100,6 +110,9 @@ - (void)endArray
#if DEBUG_PARSING
NSLog(@"status: array end");
#endif
+
+ arrayDepth--;
+ [self clearCurrentKey];
}
- (void)dealloc
View
@@ -25,6 +25,7 @@
MGTwitterEngineDeliveryOptions deliveryOptions;
yajl_handle _handle;
+ NSUInteger arrayDepth;
}
+ (id)parserWithJSON:(NSData *)theJSON
View
@@ -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;
@@ -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;
@@ -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];
@@ -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

0 comments on commit 22c17d0

Please sign in to comment.