@@ -377,47 +377,64 @@ File objects have some additional methods, such as :meth:`~file.isatty` and
377377Reference for a complete guide to file objects.
378378
379379
380- .. _tut-pickle :
380+ .. _tut-json :
381381
382- The :mod: `pickle ` Module
383- ------------------------
382+ Saving structured data with :mod: `json `
383+ ---------------------------------------
384384
385- .. index :: module: pickle
385+ .. index :: module: json
386386
387- Strings can easily be written to and read from a file. Numbers take a bit more
387+ Strings can easily be written to and read from a file. Numbers take a bit more
388388effort, since the :meth: `read ` method only returns strings, which will have to
389389be passed to a function like :func: `int `, which takes a string like ``'123' ``
390- and returns its numeric value 123. However, when you want to save more complex
391- data types like lists, dictionaries, or class instances, things get a lot more
392- complicated.
393-
394- Rather than have users be constantly writing and debugging code to save
395- complicated data types, Python provides a standard module called :mod: `pickle `.
396- This is an amazing module that can take almost any Python object (even some
397- forms of Python code!), and convert it to a string representation; this process
398- is called :dfn: `pickling `. Reconstructing the object from the string
399- representation is called :dfn: `unpickling `. Between pickling and unpickling,
400- the string representing the object may have been stored in a file or data, or
390+ and returns its numeric value 123. When you want to save more complex data
391+ types like nested lists and dictionaries, parsing and serializing by hand
392+ becomes complicated.
393+
394+ Rather than having users constantly writing and debugging code to save
395+ complicated data types to files, Python allows you to use the popular data
396+ interchange format called `JSON (JavaScript Object Notation)
397+ <http://json.org> `_. The standard module called :mod: `json ` can take Python
398+ data hierarchies, and convert them to string representations; this process is
399+ called :dfn: `serializing `. Reconstructing the data from the string representation
400+ is called :dfn: `deserializing `. Between serializing and deserializing, the
401+ string representing the object may have been stored in a file or data, or
401402sent over a network connection to some distant machine.
402403
403- If you have an object ``x ``, and a file object ``f `` that's been opened for
404- writing, the simplest way to pickle the object takes only one line of code::
404+ .. note ::
405+ The JSON format is commonly used by modern applications to allow for data
406+ exchange. Many programmers are already familiar with it, which makes
407+ it a good choice for interoperability.
405408
406- pickle.dump(x, f)
409+ If you have an object ``x ``, you can view its JSON string representation with a
410+ simple line of code::
407411
408- To unpickle the object again, if `` f `` is a file object which has been opened
409- for reading::
412+ >>> json.dumps([1, 'simple', 'list'])
413+ '[1, "simple", "list"]'
410414
411- x = pickle.load(f)
415+ Another variant of the :func: `~json.dumps ` function, called :func: `~json.dump `,
416+ simply serializes the object to a :term: `text file `. So if ``f `` is a
417+ :term: `text file ` object opened for writing, we can do this::
412418
413- (There are other variants of this, used when pickling many objects or when you
414- don't want to write the pickled data to a file; consult the complete
415- documentation for :mod: `pickle ` in the Python Library Reference.)
419+ json.dump(x, f)
416420
417- :mod: `pickle ` is the standard way to make Python objects which can be stored and
418- reused by other programs or by a future invocation of the same program; the
419- technical term for this is a :dfn: `persistent ` object. Because :mod: `pickle ` is
420- so widely used, many authors who write Python extensions take care to ensure
421- that new data types such as matrices can be properly pickled and unpickled.
421+ To decode the object again, if ``f `` is a :term: `text file ` object which has
422+ been opened for reading::
422423
424+ x = json.load(f)
425+
426+ This simple serialization technique can handle lists and dictionaries, but
427+ serializing arbitrary class instances in JSON requires a bit of extra effort.
428+ The reference for the :mod: `json ` module contains an explanation of this.
429+
430+ .. seealso ::
431+
432+ :mod: `pickle ` - the pickle module
433+
434+ Contrary to :ref: `JSON <tut-json >`, *pickle * is a protocol which allows
435+ the serialization of arbitrarily complex Python objects. As such, it is
436+ specific to Python and cannot be used to communicate with applications
437+ written in other languages. It is also insecure by default:
438+ deserializing pickle data coming from an untrusted source can execute
439+ arbitrary code, if the data was crafted by a skilled attacker.
423440
0 commit comments