Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Search YAJL parser handles embedded dictionaries

  • Loading branch information...
commit 036c41033d7005bf20a4d4493724a45095613124 1 parent e10f548
Michael Tomasello authored richardbuckle committed
View
4 MGTwitterSearchYAJLParser.h
@@ -11,10 +11,10 @@
#import "MGTwitterYAJLParser.h"
@interface MGTwitterSearchYAJLParser : MGTwitterYAJLParser {
- BOOL insideMetadata;
BOOL insideArray;
- NSMutableDictionary *_results;
NSMutableDictionary *_status;
+ NSMutableArray *_dictionaries; // effectively a stack for parsing nested dictionaries
+ NSMutableArray *_dictionaryKeys;
}
@end
View
86 MGTwitterSearchYAJLParser.m
@@ -14,16 +14,27 @@ @implementation MGTwitterSearchYAJLParser
- (void)addValue:(id)value forKey:(NSString *)key
{
- if (_results)
+ if (insideArray)
{
- [_results setObject:value forKey:key];
+ //if for some reason there are no dictionaries, exit here
+ if (!_dictionaries || [_dictionaries count] == 0)
+ {
+ return;
+ }
+
+ NSMutableDictionary *lastDictionary = [_dictionaries lastObject];
+ [lastDictionary setObject:value forKey:key];
+
#if DEBUG_PARSING
NSLog(@"search: results: %@ = %@ (%@)", key, value, NSStringFromClass([value class]));
#endif
}
- else if (_status)
+ else
{
- [_status setObject:value forKey:key];
+ if (_status)
+ {
+ [_status setObject:value forKey:key];
+ }
#if DEBUG_PARSING
NSLog(@"search: status: %@ = %@ (%@)", key, value, NSStringFromClass([value class]));
#endif
@@ -35,17 +46,25 @@ - (void)startDictionaryWithKey:(NSString *)key
#if DEBUG_PARSING
NSLog(@"search: dictionary start = %@", key);
#endif
- if ([key isEqualToString:@"metadata"])
- {
- insideMetadata = YES;
- }
-
if (insideArray)
{
- if (! _results)
+ if (!_dictionaries)
{
- _results = [[NSMutableDictionary alloc] initWithCapacity:0];
+ _dictionaries = [[NSMutableArray alloc] init];
}
+
+ if (!_dictionaryKeys)
+ {
+ _dictionaryKeys = [[NSMutableArray alloc] init];
+ }
+
+ //add a new dictionary to the array
+ NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
+ [_dictionaries addObject:newDictionary];
+ [newDictionary release];
+
+ //add a key for the above dictionary to the array
+ [_dictionaryKeys addObject:(key) ? key : @""];
}
else
{
@@ -58,23 +77,35 @@ - (void)startDictionaryWithKey:(NSString *)key
- (void)endDictionary
{
- if (insideMetadata)
- {
- insideMetadata = NO;
- return;
- }
-
if (insideArray)
{
- if (_results)
+ if (_dictionaries && _dictionaryKeys && [_dictionaries count] > 0 && [_dictionaryKeys count] > 0)
{
- [_results setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
+ //is this the root dictionary?
+ if ([_dictionaries count] == 1)
+ {
+ //one dictionary left, so it must be the root
+ NSMutableDictionary *rootDictionary = [_dictionaries lastObject];
- [self _parsedObject:_results];
-
- [parsedObjects addObject:_results];
- [_results release];
- _results = nil;
+ //set the request type in the root dictionary
+ [rootDictionary setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
+
+ //send the root dictionary to the super class
+ [self _parsedObject:rootDictionary];
+ [parsedObjects addObject:rootDictionary];
+ }
+ else
+ {
+ //child dictionary found
+ //add the child dictionary to its parent dictionary
+ NSMutableDictionary *parentDictionary = [_dictionaries objectAtIndex:[_dictionaries count] - 2];
+ [parentDictionary setObject:[_dictionaries lastObject] forKey:[_dictionaryKeys lastObject]];
+ }
+
+ //remove the last dictionary since it has been joined with its parent (or was the root dictionary)
+ //also remove the corresponding key
+ [_dictionaries removeLastObject];
+ [_dictionaryKeys removeLastObject];
}
}
else
@@ -82,7 +113,7 @@ - (void)endDictionary
if (_status)
{
[_status setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
-
+
[parsedObjects addObject:_status];
[_status release];
_status = nil;
@@ -112,9 +143,10 @@ - (void)endArray
- (void)dealloc
{
- [_results release];
+ [_dictionaries release];
+ [_dictionaryKeys release];
[_status release];
-
+
[super dealloc];
}
View
4 MGTwitterStatusesYAJLParser.h
@@ -11,8 +11,8 @@
#import "MGTwitterYAJLParser.h"
@interface MGTwitterStatusesYAJLParser : MGTwitterYAJLParser {
- NSMutableArray *_dictionaries; //an array of NSMutableDictionary objects
- NSMutableArray *_dictionaryKeys; //an array of NSMutableDictionary keys corresponding to _dictionaries
+ NSMutableArray *_dictionaries; // effectively a stack for parsing nested dictionaries
+ NSMutableArray *_dictionaryKeys;
}
@end
View
3  MGTwitterStatusesYAJLParser.m
@@ -20,7 +20,6 @@ - (void)addValue:(id)value forKey:(NSString *)key
return;
}
- //add the item to its dictionary
NSMutableDictionary *lastDictionary = [_dictionaries lastObject];
[lastDictionary setObject:value forKey:key];
@@ -35,13 +34,11 @@ - (void)startDictionaryWithKey:(NSString *)key
NSLog(@"status: dictionary start = %@", key);
#endif
- //initialize the array that will hold all of the dictionaries if it doesn't exist yet
if (!_dictionaries)
{
_dictionaries = [[NSMutableArray alloc] init];
}
- //initialize the array that will hold all of the dictionary keys if it doesn't exist yet
if (!_dictionaryKeys)
{
_dictionaryKeys = [[NSMutableArray alloc] init];
Please sign in to comment.
Something went wrong with that request. Please try again.