Coder Social home page Coder Social logo

jsonjoy's Introduction

JSONJoy

JSONJoy is a joyful little library for iOS and Mac OSX that makes converting and mapping JSON to your objects simple.

You can read more about it here: http://vluxe.io/json-parsing.html

Example

So here is an JSON blob we want to parse:

{
	"id" : 1
	"first_name": "John",
	"last_name": "Smith",
	"age": 25,
	"address": {
		"id": 1
		"street_address": "21 2nd Street",
	    "city": "New York",
	    "state": "NY",
	    "postal_code": 10021
	 }
	
}

And Here is our NSObjects we want to convert it to:

#import "Address.h"

@interface User : NSObject

@property(nonatomic,strong)NSNumber *objID;
@property(nonatomic,copy)NSString *firstName;
@property(nonatomic,copy)NSString *lastName;
@property(nonatomic,strong)NSNumber *age;
@property(nonatomic,strong)Address *address;

@end

@interface Address : NSObject

@property(nonatomic,strong)NSNumber *objID;
@property(nonatomic,copy)NSString *streetAddress;
@property(nonatomic,copy)NSString *city;
@property(nonatomic,copy)NSString *state;
@property(nonatomic,strong)NSNumber *postalCode;

@end

Take a bunch of error prone boilerplate code like this:

	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
	{
		NSDictionary* response = responseObject;
        User *john = [[User alloc] init];
        john.objID = response[@"id"];
        john.firstName = response[@"first_name"];
        john.lastName = response[@"last_name"];
        john.age = response[@"age"];
		//now for the address
		john.address = [[Address alloc] init];
        NSDictionary* address = response[@"address"];
        john.address.objID = address[@"id"];
        john.address.streetAddress = address[@"street_address"];
        john.address.city = address[@"city"];
        john.address.state = address[@"state"];
        john.address.postalCode = address[@"postal_code"];
		//finally!, now do work with object
	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
	    NSLog(@"Error: %@", error);
	}];

and Joyify it into this:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
{
	NSError *error = nil;
	JSONJoy *joy = [JSONJoy JSONJoyWithClass:[User class]];
    User *john = [joy process:responseObject error:&error];
	//do work with object
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

There is even a category on NSObject to make you be able to do a one liner like this:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
{
	User *john = [User objectWithJoy:responseObject];
	//do work with object
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Need to add some customize parsing? No Problem, just implement this method like the example below:

+(JSONJoy*)jsonMapper
{
    JSONJoy* mapper = [[JSONJoy alloc] initWithClass:[self class]];
    [mapper addArrayClassMap:@"photos" class:[Photo class]];
    return mapper;
}

All category methods call this method as well, so your objects get parsed properly.

Usage

JSONJoy works by mapping property names to JSON value names. It also supports standard rails snake case JSON names being convert to standard camel case objective-c property names. For example:

@property(nonatomic,copy)NSString *firstName;

supports JSON values in the format of:

"first_name": "John"
"firstName": "John"

Install

The recommended approach for installing JSONJoy is via the CocoaPods package manager, as it provides flexible dependency management and dead simple installation.

via CocoaPods

Install CocoaPods if not already available:

$ [sudo] gem install cocoapods
$ pod setup

Change to the directory of your Xcode project, and Create and Edit your Podfile and add JSONJoy:

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
platform :ios, '5.0' 
# Or platform :osx, '10.8'
pod 'JSONJoy'

Install into your project:

$ pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file)

Via git just add JSONJoy as a git submodule

Requirements

JSONJoy requires at least iOS 5/Mac OSX 10.8 or above.

License

JSONJoy is license under the Apache License.

Contact

Dalton Cherry

jsonjoy's People

Contributors

daltoniam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.