最近一个移动终端项目的服务器和客户端之间用到了json 来交换数据。
json是一种类似于xml的描述性语言,不过相对于xml来说,json这种格式比较简洁,可以降低服务器和客户端之间的流量。它的格式类似于:
{
"this": "is",
"really": "simple",
"json": "right?"
}
在json的网站一共有四种C parser,解析json格式我不知道哪种比较实用,花了点时间测试了一下兼容性(我比较关注,性能应该差不多)。现在把结果贴出来供大家参考。
测试的文件从yajl里的cases获取
yajl0.4.0
[PASS] /home/lyb/cases/array.json
[PASS] /home/lyb/cases/codepoints_from_unicode_org.json
[PASS] /home/lyb/cases/dc_simple_with_comments.json
[PASS] /home/lyb/cases/deep_arrays.json
[PASS] /home/lyb/cases/difficult_json_c_test_case.json
[PASS] /home/lyb/cases/difficult_json_c_test_case_with_comments.json
[PASS] /home/lyb/cases/doubles.json
[PASS] /home/lyb/cases/empty_array.json
[PASS] /home/lyb/cases/escaped_bulgarian.json
[PASS] /home/lyb/cases/escaped_foobar.json
[PASS] /home/lyb/cases/isolated_surrogate_marker.json
[PASS] /home/lyb/cases/nulls_and_bools.json
[PASS] /home/lyb/cases/simple.json
[PASS] /home/lyb/cases/simple_with_comments.json
[PASS] /home/lyb/cases/string_with_escapes.json
[PASS] /home/lyb/cases/unescaped_bulgarian.json
total: 54
pass: 16
fail: 38
jsonc0.7
[PASS] /home/lyb/cases/array.json
[PASS] /home/lyb/cases/codepoints_from_unicode_org.json
[PASS] /home/lyb/cases/difficult_json_c_test_case.json
[PASS] /home/lyb/cases/difficult_json_c_test_case_with_comments.json
[PASS] /home/lyb/cases/doubles.json
[PASS] /home/lyb/cases/empty_array.json
[PASS] /home/lyb/cases/escaped_bulgarian.json
[PASS] /home/lyb/cases/escaped_foobar.json
[PASS] /home/lyb/cases/integers.json
[PASS] /home/lyb/cases/invalid_utf8.json
[PASS] /home/lyb/cases/isolated_surrogate_marker.json
[PASS] /home/lyb/cases/leading_zero_in_number.json
[PASS] /home/lyb/cases/missing_integer_after_decimal_point.json
[PASS] /home/lyb/cases/missing_integer_after_exponent.json
[PASS] /home/lyb/cases/non_utf8_char_in_string.json
[PASS] /home/lyb/cases/nulls_and_bools.json
[PASS] /home/lyb/cases/simple.json
[PASS] /home/lyb/cases/string_with_invalid_newline.json
[PASS] /home/lyb/cases/unescaped_bulgarian.json
total: 54
pass: 19
fail: 35
mjson
[PASS] /home/lyb/cases/array.json
[PASS] /home/lyb/cases/deep_arrays.json
[PASS] /home/lyb/cases/difficult_json_c_test_case.json
[PASS] /home/lyb/cases/doubles.json
[PASS] /home/lyb/cases/empty_array.json
[PASS] /home/lyb/cases/escaped_bulgarian.json
[PASS] /home/lyb/cases/integers.json
[PASS] /home/lyb/cases/invalid_utf8.json
[PASS] /home/lyb/cases/non_utf8_char_in_string.json
[PASS] /home/lyb/cases/nulls_and_bools.json
[PASS] /home/lyb/cases/simple.json
[PASS] /home/lyb/cases/unescaped_bulgarian.json
total: 54
pass: 12
fail: 42
json_parser
[PASS] /home/lyb/cases/array.json
[PASS] /home/lyb/cases/difficult_json_c_test_case.json
[PASS] /home/lyb/cases/doubles.json
[PASS] /home/lyb/cases/empty_array.json
[PASS] /home/lyb/cases/escaped_bulgarian.json
[PASS] /home/lyb/cases/integers.json
[PASS] /home/lyb/cases/invalid_utf8.json
[PASS] /home/lyb/cases/non_utf8_char_in_string.json
[PASS] /home/lyb/cases/nulls_and_bools.json
[PASS] /home/lyb/cases/simple.json
[PASS] /home/lyb/cases/string_with_escapes.json
[PASS] /home/lyb/cases/unescaped_bulgarian.json
total: 54
pass: 12
fail: 42
结果分析:
从各个通过的文件数量看, jsonc兼容性是最好的,把结果都放到tmp.txt, 执行
[lyb@localhost jsonparser]$ cat tmp.txt | sort | uniq c | sort r
4 [PASS] /home/lyb/cases/unescaped_bulgarian.json
4 [PASS] /home/lyb/cases/simple.json
4 [PASS] /home/lyb/cases/nulls_and_bools.json
4 [PASS] /home/lyb/cases/escaped_bulgarian.json
4 [PASS] /home/lyb/cases/empty_array.json
4 [PASS] /home/lyb/cases/doubles.json
4 [PASS] /home/lyb/cases/difficult_json_c_test_case.json
4 [PASS] /home/lyb/cases/array.json
3 [PASS] /home/lyb/cases/non_utf8_char_in_string.json
3 [PASS] /home/lyb/cases/invalid_utf8.json
3 [PASS] /home/lyb/cases/integers.json
2 [PASS] /home/lyb/cases/string_with_escapes.json
2 [PASS] /home/lyb/cases/isolated_surrogate_marker.json
2 [PASS] /home/lyb/cases/escaped_foobar.json
2 [PASS] /home/lyb/cases/difficult_json_c_test_case_with_comments.json
2 [PASS] /home/lyb/cases/deep_arrays.json
2 [PASS] /home/lyb/cases/codepoints_from_unicode_org.json
1 [PASS] /home/lyb/cases/string_with_invalid_newline.json
1 [PASS] /home/lyb/cases/simple_with_comments.json
1 [PASS] /home/lyb/cases/missing_integer_after_exponent.json
1 [PASS] /home/lyb/cases/missing_integer_after_decimal_point.json
1 [PASS] /home/lyb/cases/leading_zero_in_number.json
1 [PASS] /home/lyb/cases/dc_simple_with_comments.json
有8个文件都通过了4个parser的测试。
4 [PASS] /home/lyb/cases/unescaped_bulgarian.json
4 [PASS] /home/lyb/cases/simple.json
4 [PASS] /home/lyb/cases/nulls_and_bools.json
4 [PASS] /home/lyb/cases/escaped_bulgarian.json
4 [PASS] /home/lyb/cases/empty_array.json
4 [PASS] /home/lyb/cases/doubles.json
4 [PASS] /home/lyb/cases/difficult_json_c_test_case.json
4 [PASS] /home/lyb/cases/array.json
jsonc0.7,mjson, jsonparser 都通过了下面3个文件的测试
3 [PASS] /home/lyb/cases/non_utf8_char_in_string.json
3 [PASS] /home/lyb/cases/invalid_utf8.json
3 [PASS] /home/lyb/cases/integers.json
可以看出yajl0.4.0的非utf8支持不如其它3个
四个模块的一些特点:
- mjson和jsonparser只有一两个文件,适合放到单个模块里用
- jsonc采用autoconf, 适合库形式
- yajl采用cmake, 测试做的比较好
- mjson和yajl都支持sax事件解析方法
~ END ~
cJSON是C语言中的一个JSON编解码器,非常轻量级,C文件只有500多行,速度也非常理想。项目主页:cJSON | http://sourceforge.net/projects/cjson/
cJSON也存在几个弱点:
- 不支持[1,2,3,]和{"one":1,}最后多余的那个逗号。这是C语言就开始支持的,JSON RFC文档中没有对此说明,只能说这是扩展功能吧。
- 不支持/注释/,和//单行注释。这也是扩展功能。C/C++/JAVA/JavaScript都支持注释,所以我也希望在json文件中写点注释。
- 使用了个全局变量指示出错位置。这个在多线程时就有问题啦。
- 没有封装文件操作,用户需要自己读写文件。
虽然功能不是非常强大(上面124都是非常容易添加少数几行代码都可以支持的),但cJSON的小身板和速度是最值得赞赏的。其代码被非常好地维护着,结构也简单易懂,可以作为一个非常好的C语言项目进行学习(支持上面12两个功能可以作为学习后的作业)。其解析核心是通过递归函数完成的,不过放心它的每个函数都非常非常节省资源。
jsonc是另外一个C语言项目,提供了所有的功能,甚至支持单引号字符串,但是结构较之cJSON更为复杂。解析字符串的核心函数没有使用递归实现,最多支持32层数组或对象嵌套(用到这么多层的JSON对象该有多复杂啊???),解析时使用了一堆状态在跳来跳去,加上层级跳转,阅读时容易发晕。
jsonc项目主页:jehiah/jsonc GitHub https://github.com/jehiah/jsonc
网友评论