json和simplejson Python模块之间有什么区别?

Lak*_*sad 372 python json simplejson

我见过很多项目使用simplejson模块而不是json标准库中的模块.此外,还有许多不同的simplejson模块.为什么要使用这些替代品,而不是标准库中的替代品?

Dev*_*rre 378

json simplejson,添加到stdlib.但是自从json2.6添加以来,它simplejson具有处理更多Python版本(2.4+)的优势.

simplejson也比Python更频繁地更新,因此如果您需要(或想要)最新版本,最好尽可能使用simplejson自己.

在我看来,一个好的做法是使用一个或另一个作为后备.

try:
    import simplejson as json
except ImportError:
    import json
Run Code Online (Sandbox Code Playgroud)

  • 假设您拥有最新的Python,我不同意上述答案.Python 2.7中的内置(很棒的!!!)Json库与simplejson一样快,并且具有较少的拒绝修复的unicode错误.请参阅答案http://stackoverflow.com/a/16131316/78234 (29认同)
  • 它们不相同也不兼容,simplejson有JSONDecodeError,json有ValueError (4认同)
  • @BjornTipling`JSONDecodeError`是`ValueError`的子类 (3认同)
  • 现在,如果我只能让pyflakes停止抱怨'重新定义未使用的'json' (2认同)

Tal*_*iss 81

我不同意其他答案:内置json库(在Python 2.7中)不一定比它慢simplejson.它也没有这个讨厌的unicode bug.

这是一个简单的基准:

import json
import simplejson
from timeit import repeat

NUMBER = 100000
REPEAT = 10

def compare_json_and_simplejson(data):
    """Compare json and simplejson - dumps and loads"""
    compare_json_and_simplejson.data = data
    compare_json_and_simplejson.dump = json.dumps(data)
    assert json.dumps(data) == simplejson.dumps(data)
    result = min(repeat("json.dumps(compare_json_and_simplejson.data)", "from __main__ import json, compare_json_and_simplejson", 
                 repeat = REPEAT, number = NUMBER))
    print "      json dumps {} seconds".format(result)
    result = min(repeat("simplejson.dumps(compare_json_and_simplejson.data)", "from __main__ import simplejson, compare_json_and_simplejson", 
                 repeat = REPEAT, number = NUMBER))
    print "simplejson dumps {} seconds".format(result)
    assert json.loads(compare_json_and_simplejson.dump) == data
    result = min(repeat("json.loads(compare_json_and_simplejson.dump)", "from __main__ import json, compare_json_and_simplejson", 
                 repeat = REPEAT, number = NUMBER))
    print "      json loads {} seconds".format(result)
    result = min(repeat("simplejson.loads(compare_json_and_simplejson.dump)", "from __main__ import simplejson, compare_json_and_simplejson", 
                 repeat = REPEAT, number = NUMBER))
    print "simplejson loads {} seconds".format(result)


print "Complex real world data:" 
COMPLEX_DATA = {'status': 1, 'timestamp': 1362323499.23, 'site_code': 'testing123', 'remote_address': '212.179.220.18', 'input_text': u'ny monday for less than \u20aa123', 'locale_value': 'UK', 'eva_version': 'v1.0.3286', 'message': 'Successful Parse', 'muuid1': '11e2-8414-a5e9e0fd-95a6-12313913cc26', 'api_reply': {"api_reply": {"Money": {"Currency": "ILS", "Amount": "123", "Restriction": "Less"}, "ProcessedText": "ny monday for less than \\u20aa123", "Locations": [{"Index": 0, "Derived From": "Default", "Home": "Default", "Departure": {"Date": "2013-03-04"}, "Next": 10}, {"Arrival": {"Date": "2013-03-04", "Calculated": True}, "Index": 10, "All Airports Code": "NYC", "Airports": "EWR,JFK,LGA,PHL", "Name": "New York City, New York, United States (GID=5128581)", "Latitude": 40.71427, "Country": "US", "Type": "City", "Geoid": 5128581, "Longitude": -74.00597}]}}}
compare_json_and_simplejson(COMPLEX_DATA)
print "\nSimple data:"
SIMPLE_DATA = [1, 2, 3, "asasd", {'a':'b'}]
compare_json_and_simplejson(SIMPLE_DATA)
Run Code Online (Sandbox Code Playgroud)

我的系统上的结果(Python 2.7.4,Linux 64位):

复杂的现实世界的数据:
JSON转储1.56666707993秒
simplejson转储2.25638604164秒
JSON负载2.71256899834秒
simplejson负荷1.29233884811秒

简单的数据:
JSON转储0.370109081268秒
simplejson转储0.574181079865秒
JSON负载0.422876119614秒
simplejson负荷0.270955085754秒

对于倾销,json比快simplejson.对于加载,simplejson速度更快.

