From 4fbc739be7131eae487e0e3dba0f3ac622e96e9d Mon Sep 17 00:00:00 2001 From: Matthew Lewis Date: Thu, 5 Feb 2015 13:57:28 -0600 Subject: [PATCH 1/4] Add details on how to use simplejson --- docs/scenarios/json.rst | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index a3f7ffa01..225ed2334 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -26,7 +26,7 @@ and can now be used as a normal dictionary: print(parsed_json['first_name']) "Guido" -You can also convert a the following to JSON: +You can also convert the following to JSON: .. code-block:: python @@ -43,6 +43,14 @@ You can also convert a the following to JSON: simplejson ---------- -`simplejson `_ is the externally maintained development version of the json library. +The JSON library was added to Python in version 2.6. If you're using an earlier version of Python, the `simplejson `_ library is available via PyPI. -simplejson mimics the json standard library, it is available so that developers that use an older version of python can use the latest features available in the json lib. +simplejson mimics the json standard library. It is available so that developers that use older versions of Python can use the latest features available in the json lib. + +You can start using simplejson when the json library is not available by importing simplejson under a different name: + +.. code-block:: python + + import simplejson as json + +After importing simplejson as json, the above examples will all work as if you were using the standard json library. From ecc570d1c421a1f6ea285dfeb202a037ce74b912 Mon Sep 17 00:00:00 2001 From: Matthew Lewis Date: Thu, 5 Feb 2015 14:03:06 -0600 Subject: [PATCH 2/4] Improve wording of intro paragraph --- docs/scenarios/json.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 225ed2334..2ca49e8cc 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -1,7 +1,7 @@ JSON ==== -The `json `_ library can parse JSON from strings or files. When parsing, the library converts the JSON into a Python dictionary or list. It can also parse Python dictionaries or lists into JSON strings. +The `json `_ library can parse JSON from strings or files. The library parses JSON into a Python dictionary or list. It can also convert Python dictionaries or lists into JSON strings. Parsing JSON ------------ From 2e2d70ca9be42ae644d202fdf5c7673dae65c566 Mon Sep 17 00:00:00 2001 From: Matthew Lewis Date: Thu, 5 Feb 2015 14:03:18 -0600 Subject: [PATCH 3/4] Add info on working with file objects --- docs/scenarios/json.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 2ca49e8cc..98a0292e1 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -39,6 +39,36 @@ You can also convert the following to JSON: print(json.dumps(d)) '{"first_name": "Guido", "last_name": "Rossum", "titles": ["BDFL", "Developer"]}' +Working with File Objects +------------------------- + +We can also load a JSON file by using ``json.load`` instead of ``json.loads``: + +.. code-block:: python + + import json + + with file('path/to/file.json') as f: + loaded_json = json.load(f) + + print(loaded_json) # {'first_name': 'Ada', 'last_name': 'Lovelace'} + +Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``: + +.. code-block:: python + + import json + + my_data = { + 'name': 'Alan Turing', + 'played_by': 'Benedict Cumberbatch' + } + + with file('path/to/file.json', 'w') as f: + json.dump(my_data, f) + +``path/to/file.json`` now contains a JSON representation of the my_data dictionary. + simplejson ---------- From f6d503dd90f6782f55de4a4fdeb21fe82a53b643 Mon Sep 17 00:00:00 2001 From: Matthew Lewis Date: Wed, 11 Feb 2015 15:18:04 -0600 Subject: [PATCH 4/4] Revert "Add info on working with file objects" This reverts commit 2e2d70ca9be42ae644d202fdf5c7673dae65c566. --- docs/scenarios/json.rst | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 98a0292e1..2ca49e8cc 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -39,36 +39,6 @@ You can also convert the following to JSON: print(json.dumps(d)) '{"first_name": "Guido", "last_name": "Rossum", "titles": ["BDFL", "Developer"]}' -Working with File Objects -------------------------- - -We can also load a JSON file by using ``json.load`` instead of ``json.loads``: - -.. code-block:: python - - import json - - with file('path/to/file.json') as f: - loaded_json = json.load(f) - - print(loaded_json) # {'first_name': 'Ada', 'last_name': 'Lovelace'} - -Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``: - -.. code-block:: python - - import json - - my_data = { - 'name': 'Alan Turing', - 'played_by': 'Benedict Cumberbatch' - } - - with file('path/to/file.json', 'w') as f: - json.dump(my_data, f) - -``path/to/file.json`` now contains a JSON representation of the my_data dictionary. - simplejson ----------