-
Notifications
You must be signed in to change notification settings - Fork 2
/
JSONService.m
97 lines (89 loc) · 3.56 KB
/
JSONService.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
#import "JSONService.h"
#define JSON_MIME @"application/json"
@implementation JSONService
+ (JSONService*)service{
return [[[JSONService alloc] init] autorelease];
}
+(BOOL)iOS5JSONSerialization {
static BOOL iOS5JSONSerialization = NO;
static dispatch_once_t once;
dispatch_once(&once, ^{
iOS5JSONSerialization = !!NSClassFromString(@"NSJSONSerialization");
});
return iOS5JSONSerialization;
}
-(void)requestWithURL:(NSURL*)url
method:(NSString*)method
headers:(NSDictionary*)headers
bodyStream:(NSInputStream*)bodyStream
cachePolicy:(NSURLRequestCachePolicy)cachePolicy
timeoutInterval:(NSTimeInterval)timeoutInterval
receiveHandler:(void (^)(id, NSNumber*, NSDictionary*))receiveHandler
errorHandler:(void (^)(NSError*))errorHandler {
void (^jsonReceiveHander)(id, NSNumber*, NSDictionary*) = ^(id data, NSNumber* statusCode, NSDictionary *headers) {
NSError *error = nil;
id object = nil;
if ([[self class] iOS5JSONSerialization]) {
object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
} else {
object = [[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease] JSONValue];
}
if (object) {
receiveHandler(object, statusCode, headers);
} else {
errorHandler(error);
}
};
NSMutableDictionary *newHeaders = [NSMutableDictionary dictionaryWithDictionary:headers];
[newHeaders setObject:JSON_MIME forKey:@"Accept"];
[super requestWithURL:url
method:method
headers:newHeaders
bodyStream:bodyStream
cachePolicy:cachePolicy
timeoutInterval:timeoutInterval
receiveHandler:jsonReceiveHander
errorHandler:errorHandler];
}
-(void)requestWithURL:(NSURL*)url
method:(NSString*)method
headers:(NSDictionary*)headers
jsonString:(NSString*)body
receiveHandler:(void (^)(id, NSNumber*, NSDictionary*))receiveHandler
errorHandler:(void (^)(NSError*))errorHandler {
NSMutableDictionary *newHeaders = [NSMutableDictionary dictionaryWithDictionary:headers];
[newHeaders setObject:JSON_MIME forKey:@"Content-Type"];
[self requestWithURL:url
method:method
headers:newHeaders
body:body ? [body dataUsingEncoding:NSUTF8StringEncoding] : nil
receiveHandler:receiveHandler
errorHandler:errorHandler];
}
-(void)requestWithURL:(NSURL*)url
method:(NSString*)method
headers:(NSDictionary*)headers
jsonData:(id)body
receiveHandler:(void (^)(id, NSNumber*, NSDictionary*))receiveHandler
errorHandler:(void (^)(NSError*))errorHandler {
NSError *error = nil;
NSData *bodyData = nil;
if ([[self class] iOS5JSONSerialization] && body) {
bodyData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
} else {
bodyData = [[body JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding];
}
NSMutableDictionary *newHeaders = [NSMutableDictionary dictionaryWithDictionary:headers];
[newHeaders setObject:JSON_MIME forKey:@"Content-Type"];
if (error) {
errorHandler(error);
} else {
[self requestWithURL:url
method:method
headers:newHeaders
body:bodyData
receiveHandler:receiveHandler
errorHandler:errorHandler];
}
}
@end