CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Mark Aufflick.
At The Bureau we love JSONModel's declaritive approach to object modelling, but we also love Plist file's native support for NSDate's and NSValue's etc. The main JSONModel repository lacks a few simple extensibility options required to specify that a particular data source supports more data types than JSON. It also, unrelatedly, lacks support for extending serialisation to other types.
JSONModel's author, Marin Todorov, quite reasonably wants to keep the root repository focussed on JSON and so we have begun maintaining this separate fork. This fork works identically to version 0.9.1 of JSONModel, with some extra features which are documentd in the headers. An updated Readme is in the works :)
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.
In your project's Podfile add the JSONModel pod:
pod 'JSONModel'
If you want to read more about CocoaPods, have a look at this short tutorial.
The source code includes class docs, which you can build yourself and import into Xcode:
brew install appledoc
.appledoc .
in the root directory of the repository.Consider you have a JSON like this:
{id:"10", "country":"Germany", "dialCode": 49, "isInEurope":true}
#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.
#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:
And the good news is all you had to do is define the properties and their expected types.
Official website: http://www.jsonmodel.com
Class docs online: http://jsonmodel.com/docs/
Step-by-step tutorials:
{ "id": "123", "name": "Product name", "price": 12.95 } |
@interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end |
{ "order_id": 104, "total_price": 13.45, "product" : { "id": "123", "name": "Product name", "price": 12.95 } } |
@interface OrderModel : JSONModel @property (assign, nonatomic) int order_id; @property (assign, nonatomic) float total_price; @property (strong, nonatomic) ProductModel* product; @end @implementation OrderModel @end |
{ "order_id": 104, "total_price": 103.45, "products" : [ { "id": "123", "name": "Product #1", "price": 12.95 }, { "id": "137", "name": "Product #2", "price": 82.95 } ] } |
@protocol ProductModel @end @interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end @interface OrderModel : JSONModel @property (assign, nonatomic) int order_id; @property (assign, nonatomic) float total_price; @property (strong, nonatomic) NSArray<ProductModel>* products; @end @implementation OrderModel @end |
{ "order_id": 104, "order_details" : [ { "name": "Product#1", "price": { "usd": 12.95 } } ] } |
@interface OrderModel : JSONModel @property (assign, nonatomic) int id; @property (assign, nonatomic) float price; @property (strong, nonatomic) NSString* productName; @end @implementation OrderModel +(JSONKeyMapper*)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ @"order_id": @"id", @"order_details.name": @"productName", @"order_details.price.usd": @"price" }]; } @end |
[JSONModel setGlobalKeyMapper:[ [JSONKeyMapper alloc] initWithDictionary:@{ @"item_id":@"ID", @"item.name": @"itemName" }] ]; |
{ "order_id": 104, "order_product" : @"Product#1", "order_price" : 12.95 } |
@interface OrderModel : JSONModel @property (assign, nonatomic) int orderId; @property (assign, nonatomic) float orderPrice; @property (strong, nonatomic) NSString* orderProduct; @end @implementation OrderModel +(JSONKeyMapper*)keyMapper { return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase]; } @end |
{ "id": "123", "name": null, "price": 12.95 } |
@interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString<Optional>* name; @property (assign, nonatomic) float price; @property (strong, nonatomic) NSNumber<Optional>* uuid; @end @implementation ProductModel @end |
{ "id": "123", "name": null } |
@interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString<Ignore>* customProperty; @end @implementation ProductModel @end |
@implementation ProductModel +(BOOL)propertyIsOptional:(NSString*)propertyName { return YES; } @end |
{ "order_id": 104, "total_price": 103.45, "products" : [ { "id": "123", "name": "Product #1", "price": 12.95 }, { "id": "137", "name": "Product #2", "price": 82.95 } ] } |
@protocol ProductModel @end @interface ProductModel : JSONModel @property (assign, nonatomic) int id; @property (strong, nonatomic) NSString* name; @property (assign, nonatomic) float price; @end @implementation ProductModel @end @interface OrderModel : JSONModel @property (assign, nonatomic) int order_id; @property (assign, nonatomic) float total_price; @property (strong, nonatomic) NSArray<ProductModel, ConvertOnDemand>* products; @end @implementation OrderModel @end |
//add extra headers
[[JSONHTTPClient requestHeaders] setValue:@"MySecret" forKey:@"AuthorizationToken"];
//make post, get requests
[JSONHTTPClient postJSONFromURLWithString:@"http://mydomain.com/api"
params:@{@"postParam1":@"value1"}
completion:^(id json, JSONModelError *err) {
//check err, process json ...
}];
ProductModel* pm = [[ProductModel alloc] initWithString:jsonString error:nil];
pm.name = @"Changed Name";
//convert to dictionary
NSDictionary* dict = [pm toDictionary];
//convert to text
NSString* string = [pm toJSONString];
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
This code is distributed under the terms and conditions of the MIT license.
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.