GithubHelp home page GithubHelp logo

mbjsonmodel's Introduction

#MBJSONModel

Quick and lightweight JSON → NSObject translation.

Example

@interface User : MBJSONModel
@property (nonatomic, copy) NSString *name;
@property (nonatomic) NSDate *birthDate;
@property (nonatomic) NSArray *tweets;
@end

@implementation User
+ (NSDictionary *)JSONKeyTranslationDictionary
{
    return @{
				@"user_name" : @"name",
				@"birth_date" : @{@"isDate" : @YES, @"format" : @"yyyy-MM-dd", @"property" : @"birthDate"},
				@"data.user.tweets" : @{@"class" : NSStringFromClass([Tweet class]), @"relationship" : @"tweets", @"isArray" : @YES},
              }
}
@end

Then, if you have a JSON dictionary that looks like this:

{
	"user_name" : "Jon Snow",
	"birth_date" : "1970/03/29",
	"data" : {
		"user" : {
			"tweets" : [
				{
					"text" : "hello world"
				}
			]
		}
	}
}

You can easily do:

User *userObj = [User modelFromJSONDictionary:jsonDict];

Advanced Options

####Custom value trasformers You can override a transformer method to manually transform a JSON object to native. Say you have the following JSON dictionary:

{
	"custom_text" : "hello\nworld"
}

and the following translation dictionary

+ (NSDictionary *)JSONKeyTranslationDictionary
{
    return @{@"custom_text" : @"text"}
}

and wanted to change the text to "hello world" before translating to native, you can implement

- (MBValueTransformer *)textJSONValueTransformer
{
	return [MBValueTransformer transformerWithBlock:^id(NSString *incomingString) {
       return [incomingString stringByReplacingOccurrencesOfString:@"/n" withString:@" "];
    }];
}

This method will automatically be called by MBJSONModel during the translation process.

####NSCopying MBJSONModels conform to NSCopying, which means you can make a copy of any model object:

User *user = [User modelFromJSONDictionary:@{@"user_name" : @"Jon Snow"}];
User *userCopy = [user copy];
assert([user.name isEqual:userCopy.name]); // Passes

####NSObject → NSDictionary

There are two dictionary instance methods:

/**
 Returns a dictionary where keys are named exactly as the @property is named
 */
- (NSDictionary *)dictionaryFromObjectProperties;
/**
 Returns a dictionary where keys are converted to JSONKeys using -JSONKeyForPropertyName:
 */
- (NSDictionary *)JSONDictionaryRepresentation;
User *user = [User modelFromJSONDictionary:@{@"user_name" : @"Jon Snow"}];
NSLog([user dictionaryFromObjectProperties]);

Output:

{"name" : "Jon Snow"}


```objective-c NSLog([user JSONDictionaryRepresentation]); ``` Output: > {"user_name" : "Jon Snow"}

Contact

Mo Bitar | @bitario

License

MBJSONModel is available under the MIT license.

mbjsonmodel's People

Contributors

moughxyz avatar

Watchers

wyrover 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.