views:

329

answers:

4
+2  Q: 

Pickle or json?

I need to save to disk a little dict object which keys are strings and values are ints and then recover it. Something like this:

{'juanjo': 2, 'pedro':99, 'other': 333}

Which and why is the best option? Serialize it with pickle or with simplejson?

I'm using Python 2.6

+5  A: 

If you do not have any interoperability requirements (i.e. you're just going to use the data with Python), and a binary format is fine, go with cPickle, which gives you really fast Python object serialization.

If you want interoperability, or you want a text format to store your data, go with JSON (or some other appropriate format depending on your constraints).

Håvard S
+2  A: 

I prefer JSON over pickle for my serialization. Unpickling can run arbitrary code, and using pickle to transfer data between programs or store data between sessions is a security hole. JSON does not introduce a security hole and is standardized, so the data can be accessed by programs in different languages if you ever need to.

Mike Graham
Thanks. Anyway I'll be dumping and loading in the same program.
Juanjo Conti
Though the security risks may be low in your current application, JSON allows you to close the whole altogether.
Mike Graham
A: 

Json or pickle? How about json and pickle! You can use jsonpickle. It easy to use and the file on disk is readable because it's json.

http://jsonpickle.github.com/

Paul Hildebrandt
A: 

Later on one may want to persist some arbitrary Python object. In that case, clearly cPickle plus compression offers speed. Take a look at:

y_serial.py module :: warehouse Python objects with SQLite

"Serialization + persistance :: in a few lines of code, compress and annotate Python objects into SQLite; then later retrieve them chronologically by keywords without any SQL. Most useful "standard" module for a database to store schema-less data."

http://yserial.sourceforge.net

The security issue is also carefully addressed in the internal documentation. This Python module has broad application, and it's very easy to implement < 10 minutes.

code43