Skip to content

Magical Data Modelling Framework for JSON. Create rapidly powerful, atomic and smart data model classes

Notifications You must be signed in to change notification settings

jirick1/JSONModel

 
 

Repository files navigation

JSONModel


Magical Data Modelling Framework for JSON

JSONModel for iOS and OSX

JSONModel is a library, which allows rapid creation of smart data models. You can use it in your iOS or OSX apps.

JSONModel automatically introspects your model classes and the structure of your JSON input and reduces drastically the amount of code you have to write.


Adding JSONModel to your project

Requirements

  • iOS 5.0+ (requires NSJSONSerialization)
  • OSX 10.7+ (requires NSJSONSerialization)
  • ARC only
  • SystemConfiguration.framework

Get it as source files

  1. Download the JSONModel repository as a zip file or clone it
  2. Copy the JSONModel sub-folder into your Xcode project
  3. Build your project and check that there are no compile time errors (if your project is non-arc for example, you will get a compile time error from the JSONModel library)

or get it via Cocoa pods

Be sure to update your pods specs:

pod setup

In your Podfile add the JSONModel pod:

pod 'JSONModel'

That's it! If you want to read more about CocoaPods, have a look at this great tutorial.

Source code documentation

The source code includes class docs, which you can build yourself and import into Xcode:

  1. If you don't already have appledoc installed, install it with homebrew by typing brew install appledoc.
  2. Install the documentation into Xcode by typing appledoc . in the root directory of the repository.
  3. Restart Xcode if it's already running.

Basic usage

Consider you have a JSON like this:

{id:"10", "country":"Germany", "dialCode": 49, "isInEurope":true}
  • Create a new Objective-C class for your data model and make it inherit the JSONModel class.
  • Declare properties in your header file with the name of the JSON keys:
#import "JSONModel.h"

@interface CountryModel : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope;

@end

There's no need to do anything in the .m file.

  • Initialize your model with data:
#import "CountryModel.h"
...

NSString* json = (fetch here JSON from Internet) ... 
NSError* err = nil;
CountryModel* country = [[CountryModel alloc] initWithString:json error:&err];

If the validation of the JSON passes you have all the corresponding properties in your model populated from the JSON. JSONModel will also try to convert as much data to the types you expect, in the example above it will:

  • convert "id" from string (in the JSON) to an int for your class
  • just copy country's value
  • convert dialCode from number (in the JSON) to an NSString value
  • finally convert isInEurope to a BOOL for your BOOL property

And the good news is all you had to do is define the properties and their expected types.


Online tutorials

Official website: http://www.jsonmodel.com

Class docs online: http://jsonmodel.com/docs/

Tutorial list:


Documentation

(This section will be rearranged soon to showcase code)

  • automatic data mapping
  • model cascading (models including models)
  • model collections
  • one-shot or on-demand JSON to model objects conversion
  • key mapping - map JSON keys from deeper levels or with mismatching names easily
  • JSON HTTP client - a thin HTTP client for simple async JSON requests
  • json validation
  • data transformations
  • error handling
  • custom data validation
  • automatic underscore_naming to camelCaseNaming mapping
  • synchronious and asynchronious networking
  • JSON API client
  • JSON RPC 1.0 client
  • automatic compare and equality features
  • export models back to NSDictionary or JSON text
  • and more.

License

This code is distributed under the terms and conditions of the MIT license.


Contribution guidelines

  • NB! if you are fixing a bug you discovered, please add also a unit test so I know how exactly to reproduce the bug before merging.

Misc

Author: Marin Todorov

Contributors: Christian Hoffmann, Mark Joslin, Julien Vignali, Symvaro GmbH, BB9z. Also everyone who did successful pull requests.

Change log : https://github.com/icanzilb/JSONModel/blob/master/Changelog.md

About

Magical Data Modelling Framework for JSON. Create rapidly powerful, atomic and smart data model classes

Resources

Stars

Watchers

Forks

Packages

No packages published