Skip to content
Browse files

Fallback on NSJSONSerialization only after checking for 3rd-party lib…

…raries
  • Loading branch information...
1 parent 206f6ff commit fb59b75493bf2e5553093225c37d8135d177e31a @mattt mattt committed Nov 8, 2011
Showing with 32 additions and 30 deletions.
  1. +32 −30 AFNetworking/AFJSONUtilities.h
View
62 AFNetworking/AFJSONUtilities.h
@@ -27,25 +27,14 @@
static inline NSData * AFJSONEncode(id object, NSError **error) {
NSData *data = nil;
- id _NSJSONSerializationClass = NSClassFromString(@"NSJSONSerialization");
- SEL _NSJSONSerializationSelector = NSSelectorFromString(@"dataWithJSONObject:options:error:");
SEL _JSONKitSelector = NSSelectorFromString(@"JSONDataWithOptions:error:");
SEL _SBJSONSelector = NSSelectorFromString(@"JSONRepresentation");
SEL _YAJLSelector = NSSelectorFromString(@"yajl_JSONString");
- if (_NSJSONSerializationClass && [_NSJSONSerializationClass respondsToSelector:_NSJSONSerializationSelector]) {
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[_NSJSONSerializationClass methodSignatureForSelector:_NSJSONSerializationSelector]];
- invocation.target = _NSJSONSerializationClass;
- invocation.selector = _NSJSONSerializationSelector;
-
- [invocation setArgument:&object atIndex:2]; // arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
- NSUInteger writeOptions = 0;
- [invocation setArgument:&writeOptions atIndex:3];
- [invocation setArgument:error atIndex:4];
-
- [invocation invoke];
- [invocation getReturnValue:&data];
- } else if (_JSONKitSelector && [data respondsToSelector:_JSONKitSelector]) {
+ id _NSJSONSerializationClass = NSClassFromString(@"NSJSONSerialization");
+ SEL _NSJSONSerializationSelector = NSSelectorFromString(@"dataWithJSONObject:options:error:");
+
+ if (_JSONKitSelector && [data respondsToSelector:_JSONKitSelector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[data methodSignatureForSelector:_JSONKitSelector]];
invocation.target = data;
invocation.selector = _JSONKitSelector;
@@ -81,6 +70,18 @@ static inline NSData * AFJSONEncode(id object, NSError **error) {
@catch (NSException *exception) {
*error = [[[NSError alloc] initWithDomain:NSStringFromClass([exception class]) code:0 userInfo:[exception userInfo]] autorelease];
}
+ } else if (_NSJSONSerializationClass && [_NSJSONSerializationClass respondsToSelector:_NSJSONSerializationSelector]) {
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[_NSJSONSerializationClass methodSignatureForSelector:_NSJSONSerializationSelector]];
+ invocation.target = _NSJSONSerializationClass;
+ invocation.selector = _NSJSONSerializationSelector;
+
+ [invocation setArgument:&object atIndex:2]; // arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
+ NSUInteger writeOptions = 0;
+ [invocation setArgument:&writeOptions atIndex:3];
+ [invocation setArgument:error atIndex:4];
+
+ [invocation invoke];
+ [invocation getReturnValue:&data];
}
return data;
@@ -89,25 +90,14 @@ static inline NSData * AFJSONEncode(id object, NSError **error) {
static inline id AFJSONDecode(NSData *data, NSError **error) {
id JSON = nil;
- id _NSJSONSerializationClass = NSClassFromString(@"NSJSONSerialization");
- SEL _NSJSONSerializationSelector = NSSelectorFromString(@"JSONObjectWithData:options:error:");
SEL _JSONKitSelector = NSSelectorFromString(@"objectFromJSONDataWithParseOptions:error:");
SEL _SBJSONSelector = NSSelectorFromString(@"JSONValue");
SEL _YAJLSelector = NSSelectorFromString(@"yajl_JSONWithOptions:error:");
- if (_NSJSONSerializationClass && [_NSJSONSerializationClass respondsToSelector:_NSJSONSerializationSelector]) {
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[_NSJSONSerializationClass methodSignatureForSelector:_NSJSONSerializationSelector]];
- invocation.target = _NSJSONSerializationClass;
- invocation.selector = _NSJSONSerializationSelector;
-
- [invocation setArgument:&data atIndex:2]; // arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
- NSUInteger readOptions = 0;
- [invocation setArgument:&readOptions atIndex:3];
- [invocation setArgument:error atIndex:4];
-
- [invocation invoke];
- [invocation getReturnValue:&JSON];
- } else if (_JSONKitSelector && [data respondsToSelector:_JSONKitSelector]) {
+ id _NSJSONSerializationClass = NSClassFromString(@"NSJSONSerialization");
+ SEL _NSJSONSerializationSelector = NSSelectorFromString(@"JSONObjectWithData:options:error:");
+
+ if (_JSONKitSelector && [data respondsToSelector:_JSONKitSelector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[data methodSignatureForSelector:_JSONKitSelector]];
invocation.target = data;
invocation.selector = _JSONKitSelector;
@@ -136,6 +126,18 @@ static inline id AFJSONDecode(NSData *data, NSError **error) {
[invocation invoke];
[invocation getReturnValue:&JSON];
+ } else if (_NSJSONSerializationClass && [_NSJSONSerializationClass respondsToSelector:_NSJSONSerializationSelector]) {
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[_NSJSONSerializationClass methodSignatureForSelector:_NSJSONSerializationSelector]];
+ invocation.target = _NSJSONSerializationClass;
+ invocation.selector = _NSJSONSerializationSelector;
+
+ [invocation setArgument:&data atIndex:2]; // arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
+ NSUInteger readOptions = 0;
+ [invocation setArgument:&readOptions atIndex:3];
+ [invocation setArgument:error atIndex:4];
+
+ [invocation invoke];
+ [invocation getReturnValue:&JSON];
}
return JSON;

0 comments on commit fb59b75

Please sign in to comment.
Something went wrong with that request. Please try again.