Domanda

Ho visto molti progetti utilizzando modulo simplejson invece di json modulo dalla libreria standard. Inoltre, ci sono molti moduli simplejson differenti. Perché sarebbe utilizzare queste alternative, invece di quello della libreria standard?

È stato utile?

Soluzione

json href="http://docs.python.org/whatsnew/2.6.html#the-json-module-javascript-object-notation" rel="noreferrer"> è simplejson, aggiunto alla stdlib. Ma dal momento che è stata aggiunta nel json 2.6, simplejson ha il vantaggio di lavorare su più versioni di Python (2.4+).

simplejson viene aggiornato più frequentemente di Python, quindi se avete bisogno (o vuole) l'ultima versione, è meglio usare simplejson sé, se possibile.

Una buona pratica, a mio parere, è quello di utilizzare uno o l'altro come un ripiego.

try:
    import simplejson as json
except ImportError:
    import json

Altri suggerimenti

Non sono d'accordo con le altre risposte: il costruito nel json biblioteca (in Python 2.7) non è necessariamente più lento di simplejson . Inoltre non ha questo fastidioso bug unicode .

Qui è un punto di riferimento semplice:

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)

E i risultati sul mio sistema (Python 2.7.4, Linux a 64-bit):

  

Complesso di dati del mondo reale:
        JSON discariche 1.56666707993 secondi
  simplejson discariche 2.25638604164 secondi
        carichi JSON 2.71256899834 secondi
  carichi simplejson 1.29233884811 secondi

     

Dati Semplice:
        JSON discariche 0.370109081268 secondi
  simplejson discariche 0.574181079865 secondi
        carichi JSON 0.422876119614 secondi
  carichi simplejson 0.270955085754 secondi

Per dumping, json è più veloce di simplejson. Per il carico, simplejson è più veloce.

Dal momento che attualmente sto costruendo un servizio web, dumps() è più importante e l'utilizzo di una libreria standard è sempre preferito.

Inoltre, cjson non è stato aggiornato negli ultimi 4 anni, in modo da non toccare.

Tutte queste risposte non sono molto utili perché sono momento delicato .

Dopo aver fatto alcune ricerche di mio ho trovato che simplejson è davvero più veloce del comando incorporato, se si mantiene l'aggiornamento alla versione più recente.

pip/easy_install voluto installare 2.3.2 su Ubuntu 12.04, ma dopo aver scoperto l'ultima versione simplejson è in realtà 3.3.0, così ho aggiornato e nuovamente eseguita il test del tempo.

  • simplejson è circa 3 volte più veloce rispetto alla json incorporato a carichi
  • simplejson è di circa il 30% più veloce rispetto alla json incorporato in discariche

Disclaimer:

Quanto sopra sono in pitone-2.7.3 e 3.3.0 simplejson (con c speedups) E per assicurarsi che la mia risposta non è anche il tempo sensibile, si dovrebbe eseguire i propri test per verificare poiché varia così tanto tra le versioni; non c'è una risposta semplice che non è sensibile al tempo.

Come sapere se C incrementi nella velocità sono abilitati in simplejson:

import simplejson
# If this is True, then c speedups are enabled.
print bool(getattr(simplejson, '_speedups', False))

UPDATE: di recente è venuto in una libreria chiamata ujson che sta eseguendo ~ 3 volte più veloce rispetto simplejson con alcuni test di base.

Sono di test comparativi di JSON, simplejson e cjson.

  • cjson è più veloce
  • simplejson è quasi alla pari con cjson
  • JSON è circa 10 volte più lento di simplejson

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

Alcuni valori vengono serializzati in modo diverso tra simplejson e JSON.

In particolare, le istanze di collections.namedtuple vengono serializzati come array di json ma come oggetti da simplejson. È possibile ignorare questo comportamento passando namedtuple_as_object=False a simplejson.dump, ma probabilmente i comportamenti non corrispondono.

>>> 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]'

Un incompatibilità API ho trovato, con Python 2.7 vs simplejson 3.3.1 è in se uscita produce oggetti str o unicode. per es.

>>> from json import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{u'a': u'b'}

vs

>>> from simplejson import JSONDecoder
>>> jd = JSONDecoder()
>>> jd.decode("""{ "a":"b" }""")
{'a': 'b'}

Se la preferenza è quella di utilizzare simplejson, allora questo può essere affrontato da costringere la stringa argomento unicode, come in:

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

La coercizione non richiede conoscere il set di caratteri originali, ad esempio:

>>> 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)

Questa è la volontà di non risolvere problema 40

Il modulo integrato json ottenuto incluso in Python 2.6. Tutti i progetti che sostengono versioni di Python <2,6 bisogno di avere un ripiego. In molti casi, che fallback è simplejson.

Un altro motivo Utilizza progetti simplejson è che il JSON incorporato non ha inizialmente includere i suoi incrementi nella velocità C, quindi la differenza di prestazioni era evidente.

Ecco (una ormai superata) confronto delle librerie JSON Python:

Confrontando moduli JSON per Python ( archivio link )

A prescindere dai risultati di questo confronto è necessario utilizzare la libreria standard JSON se si è in Python 2.6. E .. potrebbe anche semplicemente usare simplejson altrimenti.

modulo simplejson è semplicemente 1,5 volte più veloce di JSON (Sul mio computer, con simplejson 2.1.1 e Python 2.7 x 86).

Se si vuole, si può provare il punto di riferimento: http://abral.altervista.org /jsonpickle-bench.zip Sul mio PC simplejson è più veloce di cPickle. Vorrei sapere anche i vostri punti di riferimento!

Probabilmente, come detto Coady, la differenza tra simplejson e JSON è che simplejson include _speedups.c. Quindi, perché non Python sviluppatori utilizzano simplejson?

In python3, se una serie di b'bytes', con json bisogna .decode() il contenuto prima di poter caricare. simplejson si prende cura di questo in modo da poter fare solo simplejson.loads(byte_string).

mi sono imbattuto in questa domanda come stavo cercando di installare simplejson per Python 2.6. Avevo bisogno di utilizzare il 'object_pairs_hook' di json.load () al fine di caricare un file JSON come OrderedDict. Avere familiarità con le versioni più recenti di Python non mi rendevo conto che il modulo JSON per Python 2.6 non include il 'object_pairs_hook' così ho dovuto installare simplejson per questo scopo. Per esperienza personale questo è il motivo per cui io uso simplejson in contrasto con il modulo json standard.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top