Skip to content

clundie/CDLJSONPResponseSerializer

Repository files navigation

CDLJSONPResponseSerializer

Build Status

This is a class to parse JSONP responses with AFNetworking. It uses Javascript contexts provided by the JavaScriptCore framework.

Usage

#import "AFNetworking.h"
#import "CDLJSONPResponseSerializer.h"
NSString     *urlString  = @"http://derp.example/";
NSString     *callback   = @"foo";
NSDictionary *parameters = @{@"callback": callback};
NSURLRequest *request    =
  [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET"
                                                URLString:urlString
                                               parameters:parameters];
AFHTTPRequestOperation *op =
  [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer =
  [CDLJSONPResponseSerializer serializerWithCallback:callback];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
                                    id responseObject) {
  NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];

Requirements

  • Xcode 5.0

  • Target of iOS 7.0 or OS X 10.9

  • AFNetworking 2.0

  • ARC enabled

  • JavaScriptCore framework

Installation with CocoaPods

Add this line to your Podfile:

pod "CDLJSONPResponseSerializer", "~> 0.9"

Running unit tests

In Xcode

In the Tests/ directory, run pod install to set up the workspace. Then you can open the workspace and run tests in the Tests project.

In a terminal

To run the tests from the command line, install xctool with Homebrew:

$ brew update
$ brew install xctool --HEAD

Then, run rake test.

Security Issues

Because JSONP responses are executed in Javascript contexts, a malicious response could cause very large memory or CPU usage. Your app could quit or lock up. This is also true of plain JSON, but JSONP can do it with only a small amount of data, for example:

while(1); // The response will use 100% CPU and never complete.

Credits

Created by Chris Lundie.

License

See the LICENSE file.

About

Parse JSONP responses with AFNetworking 2.0 and JavaScriptCore

Resources

License

Stars

Watchers

Forks

Packages

No packages published