Permalink
Browse files

Merge branch 'experimental-json-agnosticism' into experimental-0.8

  • Loading branch information...
2 parents 8924702 + 283d9cf commit 1fbe7846dd732951802a6bb1fb94019dd150c06e @mattt mattt committed Nov 7, 2011
@@ -24,15 +24,14 @@
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
+#import "AFJSONUtilities.h"
#import <Availability.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED
#import <UIKit/UIKit.h>
#endif
-#import "JSONKit.h"
-
static NSString * const kAFMultipartFormLineDelimiter = @"\r\n"; // CRLF
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
@@ -112,23 +111,13 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding;
}
static NSString * AFJSONStringFromParameters(NSDictionary *parameters) {
- NSString *JSONString = nil;
-
-#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3 || __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_6
- if ([NSJSONSerialization class]) {
- NSError *error = nil;
- NSData *JSONData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];
- if (!error) {
- JSONString = [[[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding] autorelease];
- }
+ NSError *error = nil;
+ NSData *JSONData = AFJSONEncode(parameters, &error);
+ if (!error) {
+ return [[[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding] autorelease];
} else {
- JSONString = [parameters JSONString];
+ return nil;
}
-#else
- JSONString = [parameters JSONString];
-#endif
-
- return JSONString;
}
static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
@@ -21,10 +21,7 @@
// THE SOFTWARE.
#import "AFJSONRequestOperation.h"
-
-#include <Availability.h>
-
-#import "JSONKit.h"
+#import "AFJSONUtilities.h"
static dispatch_queue_t af_json_request_operation_processing_queue;
static dispatch_queue_t json_request_operation_processing_queue() {
@@ -101,16 +98,7 @@ - (id)responseJSON {
if ([self.responseData length] == 0) {
self.responseJSON = nil;
} else {
-
-#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3 || __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_6
- if ([NSJSONSerialization class]) {
- self.responseJSON = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error];
- } else {
- self.responseJSON = [[JSONDecoder decoder] objectWithData:self.responseData error:&error];
- }
-#else
- self.responseJSON = [[JSONDecoder decoder] objectWithData:self.responseData error:&error];
-#endif
+ self.responseJSON = AFJSONDecode(self.responseData, &error);
}
self.JSONError = error;
@@ -0,0 +1,92 @@
+// AFJSONUtilities.h
+//
+// Copyright (c) 2011 Gowalla (http://gowalla.com/)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import <Foundation/Foundation.h>
+
+#include <Availability.h>
+
+#if defined(_AF_USE_JSONKIT)
+#import "JSONKit.h"
+#elif defined(_AF_USE_SBJSON)
+#import "SBJSON.h"
+
+static SBJsonParser * _SBJSONParser() {
+ static SBJsonParser *_af_SBJSONParser = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ _af_SBJSONParser = [[SBJsonParser alloc] init];
+ });
+
+ return _af_SBJSONParser;
+}
+
+static SBJsonWriter * _SBJSONWriter() {
+ static SBJsonWriter *_af_SBJSONWriter = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ _af_SBJSONWriter = [[SBJsonWriter alloc] init];
+ });
+
+ return _af_SBJSONWriter;
+}
+#elif defined(_AF_USE_YAJL)
+ #if __IPHONE_OS_VERSION_MIN_REQUIRED
+ #import <YAJL/YAJL.h>
+ #elif __MAC_OS_X_VERSION_MIN_REQUIRED
+ #import <YAJLiOS/YAJL.h>
+ #endif
+#endif
+
+static inline NSData * AFJSONEncode(id object, NSError **error) {
+#if defined(_AF_USE_JSONKIT)
+ return [object JSONData];
+#elif defined(_AF_USE_SBJSON)
+ SBJsonWriter *writer = _SBJSONWriter();
+ return [writer dataWithObject:object];
+#elif defined(_AF_USE_YAJL)
+ return [[object yajl_JSONString] dataUsingEncoding:NSUTF8StringEncoding]];
+#else
+ if ([NSJSONSerialization class]) {
+ return [NSJSONSerialization dataWithJSONObject:object options:0 error:error];
+ }
+#endif
+
+ return nil;
+}
+
+static inline id AFJSONDecode(NSData *data, NSError **error) {
+
+#if defined(_AF_USE_JSONKIT)
+ return [[JSONDecoder decoder] objectWithData:data error:error];
+#elif defined(_AF_USE_SBJSON)
+ SBJsonParser *parser = _SBJsonParser();
+ return [parser objectWithData:data];
+#elif defined(_AF_USE_YAJL)
+ return [data yajl_JSON];
+#else
+ if ([NSJSONSerialization class]) {
+ return [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
+ }
+#endif
+
+ return nil;
+}
@@ -29,6 +29,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ F8323C901455B4FE00190CCB /* AFJSONUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFJSONUtilities.h; sourceTree = "<group>"; };
F87A159D1444926300318955 /* AFURLConnectionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFURLConnectionOperation.h; sourceTree = "<group>"; };
F87A159E1444926300318955 /* AFURLConnectionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFURLConnectionOperation.m; sourceTree = "<group>"; };
F87A15A01444926900318955 /* AFXMLRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFXMLRequestOperation.h; sourceTree = "<group>"; };
@@ -140,6 +141,7 @@
F897DE70142CFEDC000DDD35 /* AFImageRequestOperation.m */,
F897DE6D142CFEDC000DDD35 /* AFImageCache.h */,
F897DE6E142CFEDC000DDD35 /* AFImageCache.m */,
+ F8323C901455B4FE00190CCB /* AFJSONUtilities.h */,
);
name = AFNetworking;
path = ../../AFNetworking;
@@ -40,6 +40,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ F8323C941455C03400190CCB /* AFJSONUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFJSONUtilities.h; path = ../AFNetworking/AFJSONUtilities.h; sourceTree = "<group>"; };
F86E5527143A28F3002B438C /* AFURLConnectionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFURLConnectionOperation.h; path = ../AFNetworking/AFURLConnectionOperation.h; sourceTree = "<group>"; };
F86E5528143A28F3002B438C /* AFURLConnectionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFURLConnectionOperation.m; path = ../AFNetworking/AFURLConnectionOperation.m; sourceTree = "<group>"; };
F874B5C913E0AA6500B28E3E /* AFHTTPRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFHTTPRequestOperation.m; path = ../AFNetworking/AFHTTPRequestOperation.m; sourceTree = "<group>"; };
@@ -251,6 +252,7 @@
F874B5D013E0AA6500B28E3E /* UIImageView+AFNetworking.m */,
F874B5D513E0AA6500B28E3E /* AFNetworkActivityIndicatorManager.h */,
F874B5CD13E0AA6500B28E3E /* AFNetworkActivityIndicatorManager.m */,
+ F8323C941455C03400190CCB /* AFJSONUtilities.h */,
);
name = AFNetworking;
sourceTree = "<group>";

0 comments on commit 1fbe784

Please sign in to comment.