JSON-framework 与 JSONKit的初步评估

来源:互联网 发布:sql数据库安全性 编辑:程序博客网 时间:2024/06/05 16:31

JSON-Framework 和 JSONKit 都是Objective C 平台上比较常用的 JSON 数据访问工具。我经手过的项目中,这两个组件都用过。在JSONKit的主页上可以看到,JSONKit 的性能远优于JSON-Framework(即SBJSON),而使用上JSONKit也非常简单,这也得到了包括我在内的大多数同行的欢迎。但是在使用功能上,到底这两个工具有什么差异呢?我昨天索性写了一个小程序对比了一下。

程序很简单,同一个json文件,用这两个工具做转换,包括自反和交叉实验,主要是考察它们在功能上的差异,还有就是是否有什么隐藏的缺陷、陷阱等。

#import <Foundation/Foundation.h>#import "JSONKit.h"#import "NSObject+SBJson.h"int main (int argc, const char * argv[]){    @autoreleasepool {        NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];        NSLog(@"data path: %@", path);        NSData *content = [NSData dataWithContentsOfFile:path];                NSDictionary *kitData = [content objectFromJSONData];        NSDictionary *sbData = [[[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding] JSONValue];                NSString *kitString = [kitData JSONString];        NSString *sbString = [sbData JSONRepresentation];        NSLog(@"==============");        if ([kitString isEqualToString:sbString]) {            NSLog(@"the string equal check by JSONKit and SBJSON is passed");        } else {            NSLog(@"the string equal check by JSONKit and SBJSON is't passed");            NSLog(@"string from JSONKit: \n%@", kitString);            NSLog(@"string from SBJSON: \n%@", sbString);        }        NSLog(@"==============");        NSString *kit2sbString = [kitData JSONRepresentation];        NSString *sb2kitString = [sbData JSONString];        if ([sb2kitString isEqualToString:kit2sbString]) {            NSLog(@"the string double check by JSONKit and SBJSON is passed");        } else {            NSLog(@"the string double check by JSONKit and SBJSON is't passed");            NSLog(@"string from SBJSON to JSONKit: \n%@", sb2kitString);            NSLog(@"string from JSONKit to SBJSON: \n%@", kit2sbString);        }        }    return 0;}

代码中使用的data.json是我从工作数据中dump出的一段,就不贴出来了,要测试的话,大家可以随便构造一个,这个没啥特殊的。经过比对,我的结论如下:


  • 两个框架对数据的处理应该都是正确的,但是对字典的解析逻辑不同,观察到转为字符串时,key的先后有时会不一致,这不影响转为本地数据后的使用
  • JSONKit arc 应用中需加入 -fno-objc-arc
  • SBJSON 的最新版以 arc 方式建立
  • 新版 jsonkit没有编译警告
  • JSONKit 可以生成可变或不可变的对象,sbjson只能生成不可变对象
  • sbjson 生成的是普通的 cocoa 容器,jsonkit生成的是隐藏了定义的jk系列容器,sbjson生成的容器更规范
  • sbjson 支持多种编码,jsonkit 必须使用utf8
  • jsonkit 的性能优于 sbjson
  • sbjson 已经一年多不活跃了

建议使用 JSONkit,功能足够使用,速度有压倒性优势,代码体积比sbjson小很多。




原创粉丝点击