由于我目前正在构建Web服务,dumps()因此更为重要 - 并且始终首选使用标准库.

此外,cjson在过去4年没有更新,所以我不会碰它.

  • 在我的Win7 PC(i7 CPU)上,`json`(CPython 3.5.0)在简单复杂转储上的速度提高了68%| 45%|在简单复杂的情况下提高了35%| 17%|带有C的simplejson` v3.8.0使用基准代码加速.因此,我不会再使用simplejson这个设置了. (2认同)

not*_*peg 26

所有这些答案都不是很有用,因为它们对时间敏感.

在对我自己做了一些研究之后,我发现它simplejson确实比内置更快,如果你把它保持更新到最新版本.

pip/easy_install想在ubuntu 12.04上安装2.3.2,但在找到最新simplejson版本后实际是3.3.0,所以我更新了它并重新进行了时间测试.

  • simplejsonjson在加载时比内置驱动器快3倍
  • simplejsonjson转储中的内置快约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))
Run Code Online (Sandbox Code Playgroud)

更新:我最近遇到了一个名为ujson的库,其执行速度比simplejson基本测试快约3倍.


Chr*_*ris 21

我一直在对json,simplejson和cjson进行基准测试.

  • cjson是最快的
  • simplejson几乎与cjson相提并论
  • json比simplejson慢大约10倍

http://pastie.org/1507411:

$ python test_serialization_speed.py 
--------------------
   Encoding Tests
--------------------
Encoding: 100000 x {'m': 'asdsasdqwqw', 't': 3}
[      json] 1.12385 seconds for 100000 runs. avg: 0.011239ms
[simplejson] 0.44356 seconds for 100000 runs. avg: 0.004436ms
[     cjson] 0.09593 seconds for 100000 runs. avg: 0.000959ms

Encoding: 10000 x {'m': [['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19], ['0', 1, '2', 3, '4', 5, '6', 7, '8', 9, '10', 11, '12', 13, '14', 15, '16', 17, '18', 19]], 't': 3}
[      json] 7.76628 seconds for 10000 runs. avg: 0.776628ms
[simplejson] 0.51179 seconds for 10000 runs. avg: 0.051179ms
[     cjson] 0.44362 seconds for 10000 runs. avg: 0.044362ms

--------------------
   Decoding Tests
--------------------
Decoding: 100000 x {"m": "asdsasdqwqw", "t": 3}
[      json] 3.32861 seconds for 100000 runs. avg: 0.033286ms
[simplejson] 0.37164 seconds for 100000 runs. avg: 0.003716ms
[     cjson] 0.03893 seconds for 100000 runs. avg: 0.000389ms

Decoding: 10000 x {"m": [["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19], ["0", 1, "2", 3, "4", 5, "6", 7, "8", 9, "10", 11, "12", 13, "14", 15, "16", 17, "18", 19]], "t": 3}
[      json] 37.26270 seconds for 10000 runs. avg: 3.726270ms
[simplejson] 0.56643 seconds for 10000 runs. avg: 0.056643ms
[     cjson] 0.33007 seconds for 10000 runs. avg: 0.033007ms
Run Code Online (Sandbox Code Playgroud)

  • 请为实际测试模块添加一个贴图. (6认同)
  • 这不再适用.python2.7中的json是性能改进. (6认同)
  • 哪个版本的Python和libs有问题? (4认同)

pfh*_*yes 9

有些值在simplejson和json之间有不同的序列化.

值得注意的是,实例collections.namedtuple被序列化为数组,json但作为对象simplejson.您可以通过传递namedtuple_as_object=False来覆盖此行为simplejson.dump,但默认情况下行为不匹配.

>>> 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]'
Run Code Online (Sandbox Code Playgroud)


jjc*_*jjc 7

我发现,使用Python 2.7与simplejson 3.3.1的API不兼容是输出是否生成str或unicode对象.例如

>>> from json import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{u'a': u'b'}
Run Code Online (Sandbox Code Playgroud)

VS

>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{'a': 'b'}
Run Code Online (Sandbox Code Playgroud)

如果首选项是使用simplejson,那么可以通过将参数字符串强制转换为unicode来解决此问题,如下所示:

>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode(unicode("""{ "a":"b" }""", "utf-8"))
{u'a': u'b'}
Run Code Online (Sandbox Code Playgroud)

强制确实需要知道原始的字符集,例如:

>>> jd.decode(unicode("""{ "a": "????????????" }"""))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 8: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

这是不会解决问题的问题40


the*_*edz 5

内置json模块包含在Python 2.6中.任何支持Python <2.6版本的项目都需要备用.在许多情况下,这种后退是simplejson.


A. *_*ady 5

项目使用simplejson的另一个原因是内置json最初没有包含其C加速,因此性能差异显而易见.