我见过许多项目使用simplejson模块而不是标准库中的json模块。此外,还有许多不同的simplejson模块。为什么要使用这些替代品,而不是标准库中的替代品?
当前回答
Json是simplejson,添加到stdlib中。但由于json是在2.6中添加的,因此simplejson具有在更多Python版本(2.4+)上工作的优势。
simplejson的更新频率也高于Python,因此如果您需要(或想要)最新版本,最好尽可能使用simplejson本身。
在我看来,一个好的做法是使用其中一种作为后备。
try:
import simplejson as json
except ImportError:
import json
其他回答
所有这些答案都不是很有用,因为它们对时间很敏感。
在我自己做了一些研究之后,我发现simplejson确实比内置更快,如果你把它更新到最新版本的话。
pip/easy_install想在ubuntu 12.04上安装2.3.2,但在发现最新的simplejson版本实际上是3.3.0之后,所以我更新了它并重新运行时间测试。
Simplejson在加载时比内置json快3倍 Simplejson在转储时比内置json快30%左右
免责声明:
上面的语句在python-2.7.3和simplejson 3.3.0中(带有c加速) 为了确保我的答案也不是时间敏感的,你应该运行自己的测试来检查,因为它在不同版本之间有很大的差异;没有时间紧迫的简单答案。
如何判断simplejson中是否启用了C加速:
import simplejson
# If this is True, then c speedups are enabled.
print bool(getattr(simplejson, '_speedups', False))
更新:我最近发现了一个名为ujson的库,它在一些基本测试中执行的速度比simplejson快~3倍。
在最新版本中,Json在加载和转储两种情况下都比simplejson快
测试版本:
python: 3.6.8 json: 2.0.9 simplejson: 3.16.0
结果:
>>> def test(obj, call, data, times):
... s = datetime.now()
... print("calling: ", call, " in ", obj, " ", times, " times")
... for _ in range(times):
... r = getattr(obj, call)(data)
... e = datetime.now()
... print("total time: ", str(e-s))
... return r
>>> test(json, "dumps", data, 10000)
calling: dumps in <module 'json' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\json\\__init__.py'> 10000 times
total time: 0:00:00.054857
>>> test(simplejson, "dumps", data, 10000)
calling: dumps in <module 'simplejson' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\simplejson\\__init__.py'> 10000 times
total time: 0:00:00.419895
'{"1": 100, "2": "acs", "3.5": 3.5567, "d": [1, "23"], "e": {"a": "A"}}'
>>> test(json, "loads", strdata, 1000)
calling: loads in <module 'json' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\json\\__init__.py'> 1000 times
total time: 0:00:00.004985
{'1': 100, '2': 'acs', '3.5': 3.5567, 'd': [1, '23'], 'e': {'a': 'A'}}
>>> test(simplejson, "loads", strdata, 1000)
calling: loads in <module 'simplejson' from 'C:\\Users\\jophine.antony\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\simplejson\\__init__.py'> 1000 times
total time: 0:00:00.040890
{'1': 100, '2': 'acs', '3.5': 3.5567, 'd': [1, '23'], 'e': {'a': 'A'}}
版本:
python: 3.7.4 json: 2.0.9 simplejson: 3.17.0
Json在转储操作中比simplejson快,但在加载操作中两者保持相同的速度
一些值在simplejson和json之间的序列化方式不同。
值得注意的是,collections.namedtuple的实例被json序列化为数组,而被simplejson序列化为对象。您可以通过将namedtuple_as_object=False传递给simplejson来覆盖此行为。转储,但默认情况下行为不匹配。
>>> import collections, simplejson, json
>>> TupleClass = collections.namedtuple("TupleClass", ("a", "b"))
>>> value = TupleClass(1, 2)
>>> json.dumps(value)
'[1, 2]'
>>> simplejson.dumps(value)
'{"a": 1, "b": 2}'
>>> simplejson.dumps(value, namedtuple_as_object=False)
'[1, 2]'
内置json模块被包含在Python 2.6中。任何支持Python < 2.6版本的项目都需要有一个备份。在很多情况下,这种退路很简单。
Json是simplejson,添加到stdlib中。但由于json是在2.6中添加的,因此simplejson具有在更多Python版本(2.4+)上工作的优势。
simplejson的更新频率也高于Python,因此如果您需要(或想要)最新版本,最好尽可能使用simplejson本身。
在我看来,一个好的做法是使用其中一种作为后备。
try:
import simplejson as json
except ImportError:
import json