Git Product home page Git Product logo

whc_model's Introduction

WHC_Model

Build Status Pod Version Pod Platform Pod License 简介

  • 高效: 深度递归高性能解析架构,性能超过目前主流JsonModel,MJExtension...
  • 继承: 支持model类继承其他model类
  • 安全: 自动处理json中的null
  • 优势: 高容错能力(model类属性名称和json里key名称不区分大小写)
  • 强大: 支持自定义模型类属性名称以及类型别名设置
  • 特性: 支持反射指定json路径key来解析指定的节点json对象
  • 嵌套: 支持json或者model类的无限嵌套,json->model ,model->json的转换
  • 附加: 支持模型对象归档解档以及copy操作

性能测试

Time lost (Benchmark 1000 times)

  • 查看性能测试请运行项目: Benchmark/ModelBenchmark.xcodeproj

要求

  • iOS 6.0 or later
  • Xcode 8.0 or later

集成

  • 使用CocoaPods:
    • pod 'WHC_Model'
  • 手工集成:
    • 导入文件夹WHC_ModelKit

用法

json -> model

/// jsonString 是一个比较复杂3000行的json文件,具体参考demo
    ModelObject * model = [ModelObject whc_ModelWithJson:jsonString];
    NSLog(@"model = %@\n\n\n",model);

model -> json

    NSString * modelString = [model whc_Json];
    NSLog(@"modelString = %@\n\n\n",modelString);

model - > NSDictionary

    NSDictionary * modelDict = [model whc_Dictionary];
    NSLog(@"modelDict = %@\n\n\n",modelDict);

四,指定路径只解析Head对象

    Head * head = [Head whc_ModelWithJson:jsonString keyPath:@"Head"];
    NSLog(@"head = %@\n\n\n",head);

五,指定路径只解析ResponseBody对象

    ResponseBody * body = [ResponseBody whc_ModelWithJson:jsonString keyPath:@"ResponseBody"];
    NSLog(@"ResponseBody = %@\n\n\n",body);

六,指定路径只解析PolicyRuleList集合中第一个对象

    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;

期待

  • 如果您在使用过程中有任何问题,欢迎issue me! 很乐意为您解答任何相关问题!
  • 与其给我点star,不如向我狠狠地抛来一个BUG!
  • 如果您想要更多的接口来自定义或者建议/意见,欢迎issue me!我会根据大家的需求提供更多的接口!

Licenses

All source code is licensed under the MIT License.

whc_model's People

Contributors

netyouli avatar thilagavathi1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

whc_model's Issues

bug

homeCorner = ""; homeCorner 属性为 NSInteger 就蹦了

Bug

当字典的值某一个为空时,就会崩溃

关于json返回的数组的属性名是copy该如何使用

/// 模型类可自定义属性名称<json key名, 替换实际属性名>
(NSDictionary <NSString *, NSString *> *)whc_ModelReplacePropertyMapper;
/// 模型数组/字典元素对象可自定义类<替换实际属性名,实际类>
(NSDictionary <NSString *, Class> *)whc_ModelReplaceContainerElementClassMapper;
/// 模型类可自定义属性类型<替换实际属性名,实际类>
(NSDictionary <NSString *, Class> *)whc_ModelReplacePropertyClassMapper;
关于这3个方法在demo中发现没有应用
第一个 可以看懂什么意思
但是 第二个 和第三个 不是很理解

比如 json数据 返回 格式如下

{ "outqty1" : 2.5,
"line" : 13051,
"copy" : [
{
"pmodel" : "108.0G",
"copy" : "拷贝字符串",
"pprate" : 40,
},
{
"pmodel" : "101.0G",
"copy" : "拷贝字符串1",
"pprate" : 41,
}
]
}

不知道该如何定义这个 数组的copy类型

bug

2
3
1

一些比较旧的手机如 iPhone4s iPhone4c iPhone5会出现这种崩溃

bug

当服务器返回的json的属性值为null时,但是对象的属性为NSInteger时,解释出错,程序crash;
例如:服务返回:obj:null; 而oc的对象的属性@Property(nonatomic, assign) NSInteger obj;

错误提示的大意为:无法将NSNull转换到NSInteger;

同属性名不同类型解析问题

使用过程中出现一个问题,模型里的某个属性不同情况下会有不同的类型,比如我使用过程中有一个属性expressInfo是存储快递信息,自定义了类ExpressInfo,

//数组或字典替换存储类

  • (NSDictionary <NSString *, Class> *)whc_ModelReplaceContainerElementClassMapper {
    return @{@"expressInfo" : NSClassFromString(@"ExpressInfo")};
    }

但是有时候订单是没有快递信息的,后台的这个key会传回flase,是bool类型,而解析过程中,如果第一次解析没有快递信息后,其他的数据即使是有快递信息,解析的结果也会是nil。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.