Python的超快JSON编码器和解码器
项目描述
这是ultrajson 1.35的兼容性分支,通过将C扩展重命名为“ujson1”,旨在与较新版本共存。
主线的ultrajson 3.x开发已经恢复,请将pull requests发送到https://github.com/ultrajson/ultrajson,除非它们绝对必要,否则不要将您从1.x版本中移除。
UltraJSON是用纯C编写的超快JSON编码器和解码器,具有适用于Python 2.5+和3的绑定。
为了获得更轻松的C/C++ JSON解码器日常体验,请查看基于UltraJSON的ujson4c。
只需像往常一样运行Pip即可安装它
$ pip install ujson
用法
可以作为Python中大多数其他JSON解析器的替代品使用
>>> import ujson >>> ujson.dumps([{"key": "value"}, 81, True]) '[{"key":"value"},81,true]' >>> ujson.loads("""[{"key": "value"}, 81, true]""") [{u'key': u'value'}, 81, True]
编码器选项
encode_html_chars
用于启用对“不安全”HTML字符的特殊编码,将其编码为更安全的Unicode序列。默认为false
>>> ujson.dumps("<script>John&Doe", encode_html_chars=True) '"\\u003cscript\\u003eJohn\\u0026Doe"'
ensure_ascii
将输出限制为ASCII,并转义所有大于127的扩展字符。默认为true。如果您的终端格式支持UTF-8,则建议将此选项设置为false以节省空间
>>> ujson.dumps(u"\xe5\xe4\xf6") '"\\u00e5\\u00e4\\u00f6"' >>> ujson.dumps(u"\xe5\xe4\xf6", ensure_ascii=False) '"\xc3\xa5\xc3\xa4\xc3\xb6"'
double_precision
控制对双精度或十进制值编码的小数位数。默认为9
>>> ujson.dumps(math.pi) '3.1415926536' >>> ujson.dumps(math.pi, double_precision=1) '3.1' >>> ujson.dumps(math.pi, double_precision=0) '3' >>> ujson.dumps(math.pi, double_precision=4) '3.1416'
escape_forward_slashes
控制是否转义斜杠(/)。默认为True
>>> ujson.dumps("http://esn.me") '"http:\/\/esn.me"' >>> ujson.dumps("http://esn.me", escape_forward_slashes=False) '"http://esn.me"'
缩进
控制是否启用缩进(“美化输出”)。默认为0(禁用)
>>> ujson.dumps({"foo": "bar"}) '{"foo":"bar"}' >>> ujson.dumps({"foo": "bar"}, indent=4) { "foo":"bar" }
解码器选项
precise_float
设置为在将字符串解码为双精度值时启用更高精度的(strtod)函数。默认为使用快速但精度较低的内置功能
>>> ujson.loads("4.56") 4.5600000000000005 >>> ujson.loads("4.56", precise_float=True) 4.5599999999999996
测试机器
Linux 3.13.0-66-generic x86_64 #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015
版本
CPython 2.7.6(默认,2015年6月22日,17:58:13)[GCC 4.8.2]
blist : 1.3.6
simplejson: 3.8.1
ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
yajl : 0.3.5
ujson |
yajl |
simplejson |
json |
|
---|---|---|---|---|
包含256个双精度的数组 |
||||
编码 |
3508.19 |
5742.00 |
3232.38 |
3309.09 |
解码 |
25103.37 |
11257.83 |
11696.26 |
11871.04 |
包含256个UTF-8字符串的数组 |
||||
编码 |
3189.71 |
2717.14 |
2006.38 |
2961.72 |
解码 |
1354.94 |
630.54 |
356.35 |
344.05 |
包含256个字符串的数组 |
||||
编码 |
18127.47 |
12537.39 |
12541.23 |
20001.00 |
解码 |
23264.70 |
12788.85 |
25427.88 |
9352.36 |
中等复杂对象 |
||||
编码 |
10519.38 |
5021.29 |
3686.86 |
4643.47 |
解码 |
9676.53 |
5326.79 |
8515.77 |
3017.30 |
包含256个True值的数组 |
||||
编码 |
105998.03 |
102067.28 |
44758.51 |
60424.80 |
解码 |
163869.96 |
78341.57 |
110859.36 |
115013.90 |
包含256个字符串,整数对(string, int)的字典 |
||||
编码 |
13471.32 |
12109.09 |
3876.40 |
8833.92 |
解码 |
16890.63 |
8946.07 |
12218.55 |
3350.72 |
包含256个包含256个字符串,整数对(string, int)的字典的数组 |
||||
编码 |
50.25 |
46.45 |
13.82 |
29.28 |
解码 |
33.27 |
22.10 |
27.91 |
10.43 |
包含256个包含256个字符串,整数对(string, int)的字典的数组,输出排序键 |
||||
编码 |
27.19 |
7.75 |
2.39 |
|
复杂对象 |
||||
编码 |
577.98 |
387.81 |
470.02 |
|
解码 |
496.73 |
234.44 |
151.00 |
145.16 |
版本
CPython 3.4.3(默认,2015年10月14日,20:28:29)[GCC 4.8.4]
blist : 1.3.6
simplejson: 3.8.1
ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
yajl : 0.3.5
ujson |
yajl |
simplejson |
json |
|
---|---|---|---|---|
包含256个双精度的数组 |
||||
编码 |
3477.15 |
5732.24 |
3016.76 |
3071.99 |
解码 |
23625.20 |
9731.45 |
9501.57 |
9901.92 |
包含256个UTF-8字符串的数组 |
||||
编码 |
1995.89 |
2151.61 |
1771.98 |
1817.20 |
解码 |
1425.04 |
625.38 |
327.14 |
305.95 |
包含256个字符串的数组 |
||||
编码 |
25461.75 |
12188.64 |
13054.76 |
14429.81 |
解码 |
21981.31 |
17014.22 |
23869.48 |
22483.58 |
中等复杂对象 |
||||
编码 |
10821.46 |
4837.04 |
3114.04 |
4254.46 |
解码 |
7887.77 |
5126.67 |
4934.60 |
6204.97 |
包含256个True值的数组 |
||||
编码 |
100452.86 |
94639.42 |
46657.63 |
60358.63 |
解码 |
148312.69 |
75485.90 |
88434.91 |
116395.51 |
包含256个字符串,整数对(string, int)的字典 |
||||
编码 |
11698.13 |
8886.96 |
3043.69 |
6302.35 |
解码 |
10686.40 |
7061.77 |
5646.80 |
7702.29 |
包含256个包含256个字符串,整数对(string, int)的字典的数组 |
||||
编码 |
44.26 |
34.43 |
10.40 |
21.97 |
解码 |
28.46 |
23.95 |
18.70 |
22.83 |
包含256个包含256个字符串,整数对(string, int)的字典的数组,输出排序键 |
||||
编码 |
33.60 |
6.94 |
22.34 |
|
复杂对象 |
||||
编码 |
432.30 |
351.47 |
379.34 |
|
解码 |
434.40 |
221.97 |
149.57 |
147.79 |
项目详情
ujson1-1.36.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 00792db47b0bad1edd680c72f3f5c101db0b861d64b506ce9629aa0758246e01 |
|
MD5 | d95f913c2ece239cbda0c56454ee02e5 |
|
BLAKE2b-256 | fc214f49e23bb0a7d38f077dbdced7012a1ea0e66e5929abac7139f397587ace |