JSONModel - JSON的数据建模框架
JSONModel 允许快速创建智能数据模型。您可以在您iOS、macOS、watchOS 和 tvOS 应用中使用它。您的模型类的自动自省和 JSON 输入大大减少了您需要编写的代码量。
请参阅 CHANGELOG.md 了解更改的详细信息。
安装
CocoaPods
pod 'JSONModel'
Carthage
github "jsonmodel/jsonmodel"
手册
- 下载JSONModel仓库
- 将JSONModel子文件夹复制到您的Xcode项目中
- 将您的应用程序链接到SystemConfiguration.framework
基本用法
假设你有这样的JSON
{ "id": 10, "country": "Germany", "dialCode": 49, "isInEurope": true }
- 为您的数据模型创建一个JSONModel子类
- 在头文件中声明属性,使用JSON键的名称
@interface CountryModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *country;
@property (nonatomic) NSString *dialCode;
@property (nonatomic) BOOL isInEurope;
@end
在实现文件(.m)中无需做任何事情。
- 用数据初始化您的模型
NSError *error;
CountryModel *country = [[CountryModel alloc] initWithString:myJson error:&error];
如果JSON的验证通过,您的模型中的所有对应属性都将会从JSON中填充。JSONModel还会尝试尽可能多地将数据转换为您期望的类型。在上面的例子中,它会
- 将
id
从JSON中的字符串转换为您的类中的int
- 复制
country
的值 - 将
dialCode
从JSON中的数字转换为NSString
值 - 复制
isInEurope
的值
您需要做的只是定义属性及其期望的类型。
示例
基于名称的自动映射
{
"id": 123,
"name": "Product name",
"price": 12.95
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@end
模型级联(包含其他模型的模型)
{
"orderId": 104,
"totalPrice": 13.45,
"product": {
"id": 123,
"name": "Product name",
"price": 12.95
}
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@end
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) float totalPrice;
@property (nonatomic) ProductModel *product;
@end
模型集合
{
"orderId": 104,
"totalPrice": 103.45,
"products": [
{
"id": 123,
"name": "Product #1",
"price": 12.95
},
{
"id": 137,
"name": "Product #2",
"price": 82.95
}
]
}
@protocol ProductModel;
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@end
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) float totalPrice;
@property (nonatomic) NSArray <ProductModel> *products;
@end
注意:在 NSArray
之后的尖括号包含了一个协议。这不同于 Objective-C 的泛型系统。它们不是互相排斥的,但为了 JSONModel 能够正常工作,协议必须存在。
此外,属性可以为编译器提供泛型信息。
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) float totalPrice;
@property (nonatomic) NSArray<ProductModel *> <ProductModel> *products;
@end
嵌套键映射
{
"orderId": 104,
"orderDetails": {
"name": "Product #1",
"price": {
"usd": 12.95
}
}
}
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *productName;
@property (nonatomic) float price;
@end
@implementation OrderModel
+ (JSONKeyMapper *)keyMapper
{
return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
@"id": @"orderId",
@"productName": @"orderDetails.name",
@"price": @"orderDetails.price.usd"
}];
}
@end
自动映射到 snake_case
{
"order_id": 104,
"order_product": "Product #1",
"order_price": 12.95
}
@interface OrderModel : JSONModel
@property (nonatomic) NSInteger orderId;
@property (nonatomic) NSString *orderProduct;
@property (nonatomic) float orderPrice;
@end
@implementation OrderModel
+ (JSONKeyMapper *)keyMapper
{
return [JSONKeyMapper mapperForSnakeCase];
}
@end
可选属性(即可以是缺失或为 null 的属性)
{
"id": 123,
"name": null,
"price": 12.95
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString <Optional> *name;
@property (nonatomic) float price;
@property (nonatomic) NSNumber <Optional> *uuid;
@end
忽略的属性(即 JSONModel 将完全忽略它们)
{
"id": 123,
"name": null
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString <Ignore> *customProperty;
@end
使标量类型可选
{
"id": null
}
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@end
@implementation ProductModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName
{
if ([propertyName isEqualToString:@"id"])
return YES;
return NO;
}
@end
NSDictionary
或 JSON
将模型导出为 ProductModel *pm = [ProductModel new];
pm.name = @"Some Name";
// convert to dictionary
NSDictionary *dict = [pm toDictionary];
// convert to json
NSString *string = [pm toJSONString];
自定义数据转换器
@interface JSONValueTransformer (CustomTransformer)
@end
@implementation JSONValueTransformer (CustomTransformer)
- (NSDate *)NSDateFromNSString:(NSString *)string
{
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = APIDateFormat;
return [formatter dateFromString:string];
}
- (NSString *)JSONObjectFromNSDate:(NSDate *)date
{
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = APIDateFormat;
return [formatter stringFromDate:date];
}
@end
自定义获取器/设置器
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@property (nonatomic) NSLocale *locale;
@end
@implementation ProductModel
- (void)setLocaleWithNSString:(NSString *)string
{
self.locale = [NSLocale localeWithLocaleIdentifier:string];
}
- (void)setLocaleWithNSDictionary:(NSDictionary *)dictionary
{
self.locale = [NSLocale localeWithLocaleIdentifier:dictionary[@"identifier"]];
}
- (NSString *)JSONObjectForLocale
{
return self.locale.localeIdentifier;
}
@end
自定义JSON验证
@interface ProductModel : JSONModel
@property (nonatomic) NSInteger id;
@property (nonatomic) NSString *name;
@property (nonatomic) float price;
@property (nonatomic) NSLocale *locale;
@property (nonatomic) NSNumber <Ignore> *minNameLength;
@end
@implementation ProductModel
- (BOOL)validate:(NSError **)error
{
if (![super validate:error])
return NO;
if (self.name.length < self.minNameLength.integerValue)
{
*error = [NSError errorWithDomain:@"me.mycompany.com" code:1 userInfo:nil];
return NO;
}
return YES;
}
@end
授权
MIT授权 - 查看LICENSE文件。
贡献
我们喜爱pull requests!参阅CONTRIBUTING.md获取全部详情。