iOS平台高效转换引擎json->model,model->json,model->Dictionary,支持模型类继承其他模型类,支持指定路径转换,不区分json的key和模型属性名称大小写,自动处理json中null
Time lost (Benchmark 1000 times)
/// jsonString 是一个比较复杂3000行的json文件,具体参考demo
ModelObject * model = [ModelObject whc_ModelWithJson:jsonString];
NSLog(@"model = %@\n\n\n",model);
NSString * modelString = [model whc_Json];
NSLog(@"modelString = %@\n\n\n",modelString);
NSDictionary * modelDict = [model whc_Dictionary];
NSLog(@"modelDict = %@\n\n\n",modelDict);
Head * head = [Head whc_ModelWithJson:jsonString keyPath:@"Head"];
NSLog(@"head = %@\n\n\n",head);
ResponseBody * body = [ResponseBody whc_ModelWithJson:jsonString keyPath:@"ResponseBody"];
NSLog(@"ResponseBody = %@\n\n\n",body);
PolicyRuleList * rule = [PolicyRuleList whc_ModelWithJson:jsonString keyPath:@"ResponseBody.PolicyRuleList[0]"];
NSLog(@"rule = %@\n\n\n",rule);
###七,归档对象
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:body];
NSLog(@"data = %@\n\n\n",data);
ResponseBody * body = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"body = %@\n\n\n",body);
ResponseBody * copyBody = body.copy;
NSLog(@"copyBody = %@",copyBody);
/// 模型类可自定义属性名称
+ (NSDictionary <NSString *, NSString *> *)whc_ModelReplacePropertyMapper {
return @{<json key名: 替换实际属性名>};
}
/// 模型数组/字典元素对象可自定义类
+ (NSDictionary <NSString *, Class> *)whc_ModelReplaceContainerElementClassMapper {
return @{替换实际属性名 : 实际类};
}
/// 模型类可自定义属性类型
+ (NSDictionary <NSString *, Class> *)whc_ModelReplacePropertyClassMapper {
return @{替换实际属性名 : 实际类};
}
#pragma mark - json转模型对象 Api -
/** 说明:把json解析为模型对象
*@param json :json数据对象
*@return 模型对象
*/
+ (id)whc_ModelWithJson:(id)json;
/** 说明:把json解析为模型对象
*@param json :json数据对象
*@param keyPath :json key的路径
*@return 模型对象
*/
+ (id)whc_ModelWithJson:(id)json keyPath:(NSString *)keyPath;
#pragma mark - 模型对象转json Api -
/** 说明:把模型对象转换为字典
*@return 字典对象
*/
- (NSDictionary *)whc_Dictionary;
/** 说明:把模型对象转换为json字符串
*@return json字符串
*/
- (NSString *)whc_Json;
#pragma mark - 模型对象序列化 Api -
/// 复制模型对象
- (id)whc_Copy;
/// 序列化模型对象
- (void)whc_Encode:(NSCoder *)aCoder;
/// 反序列化模型对象
- (void)whc_Decode:(NSCoder *)aDecoder;
All source code is licensed under the MIT License.