BWObjectSerializer 0.0.1

BWObjectSerializer 0.0.1

测试已测试
Lang语言 Obj-CObjective C
许可证 Apache 2
发布上次发布2014年12月

未知 维护。



  • Bruno Wernimont

将对象转换成可以发送到 Web 服务的字典的小型库。

与字符串、数字和日期兼容良好。其他数据需要转换。默认情况下,日期使用 rails 日期时间格式解析。

一个对象

Item *item = [[Item alloc] init];
item.title = @"Title";
item.itemID = [NSNumber numberWithInt:15];
item.createdAt = [NSDate date];

序列化映射

[BWObjectSerializerMapping mappingForObject:[Item class] block:^(BWObjectSerializerMapping *serializer) {
    [serializer mapKeyPath:@"title" toAttribute:@"title"];
    [serializer mapKeyPath:@"itemID" toAttribute:@"id"];

    [serializer mapKeyPath:@"imagePath" toAttribute:@"image_url" valueBlock:^id(id value, id object) {
        return @"custom value";
    }];

    [serializer mapKeyPath:@"createdAt" toAttribute:@"created_at" withDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

    [[BWObjectSerializer shared] registerSerializer:serializer withRootKeyPath:@"item"];
}];

序列化对象

NSDictionary *dict = [[BWObjectSerializer shared] serializeObject:item];

或者

NSDictionary *dict [[BWObjectSerializer shared] serializeObject:item withMapping:mapping];

将生成以下字典

{
        item =     {
            "created_at" = "2012-07-20T21:56:26Z";
            "image_url" = "custom value";
            title = Title;
        };
}

ARC

BWStatusBarOverlay 仅支持 ARC。

联系方式

Bruno Wernimont