forked from mattgemmell/MGTwitterEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MGTwitterTouchJSONParser.m
141 lines (122 loc) · 4.11 KB
/
MGTwitterTouchJSONParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// MGTwitterTouchJSONParser.m
// MGTwitterEngine
//
// Created by Steve Streza on 3/24/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MGTwitterTouchJSONParser.h"
#import "CJSONDeserializer.h"
@implementation MGTwitterTouchJSONParser
+ (id)parserWithJSON:(NSData *)theJSON
delegate:(NSObject *)theDelegate
connectionIdentifier:(NSString *)identifier
requestType:(MGTwitterRequestType)reqType
responseType:(MGTwitterResponseType)respType
URL:(NSURL *)URL
deliveryOptions:(MGTwitterEngineDeliveryOptions)deliveryOptions
{
return [[[self alloc] initWithJSON:theJSON
delegate:theDelegate
connectionIdentifier:identifier
requestType:reqType
responseType:respType
URL:URL
deliveryOptions:deliveryOptions] autorelease];
}
- (id) initWithJSON:(NSData *)theJSON
delegate:(NSObject *)theDelegate
connectionIdentifier:(NSString *)theIdentifier
requestType:(MGTwitterRequestType)reqType
responseType:(MGTwitterResponseType)respType
URL:(NSURL *)theURL
deliveryOptions:(MGTwitterEngineDeliveryOptions)theDeliveryOptions
{
if(self = [super init]){
json = [theJSON retain];
identifier = [theIdentifier retain];
requestType = reqType;
responseType = respType;
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;
}
- (void)dealloc
{
[parsedObjects release];
[json release];
[identifier release];
[URL release];
delegate = nil;
[super dealloc];
}
#pragma mark Delegate callbacks
- (BOOL) _isValidDelegateForSelector:(SEL)selector
{
return ((delegate != nil) && [delegate respondsToSelector:selector]);
}
- (void)_parsingDidEnd
{
if ([self _isValidDelegateForSelector:@selector(parsingSucceededForRequest:ofResponseType:withParsedObjects:)])
[delegate parsingSucceededForRequest:identifier ofResponseType:responseType withParsedObjects:parsedObjects];
}
- (void)_parsingErrorOccurred:(NSError *)parseError
{
if ([self _isValidDelegateForSelector:@selector(parsingFailedForRequest:ofResponseType:withError:)])
[delegate parsingFailedForRequest:identifier ofResponseType:responseType withError: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];
}
@end