summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorChristopher J. White <chris@grierwhite.com>2013-09-21 13:19:50 -0400
committerStefan Kögl <stefan@skoegl.net>2013-09-22 15:48:43 +0200
commit19f9f2152480c72e0f83f48930088bd22f1eded5 (patch)
tree00239b5f0037a584e52143d7729ee749194a3a8b /doc
parent48dce313141ba5bf0d2f3dd2e590042c05755e53 (diff)
downloadpython-json-pointer-19f9f2152480c72e0f83f48930088bd22f1eded5.tar.gz
Support for set_pointer and indexing arbitrary objects via __getitem__/__setitem__
Diffstat (limited to 'doc')
-rw-r--r--doc/tutorial.rst28
1 files changed, 27 insertions, 1 deletions
diff --git a/doc/tutorial.rst b/doc/tutorial.rst
index 4cdb75a..4519b29 100644
--- a/doc/tutorial.rst
+++ b/doc/tutorial.rst
@@ -7,7 +7,7 @@ method is basically a deep ``get``.
.. code-block:: python
- >>> import jsonpointer
+ >>> from jsonpointer import resolve_pointer
>>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
>>> resolve_pointer(obj, '') == obj
@@ -29,6 +29,32 @@ method is basically a deep ``get``.
True
+The ``set_pointer`` method allows modifying a portion of an object using
+JSON pointer notation:
+
+.. code-block:: python
+
+ >>> from jsonpointer import set_pointer
+ >>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
+
+ >>> set_pointer(obj, '/foo/anArray/0/prop', 55)
+ {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
+
+ >>> obj
+ {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
+
+By default ``set_pointer`` modifies the original object. Pass ``inplace=False``
+to create a copy and modify the copy instead:
+
+ >>> from jsonpointer import set_pointer
+ >>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}}
+
+ >>> set_pointer(obj, '/foo/anArray/0/prop', 55, inplace=False)
+ {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
+
+ >>> obj
+ {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 44}]}}
+
The ``JsonPointer`` class wraps a (string) path and can be used to access the
same path on several objects.