jsonpatch 1.38__tar.gz → 1.39__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {jsonpatch-1.38 → jsonpatch-1.39}/MANIFEST.in +1 -0
- {jsonpatch-1.38/jsonpatch.egg-info → jsonpatch-1.39}/PKG-INFO +1 -1
- jsonpatch-1.39/doc/commandline.rst +119 -0
- jsonpatch-1.39/doc/index.rst +37 -0
- jsonpatch-1.39/doc/mod-jsonpatch.rst +7 -0
- jsonpatch-1.39/doc/tutorial.rst +268 -0
- {jsonpatch-1.38 → jsonpatch-1.39/jsonpatch.egg-info}/PKG-INFO +1 -1
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch.egg-info/SOURCES.txt +4 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch.py +3 -1
- {jsonpatch-1.38 → jsonpatch-1.39}/tests.py +165 -20
- {jsonpatch-1.38 → jsonpatch-1.39}/AUTHORS +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/LICENSE +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/README.md +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/ext_tests.py +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch.egg-info/dependency_links.txt +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch.egg-info/entry_points.txt +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch.egg-info/requires.txt +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch.egg-info/top_level.txt +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/jsonpatch_cli.py +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/property_tests.py +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/pyproject.toml +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/pytest.ini +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/requirements.txt +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/setup.cfg +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/setup.py +0 -0
- {jsonpatch-1.38 → jsonpatch-1.39}/tests.js +0 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Commandline Utilities
|
|
2
|
+
=====================
|
|
3
|
+
|
|
4
|
+
The JSON patch package contains the commandline utilities ``jsondiff`` and
|
|
5
|
+
``jsonpatch``.
|
|
6
|
+
|
|
7
|
+
Both utilities read JSON files as UTF-8. When ``jsonpatch`` modifies a file
|
|
8
|
+
in-place, it also writes UTF-8.
|
|
9
|
+
|
|
10
|
+
``jsondiff``
|
|
11
|
+
------------
|
|
12
|
+
|
|
13
|
+
The program ``jsondiff`` can be used to create a JSON patch by comparing two
|
|
14
|
+
JSON files ::
|
|
15
|
+
|
|
16
|
+
usage: jsondiff [-h] [--indent INDENT] [-u] [-v] FILE1 FILE2
|
|
17
|
+
|
|
18
|
+
Diff two JSON files
|
|
19
|
+
|
|
20
|
+
positional arguments:
|
|
21
|
+
FILE1
|
|
22
|
+
FILE2
|
|
23
|
+
|
|
24
|
+
options:
|
|
25
|
+
-h, --help show this help message and exit
|
|
26
|
+
--indent INDENT Indent output by n spaces
|
|
27
|
+
-u, --preserve-unicode
|
|
28
|
+
Output Unicode character as-is without using Code
|
|
29
|
+
Point
|
|
30
|
+
-v, --version show program's version number and exit
|
|
31
|
+
|
|
32
|
+
Example
|
|
33
|
+
^^^^^^^
|
|
34
|
+
|
|
35
|
+
.. code-block:: bash
|
|
36
|
+
|
|
37
|
+
# inspect JSON files
|
|
38
|
+
$ cat a.json
|
|
39
|
+
{ "a": [1, 2], "b": 0 }
|
|
40
|
+
|
|
41
|
+
$ cat b.json
|
|
42
|
+
{ "a": [1, 2, 3], "c": 100 }
|
|
43
|
+
|
|
44
|
+
# show patch in "dense" representation
|
|
45
|
+
$ jsondiff a.json b.json
|
|
46
|
+
[{"op": "remove", "path": "/b"}, {"op": "add", "path": "/c", "value": 100}, {"op": "add", "path": "/a/2", "value": 3}]
|
|
47
|
+
|
|
48
|
+
# show patch with some indentation
|
|
49
|
+
$ jsondiff a.json b.json --indent=2
|
|
50
|
+
[
|
|
51
|
+
{
|
|
52
|
+
"op": "remove",
|
|
53
|
+
"path": "/b"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"op": "add",
|
|
57
|
+
"path": "/c",
|
|
58
|
+
"value": 100
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"op": "add",
|
|
62
|
+
"path": "/a/2",
|
|
63
|
+
"value": 3
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
``jsonpatch``
|
|
70
|
+
-------------
|
|
71
|
+
|
|
72
|
+
The program ``jsonpatch`` is used to apply JSON patches on JSON files. ::
|
|
73
|
+
|
|
74
|
+
usage: jsonpatch [-h] [--indent INDENT] [-b] [-i] [-v] [-u] ORIGINAL [PATCH]
|
|
75
|
+
|
|
76
|
+
Apply a JSON patch on a JSON file
|
|
77
|
+
|
|
78
|
+
positional arguments:
|
|
79
|
+
ORIGINAL Original file
|
|
80
|
+
PATCH Patch file (read from stdin if omitted)
|
|
81
|
+
|
|
82
|
+
options:
|
|
83
|
+
-h, --help show this help message and exit
|
|
84
|
+
--indent INDENT Indent output by n spaces
|
|
85
|
+
-b, --backup Back up ORIGINAL if modifying in-place
|
|
86
|
+
-i, --in-place Modify ORIGINAL in-place instead of to stdout
|
|
87
|
+
-v, --version show program's version number and exit
|
|
88
|
+
-u, --preserve-unicode
|
|
89
|
+
Output Unicode character as-is without using Code
|
|
90
|
+
Point
|
|
91
|
+
|
|
92
|
+
Example
|
|
93
|
+
^^^^^^^
|
|
94
|
+
|
|
95
|
+
.. code-block:: bash
|
|
96
|
+
|
|
97
|
+
# create a patch
|
|
98
|
+
$ jsondiff a.json b.json > patch.json
|
|
99
|
+
|
|
100
|
+
# show the result after applying a patch
|
|
101
|
+
$ jsonpatch a.json patch.json
|
|
102
|
+
{"a": [1, 2, 3], "c": 100}
|
|
103
|
+
|
|
104
|
+
$ jsonpatch a.json patch.json --indent=2
|
|
105
|
+
{
|
|
106
|
+
"a": [
|
|
107
|
+
1,
|
|
108
|
+
2,
|
|
109
|
+
3
|
|
110
|
+
],
|
|
111
|
+
"c": 100
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# pipe result into new file
|
|
115
|
+
$ jsonpatch a.json patch.json --indent=2 > c.json
|
|
116
|
+
|
|
117
|
+
# c.json now equals b.json, so there is no difference to print
|
|
118
|
+
$ jsondiff b.json c.json
|
|
119
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
.. python-json-patch documentation master file, created by
|
|
2
|
+
sphinx-quickstart on Sat Apr 13 23:07:23 2013.
|
|
3
|
+
You can adapt this file completely to your liking, but it should at least
|
|
4
|
+
contain the root `toctree` directive.
|
|
5
|
+
|
|
6
|
+
python-json-patch
|
|
7
|
+
=================
|
|
8
|
+
|
|
9
|
+
`python-json-patch <https://github.com/stefankoegl/python-json-patch>`_
|
|
10
|
+
is a Python library for applying JSON patches (`RFC 6902
|
|
11
|
+
<https://datatracker.ietf.org/doc/html/rfc6902>`_). Python 3.10+ is
|
|
12
|
+
supported. Tests are run on both CPython and PyPy.
|
|
13
|
+
|
|
14
|
+
**Installation**
|
|
15
|
+
|
|
16
|
+
.. code-block:: bash
|
|
17
|
+
|
|
18
|
+
$ pip install jsonpatch
|
|
19
|
+
|
|
20
|
+
**Contents**
|
|
21
|
+
|
|
22
|
+
.. toctree::
|
|
23
|
+
:maxdepth: 2
|
|
24
|
+
|
|
25
|
+
tutorial
|
|
26
|
+
mod-jsonpatch
|
|
27
|
+
commandline
|
|
28
|
+
RFC 6902 <https://datatracker.ietf.org/doc/html/rfc6902>
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Indices and tables
|
|
32
|
+
==================
|
|
33
|
+
|
|
34
|
+
* :ref:`genindex`
|
|
35
|
+
* :ref:`modindex`
|
|
36
|
+
* :ref:`search`
|
|
37
|
+
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
Tutorial
|
|
2
|
+
========
|
|
3
|
+
|
|
4
|
+
Please refer to `RFC 6902 <https://datatracker.ietf.org/doc/html/rfc6902>`_ for the exact
|
|
5
|
+
patch syntax.
|
|
6
|
+
|
|
7
|
+
Creating a Patch
|
|
8
|
+
----------------
|
|
9
|
+
|
|
10
|
+
Patches can be created in two ways. One way is to explicitly create a
|
|
11
|
+
``JsonPatch`` object from an iterable of operation dictionaries, such as a
|
|
12
|
+
list, tuple, iterator, or generator. Non-sequence iterables are materialized
|
|
13
|
+
before validation so that the patch can be applied repeatedly.
|
|
14
|
+
For convenience, the method
|
|
15
|
+
``JsonPatch.from_string()`` accepts a string, parses it and constructs the
|
|
16
|
+
patch object from it.
|
|
17
|
+
|
|
18
|
+
.. code-block:: python
|
|
19
|
+
|
|
20
|
+
>>> import jsonpatch
|
|
21
|
+
>>> patch = jsonpatch.JsonPatch([
|
|
22
|
+
... {'op': 'add', 'path': '/foo', 'value': 'bar'},
|
|
23
|
+
... {'op': 'add', 'path': '/baz', 'value': [1, 2, 3]},
|
|
24
|
+
... {'op': 'remove', 'path': '/baz/1'},
|
|
25
|
+
... {'op': 'test', 'path': '/baz', 'value': [1, 3]},
|
|
26
|
+
... {'op': 'replace', 'path': '/baz/0', 'value': 42},
|
|
27
|
+
... {'op': 'remove', 'path': '/baz/1'},
|
|
28
|
+
... ])
|
|
29
|
+
|
|
30
|
+
# or equivalently
|
|
31
|
+
>>> patch = jsonpatch.JsonPatch.from_string('''[
|
|
32
|
+
... {"op": "add", "path": "/foo", "value": "bar"},
|
|
33
|
+
... {"op": "add", "path": "/baz", "value": [1, 2, 3]},
|
|
34
|
+
... {"op": "remove", "path": "/baz/1"},
|
|
35
|
+
... {"op": "test", "path": "/baz", "value": [1, 3]},
|
|
36
|
+
... {"op": "replace", "path": "/baz/0", "value": 42},
|
|
37
|
+
... {"op": "remove", "path": "/baz/1"}
|
|
38
|
+
... ]''')
|
|
39
|
+
>>> patch.apply({})
|
|
40
|
+
{'foo': 'bar', 'baz': [42]}
|
|
41
|
+
|
|
42
|
+
Another way is to *diff* two objects.
|
|
43
|
+
|
|
44
|
+
.. code-block:: python
|
|
45
|
+
|
|
46
|
+
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
|
|
47
|
+
>>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
|
|
48
|
+
>>> patch = jsonpatch.JsonPatch.from_diff(src, dst)
|
|
49
|
+
|
|
50
|
+
# or equivalently
|
|
51
|
+
>>> patch = jsonpatch.make_patch(src, dst)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
Applying a Patch
|
|
55
|
+
----------------
|
|
56
|
+
|
|
57
|
+
A patch is applied to a JSON document. The patch created by diffing ``src``
|
|
58
|
+
and ``dst`` above turns ``src`` into ``dst``:
|
|
59
|
+
|
|
60
|
+
.. code-block:: python
|
|
61
|
+
|
|
62
|
+
>>> result = patch.apply(src)
|
|
63
|
+
>>> result == dst
|
|
64
|
+
True
|
|
65
|
+
|
|
66
|
+
The ``apply`` method returns a new object as a result. If ``in_place=True`` the
|
|
67
|
+
object is modified in place.
|
|
68
|
+
|
|
69
|
+
The operations of a patch refer to locations in the object it is applied to.
|
|
70
|
+
A patch created with ``make_patch(src, dst)`` or ``JsonPatch.from_diff(src,
|
|
71
|
+
dst)`` must therefore be applied to ``src`` (or to an object equal to it).
|
|
72
|
+
Applying it to any other object, such as ``dst``, usually fails with a
|
|
73
|
+
``JsonPatchConflict``, because the locations it refers to don't exist there:
|
|
74
|
+
|
|
75
|
+
.. code-block:: python
|
|
76
|
+
|
|
77
|
+
>>> patch.apply(dst)
|
|
78
|
+
Traceback (most recent call last):
|
|
79
|
+
...
|
|
80
|
+
jsonpatch.JsonPatchConflict: can't remove a non-existent object 'foo'
|
|
81
|
+
|
|
82
|
+
If a patch is only used once, it is not necessary to create a patch object
|
|
83
|
+
explicitly.
|
|
84
|
+
|
|
85
|
+
.. code-block:: python
|
|
86
|
+
|
|
87
|
+
>>> obj = {'foo': 'bar'}
|
|
88
|
+
|
|
89
|
+
# from a patch string
|
|
90
|
+
>>> patch = '[{"op": "add", "path": "/baz", "value": "qux"}]'
|
|
91
|
+
>>> res = jsonpatch.apply_patch(obj, patch)
|
|
92
|
+
|
|
93
|
+
# or from a list
|
|
94
|
+
>>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}]
|
|
95
|
+
>>> res = jsonpatch.apply_patch(obj, patch)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
When an Operation Fails
|
|
99
|
+
~~~~~~~~~~~~~~~~~~~~~~~
|
|
100
|
+
|
|
101
|
+
Operations are applied in order. If an operation raises an exception,
|
|
102
|
+
application stops. With the default ``in_place=False``, operations affect a
|
|
103
|
+
copy, so the original document is unchanged:
|
|
104
|
+
|
|
105
|
+
.. code-block:: python
|
|
106
|
+
|
|
107
|
+
>>> import jsonpatch
|
|
108
|
+
>>> original = {'value': 1}
|
|
109
|
+
>>> patch = [
|
|
110
|
+
... {'op': 'replace', 'path': '/value', 'value': 2},
|
|
111
|
+
... {'op': 'test', 'path': '/value', 'value': 3},
|
|
112
|
+
... ]
|
|
113
|
+
>>> try:
|
|
114
|
+
... jsonpatch.apply_patch(original, patch)
|
|
115
|
+
... except jsonpatch.JsonPatchTestFailed:
|
|
116
|
+
... print('Test failed')
|
|
117
|
+
Test failed
|
|
118
|
+
>>> original
|
|
119
|
+
{'value': 1}
|
|
120
|
+
|
|
121
|
+
With ``in_place=True``, the same patch raises ``JsonPatchTestFailed``, but
|
|
122
|
+
the earlier replacement remains in the original document. Failed patches
|
|
123
|
+
do not roll back changes already made in place:
|
|
124
|
+
|
|
125
|
+
.. code-block:: python
|
|
126
|
+
|
|
127
|
+
>>> try:
|
|
128
|
+
... jsonpatch.apply_patch(original, patch, in_place=True)
|
|
129
|
+
... except jsonpatch.JsonPatchTestFailed:
|
|
130
|
+
... print('Test failed')
|
|
131
|
+
Test failed
|
|
132
|
+
>>> original
|
|
133
|
+
{'value': 2}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
Replacing the Whole Document
|
|
137
|
+
----------------------------
|
|
138
|
+
|
|
139
|
+
A document can be an object, array, string, number, boolean, or ``null``.
|
|
140
|
+
An empty ``path`` (``""``) selects the whole document, so a replacement can
|
|
141
|
+
also change its type:
|
|
142
|
+
|
|
143
|
+
.. code-block:: python
|
|
144
|
+
|
|
145
|
+
>>> import jsonpatch
|
|
146
|
+
>>> original = {'old': True}
|
|
147
|
+
>>> patch = jsonpatch.JsonPatch([
|
|
148
|
+
... {'op': 'replace', 'path': '', 'value': [1, 2]}
|
|
149
|
+
... ])
|
|
150
|
+
>>> result = patch.apply(original, in_place=True)
|
|
151
|
+
>>> result
|
|
152
|
+
[1, 2]
|
|
153
|
+
>>> original
|
|
154
|
+
{'old': True}
|
|
155
|
+
|
|
156
|
+
Always use the return value when an operation can replace the root, even
|
|
157
|
+
with ``in_place=True``. Replacing the root returns a new value; it cannot
|
|
158
|
+
rebind the caller's variable. Changes to members of the original container
|
|
159
|
+
can still happen in place.
|
|
160
|
+
|
|
161
|
+
The empty pointer differs from ``"/"``, which selects an object member
|
|
162
|
+
whose key is the empty string:
|
|
163
|
+
|
|
164
|
+
.. code-block:: python
|
|
165
|
+
|
|
166
|
+
>>> jsonpatch.apply_patch({'': 1}, [
|
|
167
|
+
... {'op': 'replace', 'path': '/', 'value': 2}
|
|
168
|
+
... ])
|
|
169
|
+
{'': 2}
|
|
170
|
+
>>> jsonpatch.apply_patch(None, [
|
|
171
|
+
... {'op': 'add', 'path': '', 'value': False}
|
|
172
|
+
... ])
|
|
173
|
+
False
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
Paths and Special Characters
|
|
177
|
+
----------------------------
|
|
178
|
+
|
|
179
|
+
The ``path`` and ``from`` members of an operation are JSON Pointers
|
|
180
|
+
(`RFC 6901 <https://datatracker.ietf.org/doc/html/rfc6901>`_). A pointer is a sequence of
|
|
181
|
+
reference tokens, each starting with ``/``, and every token selects one object
|
|
182
|
+
key or array index. Because ``/`` separates the tokens, a key that contains
|
|
183
|
+
``/`` or ``~`` has to be escaped: ``~`` is written as ``~0`` and ``/`` as
|
|
184
|
+
``~1``.
|
|
185
|
+
|
|
186
|
+
Patches created by ``make_patch`` follow this rule, so a key containing ``/``
|
|
187
|
+
shows up escaped in the generated path.
|
|
188
|
+
|
|
189
|
+
.. code-block:: python
|
|
190
|
+
|
|
191
|
+
>>> patch = jsonpatch.make_patch({}, {'/fields/test': '123456'})
|
|
192
|
+
>>> patch.patch
|
|
193
|
+
[{'op': 'add', 'path': '/~1fields~1test', 'value': '123456'}]
|
|
194
|
+
>>> patch.apply({})
|
|
195
|
+
{'/fields/test': '123456'}
|
|
196
|
+
|
|
197
|
+
The unescaped path ``/fields/test`` would mean something different: the key
|
|
198
|
+
``test`` inside the key ``fields``. Applying it to ``{}`` fails, because there
|
|
199
|
+
is no key ``fields``.
|
|
200
|
+
|
|
201
|
+
When writing a patch by hand, escape keys in the same way. The ``jsonpointer``
|
|
202
|
+
package, which ``jsonpatch`` depends on, can build a pointer from a list of
|
|
203
|
+
unescaped keys.
|
|
204
|
+
|
|
205
|
+
.. code-block:: python
|
|
206
|
+
|
|
207
|
+
>>> from jsonpointer import JsonPointer
|
|
208
|
+
>>> JsonPointer.from_parts(['/fields/test', 'a~b']).path
|
|
209
|
+
'/~1fields~1test/a~0b'
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
Dealing with Custom Types
|
|
213
|
+
-------------------------
|
|
214
|
+
|
|
215
|
+
Custom JSON dump and load functions can be used to support custom types such as
|
|
216
|
+
`decimal.Decimal`. The following examples shows how the
|
|
217
|
+
`simplejson <https://simplejson.readthedocs.io/>`_ package, which has native
|
|
218
|
+
support for Python's ``Decimal`` type, can be used to create a custom
|
|
219
|
+
``JsonPatch`` subclass with ``Decimal`` support:
|
|
220
|
+
|
|
221
|
+
.. code-block:: python
|
|
222
|
+
|
|
223
|
+
>>> import decimal
|
|
224
|
+
>>> import simplejson
|
|
225
|
+
|
|
226
|
+
>>> class DecimalJsonPatch(jsonpatch.JsonPatch):
|
|
227
|
+
... @staticmethod
|
|
228
|
+
... def json_dumper(obj):
|
|
229
|
+
... return simplejson.dumps(obj)
|
|
230
|
+
...
|
|
231
|
+
... @staticmethod
|
|
232
|
+
... def json_loader(obj):
|
|
233
|
+
... return simplejson.loads(obj, use_decimal=True,
|
|
234
|
+
... object_pairs_hook=jsonpatch.multidict)
|
|
235
|
+
|
|
236
|
+
>>> src = {}
|
|
237
|
+
>>> dst = {'bar': decimal.Decimal('1.10')}
|
|
238
|
+
>>> patch = DecimalJsonPatch.from_diff(src, dst)
|
|
239
|
+
>>> doc = {'foo': 1}
|
|
240
|
+
>>> patch.apply(doc)
|
|
241
|
+
{'foo': 1, 'bar': Decimal('1.10')}
|
|
242
|
+
|
|
243
|
+
Instead of subclassing it is also possible to pass a dump function to
|
|
244
|
+
``from_diff``:
|
|
245
|
+
|
|
246
|
+
.. code-block:: python
|
|
247
|
+
|
|
248
|
+
>>> patch = jsonpatch.JsonPatch.from_diff(src, dst, dumps=simplejson.dumps)
|
|
249
|
+
|
|
250
|
+
a dumps function to ``to_string``:
|
|
251
|
+
|
|
252
|
+
.. code-block:: python
|
|
253
|
+
|
|
254
|
+
>>> serialized_patch = patch.to_string(dumps=simplejson.dumps)
|
|
255
|
+
>>> serialized_patch
|
|
256
|
+
'[{"op": "add", "path": "/bar", "value": 1.10}]'
|
|
257
|
+
|
|
258
|
+
and load function to ``from_string``:
|
|
259
|
+
|
|
260
|
+
.. code-block:: python
|
|
261
|
+
|
|
262
|
+
>>> import functools
|
|
263
|
+
>>> loads = functools.partial(simplejson.loads, use_decimal=True,
|
|
264
|
+
... object_pairs_hook=jsonpatch.multidict)
|
|
265
|
+
>>> patch = jsonpatch.JsonPatch.from_string(serialized_patch, loads=loads)
|
|
266
|
+
>>> doc = {'foo': 1}
|
|
267
|
+
>>> patch.apply(doc)
|
|
268
|
+
{'foo': 1, 'bar': Decimal('1.10')}
|
|
@@ -55,7 +55,7 @@ _EXACT_MATCH_LIMIT = 500 ** 2
|
|
|
55
55
|
|
|
56
56
|
# Will be parsed by setup.py to determine package metadata
|
|
57
57
|
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
|
|
58
|
-
__version__ = '1.
|
|
58
|
+
__version__ = '1.39'
|
|
59
59
|
__website__ = 'https://github.com/stefankoegl/python-json-patch'
|
|
60
60
|
__license__ = 'BSD-3-Clause'
|
|
61
61
|
|
|
@@ -263,6 +263,8 @@ class RemoveOperation(PatchOperation):
|
|
|
263
263
|
except (KeyError, IndexError):
|
|
264
264
|
msg = "can't remove a non-existent object '{0}'".format(part)
|
|
265
265
|
raise JsonPatchConflict(msg)
|
|
266
|
+
except TypeError as ex:
|
|
267
|
+
raise JsonPatchConflict(str(ex))
|
|
266
268
|
|
|
267
269
|
return obj
|
|
268
270
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env python
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
|
|
4
|
+
import contextlib
|
|
4
5
|
import copy
|
|
6
|
+
import io
|
|
5
7
|
import json
|
|
6
8
|
import decimal
|
|
7
9
|
import doctest
|
|
@@ -10,6 +12,7 @@ import jsonpatch
|
|
|
10
12
|
import jsonpatch_cli
|
|
11
13
|
import jsonpointer
|
|
12
14
|
import random
|
|
15
|
+
import shlex
|
|
13
16
|
import sys
|
|
14
17
|
import os
|
|
15
18
|
import stat
|
|
@@ -1446,6 +1449,56 @@ class ConflictTests(unittest.TestCase):
|
|
|
1446
1449
|
patch_obj = [ { "op": "remove", "path": "/foo/non-existent"} ]
|
|
1447
1450
|
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)
|
|
1448
1451
|
|
|
1452
|
+
def test_remove_from_immutable_sequence(self):
|
|
1453
|
+
for src, path in [((1, 2), '/0'), ({'foo': (1, 2)}, '/foo/0')]:
|
|
1454
|
+
for in_place in (False, True):
|
|
1455
|
+
with self.subTest(src=src, in_place=in_place):
|
|
1456
|
+
original = copy.deepcopy(src)
|
|
1457
|
+
patch_obj = [{'op': 'remove', 'path': path}]
|
|
1458
|
+
with self.assertRaises(jsonpatch.JsonPatchConflict):
|
|
1459
|
+
jsonpatch.apply_patch(src, patch_obj, in_place=in_place)
|
|
1460
|
+
self.assertEqual(src, original)
|
|
1461
|
+
|
|
1462
|
+
def test_remove_from_read_only_mapping(self):
|
|
1463
|
+
for nested in (False, True):
|
|
1464
|
+
with self.subTest(nested=nested):
|
|
1465
|
+
data = {'foo': 1}
|
|
1466
|
+
src = MappingProxyType(data)
|
|
1467
|
+
path = '/foo'
|
|
1468
|
+
if nested:
|
|
1469
|
+
src = {'bar': src}
|
|
1470
|
+
path = '/bar/foo'
|
|
1471
|
+
patch_obj = [{'op': 'remove', 'path': path}]
|
|
1472
|
+
with self.assertRaises(jsonpatch.JsonPatchConflict):
|
|
1473
|
+
jsonpatch.apply_patch(src, patch_obj, in_place=True)
|
|
1474
|
+
self.assertEqual(data, {'foo': 1})
|
|
1475
|
+
|
|
1476
|
+
def test_move_from_immutable_sequence(self):
|
|
1477
|
+
for in_place in (False, True):
|
|
1478
|
+
with self.subTest(in_place=in_place):
|
|
1479
|
+
src = {'foo': (1, 2), 'bar': []}
|
|
1480
|
+
patch_obj = [{'op': 'move', 'from': '/foo/0', 'path': '/bar/-'}]
|
|
1481
|
+
with self.assertRaises(jsonpatch.JsonPatchConflict):
|
|
1482
|
+
jsonpatch.apply_patch(src, patch_obj, in_place=in_place)
|
|
1483
|
+
self.assertEqual(src, {'foo': (1, 2), 'bar': []})
|
|
1484
|
+
|
|
1485
|
+
def test_remove_from_duck_typed_mapping(self):
|
|
1486
|
+
class IndexedObject:
|
|
1487
|
+
def __init__(self):
|
|
1488
|
+
self.data = {'foo': 1, 'bar': 2}
|
|
1489
|
+
|
|
1490
|
+
def __getitem__(self, key):
|
|
1491
|
+
return self.data[key]
|
|
1492
|
+
|
|
1493
|
+
def __delitem__(self, key):
|
|
1494
|
+
del self.data[key]
|
|
1495
|
+
|
|
1496
|
+
src = IndexedObject()
|
|
1497
|
+
patch_obj = [{'op': 'remove', 'path': '/foo'}]
|
|
1498
|
+
result = jsonpatch.apply_patch(src, patch_obj, in_place=True)
|
|
1499
|
+
self.assertIs(result, src)
|
|
1500
|
+
self.assertEqual(src.data, {'bar': 2})
|
|
1501
|
+
|
|
1449
1502
|
def test_insert_oob(self):
|
|
1450
1503
|
src = {"foo": [1, 2]}
|
|
1451
1504
|
patch_obj = [ { "op": "add", "path": "/foo/10", "value": 1} ]
|
|
@@ -1930,7 +1983,31 @@ class CustomOperationTests(unittest.TestCase):
|
|
|
1930
1983
|
self.assertEqual(res, {})
|
|
1931
1984
|
|
|
1932
1985
|
|
|
1933
|
-
class
|
|
1986
|
+
class ScriptRunner:
|
|
1987
|
+
""" Runs the command line programs of this checkout """
|
|
1988
|
+
|
|
1989
|
+
def run_python(self, code, *args, stdin='', utf8=True, cwd=None):
|
|
1990
|
+
here = os.path.dirname(os.path.abspath(__file__))
|
|
1991
|
+
# the scripts have to import this jsonpatch, not an installed one
|
|
1992
|
+
pythonpath = [here] + [p for p in [os.environ.get('PYTHONPATH')] if p]
|
|
1993
|
+
env = dict(os.environ, PYTHONPATH=os.pathsep.join(pythonpath),
|
|
1994
|
+
PYTHONUTF8='1')
|
|
1995
|
+
if not utf8:
|
|
1996
|
+
# Exercise locale-dependent file I/O while keeping pipes in UTF-8.
|
|
1997
|
+
env.update(PYTHONUTF8='0', PYTHONIOENCODING='utf-8',
|
|
1998
|
+
LC_ALL='C', PYTHONCOERCECLOCALE='0')
|
|
1999
|
+
return subprocess.run(
|
|
2000
|
+
[sys.executable, '-c', code] + list(args),
|
|
2001
|
+
input=stdin, capture_output=True, encoding='utf-8', env=env,
|
|
2002
|
+
cwd=cwd)
|
|
2003
|
+
|
|
2004
|
+
def run_script(self, script, *args, stdin='', utf8=True, cwd=None):
|
|
2005
|
+
return self.run_python(
|
|
2006
|
+
'import jsonpatch_cli; jsonpatch_cli.{0}_main()'.format(script),
|
|
2007
|
+
*args, stdin=stdin, utf8=utf8, cwd=cwd)
|
|
2008
|
+
|
|
2009
|
+
|
|
2010
|
+
class CommandLineTests(ScriptRunner, unittest.TestCase):
|
|
1934
2011
|
""" The jsondiff and jsonpatch command line programs """
|
|
1935
2012
|
|
|
1936
2013
|
def setUp(self):
|
|
@@ -1948,25 +2025,6 @@ class CommandLineTests(unittest.TestCase):
|
|
|
1948
2025
|
with open(path, encoding='utf-8') as f:
|
|
1949
2026
|
return json.load(f)
|
|
1950
2027
|
|
|
1951
|
-
def run_python(self, code, *args, stdin='', utf8=True):
|
|
1952
|
-
here = os.path.dirname(os.path.abspath(__file__))
|
|
1953
|
-
# the scripts have to import this jsonpatch, not an installed one
|
|
1954
|
-
pythonpath = [here] + [p for p in [os.environ.get('PYTHONPATH')] if p]
|
|
1955
|
-
env = dict(os.environ, PYTHONPATH=os.pathsep.join(pythonpath),
|
|
1956
|
-
PYTHONUTF8='1')
|
|
1957
|
-
if not utf8:
|
|
1958
|
-
# Exercise locale-dependent file I/O while keeping pipes in UTF-8.
|
|
1959
|
-
env.update(PYTHONUTF8='0', PYTHONIOENCODING='utf-8',
|
|
1960
|
-
LC_ALL='C', PYTHONCOERCECLOCALE='0')
|
|
1961
|
-
return subprocess.run(
|
|
1962
|
-
[sys.executable, '-c', code] + list(args),
|
|
1963
|
-
input=stdin, capture_output=True, encoding='utf-8', env=env)
|
|
1964
|
-
|
|
1965
|
-
def run_script(self, script, *args, stdin='', utf8=True):
|
|
1966
|
-
return self.run_python(
|
|
1967
|
-
'import jsonpatch_cli; jsonpatch_cli.{0}_main()'.format(script),
|
|
1968
|
-
*args, stdin=stdin, utf8=utf8)
|
|
1969
|
-
|
|
1970
2028
|
def test_jsondiff(self):
|
|
1971
2029
|
src = self.write('a.json', {'a': [1, 2], 'b': 0})
|
|
1972
2030
|
dst = self.write('b.json', {'a': [1, 2, 3], 'c': 100})
|
|
@@ -2261,6 +2319,92 @@ class CommandLineTests(unittest.TestCase):
|
|
|
2261
2319
|
self.assertEqual(res.stdout, '{0} {1}\n'.format(script, jsonpatch.__version__))
|
|
2262
2320
|
|
|
2263
2321
|
|
|
2322
|
+
class DocumentationTests(ScriptRunner, unittest.TestCase):
|
|
2323
|
+
""" The code examples in the documentation run and give the shown output """
|
|
2324
|
+
|
|
2325
|
+
doc_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'doc')
|
|
2326
|
+
|
|
2327
|
+
def doc_path(self, name):
|
|
2328
|
+
path = os.path.join(self.doc_dir, name)
|
|
2329
|
+
if not os.path.exists(path):
|
|
2330
|
+
self.skipTest('documentation not available')
|
|
2331
|
+
return path
|
|
2332
|
+
|
|
2333
|
+
def test_tutorial(self):
|
|
2334
|
+
path = self.doc_path('tutorial.rst')
|
|
2335
|
+
out = io.StringIO()
|
|
2336
|
+
with contextlib.redirect_stdout(out):
|
|
2337
|
+
failed, attempted = doctest.testfile(
|
|
2338
|
+
path, module_relative=False, optionflags=doctest.ELLIPSIS)
|
|
2339
|
+
self.assertGreater(attempted, 0)
|
|
2340
|
+
self.assertEqual(failed, 0, out.getvalue())
|
|
2341
|
+
|
|
2342
|
+
def shell_sessions(self, path):
|
|
2343
|
+
""" Yield (command, expected output) for each "$ " line of the
|
|
2344
|
+
shell examples in ``.. code-block:: bash`` blocks """
|
|
2345
|
+
with open(path, encoding='utf-8') as f:
|
|
2346
|
+
lines = f.read().splitlines()
|
|
2347
|
+
in_block = False
|
|
2348
|
+
command, output = None, []
|
|
2349
|
+
for line in lines + ['']:
|
|
2350
|
+
if line.startswith('.. code-block:: bash'):
|
|
2351
|
+
in_block = True
|
|
2352
|
+
continue
|
|
2353
|
+
if in_block and line and not line[0].isspace():
|
|
2354
|
+
in_block = False
|
|
2355
|
+
text = line.strip()
|
|
2356
|
+
if command is not None and (not in_block or not text or
|
|
2357
|
+
text.startswith(('$ ', '#'))):
|
|
2358
|
+
yield command, output
|
|
2359
|
+
command, output = None, []
|
|
2360
|
+
if not in_block:
|
|
2361
|
+
continue
|
|
2362
|
+
if text.startswith('$ '):
|
|
2363
|
+
command = text[2:]
|
|
2364
|
+
elif command is not None:
|
|
2365
|
+
# keep indentation relative to the code block
|
|
2366
|
+
output.append(line[4:])
|
|
2367
|
+
|
|
2368
|
+
def test_commandline(self):
|
|
2369
|
+
path = self.doc_path('commandline.rst')
|
|
2370
|
+
tmp = tempfile.TemporaryDirectory()
|
|
2371
|
+
self.addCleanup(tmp.cleanup)
|
|
2372
|
+
sessions = list(self.shell_sessions(path))
|
|
2373
|
+
self.assertGreater(len(sessions), 0)
|
|
2374
|
+
|
|
2375
|
+
for command, output in sessions:
|
|
2376
|
+
with self.subTest(command=command):
|
|
2377
|
+
args = shlex.split(command)
|
|
2378
|
+
expected = ''.join(line + '\n' for line in output)
|
|
2379
|
+
program, args = args[0], args[1:]
|
|
2380
|
+
|
|
2381
|
+
if program == 'cat':
|
|
2382
|
+
# "cat" introduces the input files of the examples
|
|
2383
|
+
filename = os.path.join(tmp.name, args[0])
|
|
2384
|
+
if not os.path.exists(filename):
|
|
2385
|
+
with open(filename, 'w', encoding='utf-8') as f:
|
|
2386
|
+
f.write(expected)
|
|
2387
|
+
with open(filename, encoding='utf-8') as f:
|
|
2388
|
+
self.assertEqual(f.read(), expected)
|
|
2389
|
+
continue
|
|
2390
|
+
|
|
2391
|
+
self.assertIn(program, ['jsondiff', 'jsonpatch'])
|
|
2392
|
+
redirect = None
|
|
2393
|
+
if '>' in args:
|
|
2394
|
+
redirect = args[args.index('>') + 1]
|
|
2395
|
+
args = args[:args.index('>')]
|
|
2396
|
+
|
|
2397
|
+
res = self.run_script(program, *args, cwd=tmp.name)
|
|
2398
|
+
self.assertEqual(res.stderr, "")
|
|
2399
|
+
|
|
2400
|
+
if redirect:
|
|
2401
|
+
with open(os.path.join(tmp.name, redirect), 'w',
|
|
2402
|
+
encoding='utf-8') as f:
|
|
2403
|
+
f.write(res.stdout)
|
|
2404
|
+
res.stdout = ''
|
|
2405
|
+
self.assertEqual(res.stdout, expected)
|
|
2406
|
+
|
|
2407
|
+
|
|
2264
2408
|
if __name__ == '__main__':
|
|
2265
2409
|
modules = ['jsonpatch']
|
|
2266
2410
|
|
|
@@ -2284,6 +2428,7 @@ if __name__ == '__main__':
|
|
|
2284
2428
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(CustomJsonPointerTests))
|
|
2285
2429
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(CustomOperationTests))
|
|
2286
2430
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(CommandLineTests))
|
|
2431
|
+
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(DocumentationTests))
|
|
2287
2432
|
return suite
|
|
2288
2433
|
|
|
2289
2434
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|