四种json c parser的兼容性比较
最近一个移动终端项目的服务器和客户端之间用到了json(
http://www.json.org) 来交换数据。json是一种类似于xml的描述性语言,不过相对于xml来说,json这种格式比较简洁,可以降低服务器和客户端之间的流量。它的格式类似于:
代码:
全选
-
{
"this": "is",
"really": "simple",
"json": "right?"
}
在json的网站一共有四种C parser,解析json格式我不知道哪种比较实用,花了点时间测试了一下兼容性(我比较关注,性能应该差不多)。现在把结果贴出来供大家参考。
测试的文件从yajl里的cases获取
yajl-0.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
json-c-0.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
结果分析:
从各个通过的文件数量看, json-c兼容性是最好的
把结果都放到tmp.txt, 执行
代码:
全选
-
[lyb@localhost json-parser]$ 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
json-c-0.7,mjson, json-parser 都通过了下面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
可以看出yajl-0.4.0的非utf-8支持不如其它3个
四个模块的一些特点:
mjson和json-parser只有一两个文件,适合放到单个模块里用
json-c采用autoconf, 适合库形式
yajl采用cmake, 测试做的比较好
mjson和yajl都支持sax事件解析方法
~ END ~