JsonImpl 1.0.4

JsonImpl 1.0.4

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年4月

mkoosun 维护。



JsonImpl 1.0.4

  • 作者:
  • mkoo.sun

方便且简单的 JSON 数据建模框架 - 非常适用于网络协议,支持继承和嵌套。

安装

NSObject+JsonImpl.hNSObject+JsonImpl.m 复制到您的 projext。

JsonImpl 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中

pod "JsonImpl"

示例

要运行示例项目,请克隆仓库,并首先从 Example 目录运行 pod install

将 JSON 转换为模型

@interface Human : NSObject
@property (nonatomic, assign) NSInteger age;
@end

...

NSDictionary *dict = @{@"age":@30};
Human *man = [Human new];
[man parse:dict];

NSString *jsonStr = @"{\n  \"age\" : 30\n}";
Man *man2 = [Man new];
[man2 parse: jsonStr];

将模型转换为 JSON

Human *man = [Human new];
NSString *str = [man toJsonString];
NSDictionary *dict = [man toJsonDictionary];

继承

@interface Man : Human
@property (nonatomic, strong) NSString *job;
@end

属性嵌套

@interface Man : Human
@property (nonatomic, strong) Human* wife;
@end

数组嵌套

@interface Man : Human
@property (nonatomic, strong) NSArray* childrens;
@end

@implementation Man

+ (void)initialize
{
    if (self == [Man class]) {
        [self setArrayProperty:@"childrens" withClass:@"Human"];
    }
}

忽略属性

  1. 如果属性名以 '_ignore' 结尾,则该属性将被忽略

  2. 如果您在 initialize 中调用了 setIgnoreProperty,则属性将被忽略

  3. 如果属性包含 <Ignore>,它也将被忽略

@interface Man : Human
@property (nonatomic, strong) NSString<Ignore>* stringProperty;
@property (nonatomic, strong) NSString* stringProperty_ignore;
@property (nonatomic, assign) NSInteger intProperty;
@end

@implementation Man

+ (void)initialize
{
    if (self == [Man class]) {
        [self setIgnoreProperty:@"intProperty"];
    }
}

@end

作者

wanglin.sun, [email protected]

许可证

JsonImpl 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。