Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 142 lines (122 sloc) 4.204 kb
9463f27 @stevestreza Architectural support for TouchJSON
stevestreza authored
1 //
2 // MGTwitterTouchJSONParser.m
3 // MGTwitterEngine
4 //
5 // Created by Steve Streza on 3/24/10.
6 // Copyright 2010 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "MGTwitterTouchJSONParser.h"
3f33486 @stevestreza Added TouchJSON implementation
stevestreza authored
10 #import "CJSONDeserializer.h"
9463f27 @stevestreza Architectural support for TouchJSON
stevestreza authored
11
12 @implementation MGTwitterTouchJSONParser
13
14 + (id)parserWithJSON:(NSData *)theJSON
15 delegate:(NSObject *)theDelegate
16 connectionIdentifier:(NSString *)identifier
17 requestType:(MGTwitterRequestType)reqType
18 responseType:(MGTwitterResponseType)respType
19 URL:(NSURL *)URL
20 deliveryOptions:(MGTwitterEngineDeliveryOptions)deliveryOptions
21 {
22 return [[[self alloc] initWithJSON:theJSON
23 delegate:theDelegate
24 connectionIdentifier:identifier
25 requestType:reqType
26 responseType:respType
27 URL:URL
28 deliveryOptions:deliveryOptions] autorelease];
29 }
30
31 - (id) initWithJSON:(NSData *)theJSON
32 delegate:(NSObject *)theDelegate
33 connectionIdentifier:(NSString *)theIdentifier
34 requestType:(MGTwitterRequestType)reqType
35 responseType:(MGTwitterResponseType)respType
36 URL:(NSURL *)theURL
37 deliveryOptions:(MGTwitterEngineDeliveryOptions)theDeliveryOptions
38 {
39 if(self = [super init]){
40 json = [theJSON retain];
41 identifier = [theIdentifier retain];
42 requestType = reqType;
43 responseType = respType;
44 URL = [theURL retain];
45 deliveryOptions = theDeliveryOptions;
46 delegate = theDelegate;
3f33486 @stevestreza Added TouchJSON implementation
stevestreza authored
47
48 if (deliveryOptions & MGTwitterEngineDeliveryAllResultsOption)
49 {
50 parsedObjects = [[NSMutableArray alloc] initWithCapacity:0];
51 }
52 else
53 {
54 parsedObjects = nil; // rely on nil target to discard addObject
55 }
56
57 if ([json length] <= 5)
58 {
59 // NOTE: this is a hack for API methods that return short JSON responses that can't be parsed by YAJL. These include:
60 // friendships/exists: returns "true" or "false"
61 // help/test: returns "ok"
62 // An empty response of "[]" is a special case.
63 NSString *result = [[[NSString alloc] initWithBytes:[json bytes] length:[json length] encoding:NSUTF8StringEncoding] autorelease];
64 if (! [result isEqualToString:@"[]"])
65 {
66 NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
67
68 if ([result isEqualToString:@"\"ok\""])
69 {
70 [dictionary setObject:[NSNumber numberWithBool:YES] forKey:@"ok"];
71 }
72 else
73 {
74 [dictionary setObject:[NSNumber numberWithBool:[result isEqualToString:@"true"]] forKey:@"friends"];
75 }
76 [dictionary setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
77
78 [self _parsedObject:dictionary];
79
80 [parsedObjects addObject:dictionary];
81 }
82 }
83 else
84 {
85 id results = [[CJSONDeserializer deserializer] deserialize:json
86 error:nil];
87 if([results isKindOfClass:[NSArray class]]){
88 for(NSDictionary *result in results){
89 [self _parsedObject:result];
90 }
91 }else{
92 [self _parsedObject:results];
93 }
94
95 }
96
97 // notify the delegate that parsing completed
98 [self _parsingDidEnd];
9463f27 @stevestreza Architectural support for TouchJSON
stevestreza authored
99 }
100 return self;
101 }
102
103 - (void)dealloc
104 {
105 [parsedObjects release];
106 [json release];
107 [identifier release];
108 [URL release];
109
110 delegate = nil;
111 [super dealloc];
112 }
113
114 #pragma mark Delegate callbacks
115
116 - (BOOL) _isValidDelegateForSelector:(SEL)selector
117 {
118 return ((delegate != nil) && [delegate respondsToSelector:selector]);
119 }
120
121 - (void)_parsingDidEnd
122 {
123 if ([self _isValidDelegateForSelector:@selector(parsingSucceededForRequest:ofResponseType:withParsedObjects:)])
124 [delegate parsingSucceededForRequest:identifier ofResponseType:responseType withParsedObjects:parsedObjects];
125 }
126
127 - (void)_parsingErrorOccurred:(NSError *)parseError
128 {
129 if ([self _isValidDelegateForSelector:@selector(parsingFailedForRequest:ofResponseType:withError:)])
130 [delegate parsingFailedForRequest:identifier ofResponseType:responseType withError:parseError];
131 }
132
133 - (void)_parsedObject:(NSDictionary *)dictionary
134 {
3f33486 @stevestreza Added TouchJSON implementation
stevestreza authored
135 [parsedObjects addObject:dictionary];
9463f27 @stevestreza Architectural support for TouchJSON
stevestreza authored
136 if (deliveryOptions & MGTwitterEngineDeliveryIndividualResultsOption)
137 if ([self _isValidDelegateForSelector:@selector(parsedObject:forRequest:ofResponseType:)])
138 [delegate parsedObject:(NSDictionary *)dictionary forRequest:identifier ofResponseType:responseType];
139 }
140
141 @end
Something went wrong with that request. Please try again.