{"id":13602177,"url":"https://github.com/jsonmodel/jsonmodel","last_synced_at":"2025-12-16T15:41:56.261Z","repository":{"id":5756975,"uuid":"6969835","full_name":"jsonmodel/jsonmodel","owner":"jsonmodel","description":"Magical Data Modeling Framework for JSON - allows rapid creation of smart data models. You can use it in your iOS, macOS, watchOS and tvOS apps.","archived":false,"fork":false,"pushed_at":"2021-11-06T12:23:36.000Z","size":1637,"stargazers_count":6846,"open_issues_count":34,"forks_count":1045,"subscribers_count":258,"default_branch":"master","last_synced_at":"2025-05-06T09:46:01.867Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsonmodel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-02T17:19:21.000Z","updated_at":"2025-05-06T00:49:45.000Z","dependencies_parsed_at":"2022-07-14T00:50:35.717Z","dependency_job_id":null,"html_url":"https://github.com/jsonmodel/jsonmodel","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonmodel%2Fjsonmodel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonmodel%2Fjsonmodel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonmodel%2Fjsonmodel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonmodel%2Fjsonmodel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsonmodel","download_url":"https://codeload.github.com/jsonmodel/jsonmodel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252873955,"owners_count":21817713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:01:15.762Z","updated_at":"2025-12-16T15:41:56.178Z","avatar_url":"https://github.com/jsonmodel.png","language":"Objective-C","readme":"# JSONModel - Magical Data Modeling Framework for JSON\n\nJSONModel allows rapid creation of smart data models. You can use it in your\niOS, macOS, watchOS and tvOS apps. Automatic introspection of your model classes\nand JSON input drastically reduces the amount of code you have to write.\n\nSee [CHANGELOG.md](CHANGELOG.md) for details on changes.\n\n## Installation\n\n### CocoaPods\n\n```ruby\npod 'JSONModel'\n```\n\n### Carthage\n\n```ruby\ngithub \"jsonmodel/jsonmodel\"\n```\n\n### Manual\n\n0. download the JSONModel repository\n0. copy the JSONModel sub-folder into your Xcode project\n0. link your app to SystemConfiguration.framework\n\n## Basic Usage\n\nConsider you have JSON like this:\n\n```json\n{ \"id\": 10, \"country\": \"Germany\", \"dialCode\": 49, \"isInEurope\": true }\n```\n\n- create a JSONModel subclass for your data model\n- declare properties in your header file with the name of the JSON keys:\n\n```objc\n@interface CountryModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *country;\n@property (nonatomic) NSString *dialCode;\n@property (nonatomic) BOOL isInEurope;\n@end\n```\n\nThere's no need to do anything in the implementation (`.m`) file.\n\n- initialize your model with data:\n\n```objc\nNSError *error;\nCountryModel *country = [[CountryModel alloc] initWithString:myJson error:\u0026error];\n```\n\nIf the validation of the JSON passes. you have all the corresponding properties\nin your model populated from the JSON. JSONModel will also try to convert as\nmuch data to the types you expect. In the example above it will:\n\n- convert `id` from string (in the JSON) to an `int` for your class\n- copy the `country` value\n- convert `dialCode` from a number (in the JSON) to an `NSString` value\n- copy the `isInEurope` value\n\nAll you have to do is define the properties and their expected types.\n\n## Examples\n\n### Automatic name based mapping\n\n```json\n{\n\t\"id\": 123,\n\t\"name\": \"Product name\",\n\t\"price\": 12.95\n}\n```\n\n```objc\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *name;\n@property (nonatomic) float price;\n@end\n```\n\n### Model cascading (models including other models)\n\n```json\n{\n\t\"orderId\": 104,\n\t\"totalPrice\": 13.45,\n\t\"product\": {\n\t\t\"id\": 123,\n\t\t\"name\": \"Product name\",\n\t\t\"price\": 12.95\n\t}\n}\n```\n\n```objc\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *name;\n@property (nonatomic) float price;\n@end\n\n@interface OrderModel : JSONModel\n@property (nonatomic) NSInteger orderId;\n@property (nonatomic) float totalPrice;\n@property (nonatomic) ProductModel *product;\n@end\n```\n\n### Model collections\n\n```json\n{\n\t\"orderId\": 104,\n\t\"totalPrice\": 103.45,\n\t\"products\": [\n\t\t{\n\t\t\t\"id\": 123,\n\t\t\t\"name\": \"Product #1\",\n\t\t\t\"price\": 12.95\n\t\t},\n\t\t{\n\t\t\t\"id\": 137,\n\t\t\t\"name\": \"Product #2\",\n\t\t\t\"price\": 82.95\n\t\t}\n\t]\n}\n```\n\n```objc\n@protocol ProductModel;\n\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *name;\n@property (nonatomic) float price;\n@end\n\n@interface OrderModel : JSONModel\n@property (nonatomic) NSInteger orderId;\n@property (nonatomic) float totalPrice;\n@property (nonatomic) NSArray \u003cProductModel\u003e *products;\n@end\n```\n\nNote: the angle brackets after `NSArray` contain a protocol. This is not the\nsame as the Objective-C generics system. They are not mutually exclusive, but\nfor JSONModel to work, the protocol must be in place.\n\nAlso property can have generics info for compiler\n```objc\n@interface OrderModel : JSONModel\n@property (nonatomic) NSInteger orderId;\n@property (nonatomic) float totalPrice;\n@property (nonatomic) NSArray\u003cProductModel *\u003e \u003cProductModel\u003e *products;\n@end\n```\n\n### Nested key mapping\n\n```json\n{\n\t\"orderId\": 104,\n\t\"orderDetails\": {\n\t\t\"name\": \"Product #1\",\n\t\t\"price\": {\n\t\t\t\"usd\": 12.95\n\t\t}\n\t}\n}\n```\n\n```objc\n@interface OrderModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *productName;\n@property (nonatomic) float price;\n@end\n\n@implementation OrderModel\n\n+ (JSONKeyMapper *)keyMapper\n{\n\treturn [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{\n\t\t@\"id\": @\"orderId\",\n\t\t@\"productName\": @\"orderDetails.name\",\n\t\t@\"price\": @\"orderDetails.price.usd\"\n\t}];\n}\n\n@end\n```\n\n### Map automatically to snake_case\n\n```json\n{\n\t\"order_id\": 104,\n\t\"order_product\": \"Product #1\",\n\t\"order_price\": 12.95\n}\n```\n\n```objc\n@interface OrderModel : JSONModel\n@property (nonatomic) NSInteger orderId;\n@property (nonatomic) NSString *orderProduct;\n@property (nonatomic) float orderPrice;\n@end\n\n@implementation OrderModel\n\n+ (JSONKeyMapper *)keyMapper\n{\n\treturn [JSONKeyMapper mapperForSnakeCase];\n}\n\n@end\n```\n\n### Optional properties (i.e. can be missing or null)\n\n```json\n{\n\t\"id\": 123,\n\t\"name\": null,\n\t\"price\": 12.95\n}\n```\n\n```objc\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString \u003cOptional\u003e *name;\n@property (nonatomic) float price;\n@property (nonatomic) NSNumber \u003cOptional\u003e *uuid;\n@end\n```\n\n### Ignored properties (i.e. JSONModel completely ignores them)\n\n```json\n{\n\t\"id\": 123,\n\t\"name\": null\n}\n```\n\n```objc\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString \u003cIgnore\u003e *customProperty;\n@end\n```\n\n### Making scalar types optional\n\n```json\n{\n\t\"id\": null\n}\n```\n\n```objc\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@end\n\n@implementation ProductModel\n\n+ (BOOL)propertyIsOptional:(NSString *)propertyName\n{\n\tif ([propertyName isEqualToString:@\"id\"])\n\t\treturn YES;\n\n\treturn NO;\n}\n\n@end\n```\n\n### Export model to `NSDictionary` or JSON\n\n```objc\nProductModel *pm = [ProductModel new];\npm.name = @\"Some Name\";\n\n// convert to dictionary\nNSDictionary *dict = [pm toDictionary];\n\n// convert to json\nNSString *string = [pm toJSONString];\n```\n\n### Custom data transformers\n\n```objc\n@interface JSONValueTransformer (CustomTransformer)\n@end\n\n@implementation JSONValueTransformer (CustomTransformer)\n\n- (NSDate *)NSDateFromNSString:(NSString *)string\n{\n\tNSDateFormatter *formatter = [NSDateFormatter new];\n\tformatter.dateFormat = APIDateFormat;\n\treturn [formatter dateFromString:string];\n}\n\n- (NSString *)JSONObjectFromNSDate:(NSDate *)date\n{\n\tNSDateFormatter *formatter = [NSDateFormatter new];\n\tformatter.dateFormat = APIDateFormat;\n\treturn [formatter stringFromDate:date];\n}\n\n@end\n```\n\n### Custom getters/setters\n\n```objc\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *name;\n@property (nonatomic) float price;\n@property (nonatomic) NSLocale *locale;\n@end\n\n@implementation ProductModel\n\n- (void)setLocaleWithNSString:(NSString *)string\n{\n\tself.locale = [NSLocale localeWithLocaleIdentifier:string];\n}\n\n- (void)setLocaleWithNSDictionary:(NSDictionary *)dictionary\n{\n\tself.locale = [NSLocale localeWithLocaleIdentifier:dictionary[@\"identifier\"]];\n}\n\n- (NSString *)JSONObjectForLocale\n{\n\treturn self.locale.localeIdentifier;\n}\n\n@end\n```\n\n### Custom JSON validation\n\n```objc\n\n@interface ProductModel : JSONModel\n@property (nonatomic) NSInteger id;\n@property (nonatomic) NSString *name;\n@property (nonatomic) float price;\n@property (nonatomic) NSLocale *locale;\n@property (nonatomic) NSNumber \u003cIgnore\u003e *minNameLength;\n@end\n\n@implementation ProductModel\n\n- (BOOL)validate:(NSError **)error\n{\n\tif (![super validate:error])\n\t\treturn NO;\n\n\tif (self.name.length \u003c self.minNameLength.integerValue)\n\t{\n\t\t*error = [NSError errorWithDomain:@\"me.mycompany.com\" code:1 userInfo:nil];\n\t\treturn NO;\n\t}\n\n\treturn YES;\n}\n\n@end\n```\n\n## License\n\nMIT licensed - see [LICENSE](LICENSE) file.\n\n## Contributing\n\nWe love pull requests! See [CONTRIBUTING.md](CONTRIBUTING.md) for full details.\n","funding_links":[],"categories":["JSON","IOS 或 OSX","Objective-C","Data modeling","Objective-C  Stars 1000以内排名整理","Object-C 库","etc"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonmodel%2Fjsonmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsonmodel%2Fjsonmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonmodel%2Fjsonmodel/lists"}