JSONCore
https://github.com/maintoco/JSONCore
A fast, convenient and nonintrusive conversion between JSON and model.
转换速度快、使用简单方便的字典转模型框架
水平有限,欢迎各位大牛批评指教一直是拿来主义用的第三方库,总是发现一些不灵活或者不足、不适用的地方,所以自己心血来潮写一个轻量的JSON对象转换库.
目前为测试版,开题报告,可能还有很多问题,会持续更新优化.
不改变目标对象的属性类型
null值不做处理
过滤类型不匹配的情况,例如类型为NSArray,论文,但是JSON数据为字符串的情况
待完善
更新记录2016-12-24移除类继承的使用方式
优化初始化方法
修复JSON数据输出不能正确解析keyPath的问题,并增加是否格式化输出的开关
2016-12-08增加NSDate支持
修复类型不匹配的问题例如:属性定义为NSString但是JSON数据为Number
JavaScript Object Notation, or [JSON][], is a lightweight, text-based, serialization format for structured data that is used by many web-based services and API's. It is defined by [RFC 4627][].
JSON provides the following primitive types:
null
Boolean true and false
Number
String
Array
Object
These primitive types are mapped to the following Objective-C Foundation classes:
JSONObjective-Cnull [nil]
true and false [NSNumber] or [BOOL]
Number [NSNumber] or [NSDate]
String [NSString]
Array [NSArray]
Object [NSDictionary]
如何使用导入JSONCore文件夹到你的项目#import 'NSObject+JSONCore.h'Examples【示例】创建基础模型YourBaseModel并封装【推荐】为了减少代码入侵,方便以后灵活迁移,可按以下方法封装 示例:iOSExample->Weibo->Models->WeiboModel.h(包含MJExtension的实现方式作为对比)//示例基础模型 @interface YourBaseModel : NSObject /** 通过字典创建模型 @param data 许可类型<NSData,NSDictionary,NSString> @return 新创建模型对象 */ + (instancetype)jsonObjectFromData:(id)data; + (NSArray *)arrayOfModelsFromDictionaries:(NSArray *)array; /** 允许的属性名 */ + (NSArray *)allowedPropertyNames; /** key关联字段 @return key:对象属性 value:keyPath */ + (NSDictionary *)keyMappingDictionary; /** 类型关联字典 @return key:对象属性 value:类型class */ + (NSDictionary *)typeMappingDictionary; /** 忽略,不做处理的属性 */ + (NSSet *)ignoreSet; - (NSDictionary *)toDictionary; - (NSString *)toJSONString; @end #import 'YourBaseModel.h' #import 'NSObject+JSONCore.h' #import 'MJExtension.h' @implementation YourBaseModel + (void)load { //格式化输出JSON数据 [self setPrettyPrinted:YES]; } #pragma mark - 封装JSONCore + (instancetype)jsonObjectFromData:(id)data { return [self co_objectFromKeyValues:data]; } + (NSArray *)arrayOfModelsFromDictionaries:(NSArray *)array { return [self co_arrayOfModelsFromDictionaries:array]; } + (NSArray *)allowedPropertyNames { return nil; } + (NSDictionary *)keyMappingDictionary { return nil; } + (NSDictionary *)typeMappingDictionary { return nil; } + (NSSet *)ignoreSet { return nil; } - (NSDictionary *)toDictionary { return [self co_toDictionary]; } - (NSString *)toJSONString { return [self co_toJSONString]; } #pragma mark JSONCoreConfig + (NSDictionary *)co_allowedPropertyNames { return nil; } + (NSDictionary *)co_keyMappingDictionary { return [self keyMappingDictionary]; } + (NSDictionary *)co_typeMappingDictionary { return [self typeMappingDictionary]; } + (NSSet *)co_ignoreDictionary { return [self ignoreSet]; } @end JSONString -> Model【简单模型】{ 'id': 10, 'country': 'Germany', 'dialCode': 49, 'isInEurope': true }@interface CountryModel : YourBaseModel @property (nonatomic) NSInteger id; @property (nonatomic) NSString *country; @property (nonatomic) NSString *dialCode; @property (nonatomic) BOOL isInEurope; @endCountryModel *country = [CountryModel jsonObjectFromData:jsonString];JSONString -> Model【模型嵌套】{ 'orderId': 104, 'totalPrice': 103.45, 'products': [ { 'id': 123, 'name': 'Product #1', 'price': 12.95 }, { 'id': 137, 'name': 'Product #2', 'price': 82.95 } ], 'status':{ 'code':1, 'msg':'订单已付款' } }@interface ProductModel : YourBaseModel @property (nonatomic) NSInteger productId; @property (nonatomic) NSString *name; @property (nonatomic) float price; @end @interface OrderModel : YourBaseModel @property (nonatomic) NSInteger orderId; @property (nonatomic) float totalPrice; @property (nonatomic, assign) int status; @property (nonatomic) NSArray *products; @endModel name - JSON key mapping【属性和字典的key不同、多级映射】@implementation ProductModel + (NSDictionary *)keyMappingDictionary { return @{@'productID',@'id'}; } @end@implementation OrderModel + (NSDictionary *)keyMappingDictionary { return @{@'status':@'status.code'}; } @endModel contains model【模型中数组的元素也是模型】//模型中有个数组属性,数组元素映射为其他模型 + (NSDictionary *)typeMappingDictionary { return @{@'products':[ProductModel class]}; }解析速度对比
测试数据
100次循环解析平均耗时JSONCore 0.283s
MJExtension 0.998s
TODO
[已经完成] 解析速度对比分析
[ ] 复杂JSON解析测试
[ ] 全面覆盖测试,找出BUG