Permalink
Browse files

Added TouchJSON implementation

  • Loading branch information...
1 parent 9463f27 commit 3f3348639252d8c2af4d9f104b2073beb0072b71 @stevestreza stevestreza committed with richardbuckle Mar 25, 2010
Showing with 54 additions and 1 deletion.
  1. +54 −1 MGTwitterTouchJSONParser.m
View
@@ -7,7 +7,7 @@
//
#import "MGTwitterTouchJSONParser.h"
-
+#import "CJSONDeserializer.h"
@implementation MGTwitterTouchJSONParser
@@ -44,6 +44,58 @@ - (id) initWithJSON:(NSData *)theJSON
URL = [theURL retain];
deliveryOptions = theDeliveryOptions;
delegate = theDelegate;
+
+ if (deliveryOptions & MGTwitterEngineDeliveryAllResultsOption)
+ {
+ parsedObjects = [[NSMutableArray alloc] initWithCapacity:0];
+ }
+ else
+ {
+ parsedObjects = nil; // rely on nil target to discard addObject
+ }
+
+ if ([json length] <= 5)
+ {
+ // NOTE: this is a hack for API methods that return short JSON responses that can't be parsed by YAJL. These include:
+ // friendships/exists: returns "true" or "false"
+ // help/test: returns "ok"
+ // An empty response of "[]" is a special case.
+ NSString *result = [[[NSString alloc] initWithBytes:[json bytes] length:[json length] encoding:NSUTF8StringEncoding] autorelease];
+ if (! [result isEqualToString:@"[]"])
+ {
+ NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
+
+ if ([result isEqualToString:@"\"ok\""])
+ {
+ [dictionary setObject:[NSNumber numberWithBool:YES] forKey:@"ok"];
+ }
+ else
+ {
+ [dictionary setObject:[NSNumber numberWithBool:[result isEqualToString:@"true"]] forKey:@"friends"];
+ }
+ [dictionary setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
+
+ [self _parsedObject:dictionary];
+
+ [parsedObjects addObject:dictionary];
+ }
+ }
+ else
+ {
+ id results = [[CJSONDeserializer deserializer] deserialize:json
+ error:nil];
+ if([results isKindOfClass:[NSArray class]]){
+ for(NSDictionary *result in results){
+ [self _parsedObject:result];
+ }
+ }else{
+ [self _parsedObject:results];
+ }
+
+ }
+
+ // notify the delegate that parsing completed
+ [self _parsingDidEnd];
}
return self;
}
@@ -80,6 +132,7 @@ - (void)_parsingErrorOccurred:(NSError *)parseError
- (void)_parsedObject:(NSDictionary *)dictionary
{
+ [parsedObjects addObject:dictionary];
if (deliveryOptions & MGTwitterEngineDeliveryIndividualResultsOption)
if ([self _isValidDelegateForSelector:@selector(parsedObject:forRequest:ofResponseType:)])
[delegate parsedObject:(NSDictionary *)dictionary forRequest:identifier ofResponseType:responseType];

0 comments on commit 3f33486

Please sign in to comment.