diff options
Diffstat (limited to 'doc')
-rw-r--r-- | doc/tutorial.rst | 28 |
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. |