1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
# -*- coding: utf-8 -*-
#
# python-json-patch - An implementation of the JSON Patch format
# https://github.com/stefankoegl/python-json-patch
#
# Copyright (c) 2011 Stefan Kögl <stefan@skoegl.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
"""Apply JSON-Patches according to
http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-04"""
# Will be parsed by setup.py to determine package metadata
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
__version__ = '0.5'
__website__ = 'https://github.com/stefankoegl/python-json-patch'
__license__ = 'Modified BSD License'
import copy
import sys
if sys.version_info < (2, 6):
import simplejson as json
else:
import json
if sys.version_info >= (3, 0):
basestring = (bytes, str)
class JsonPatchException(Exception):
"""Base Json Patch exception"""
class JsonPatchConflict(JsonPatchException):
"""Raised if patch could not be applied due to conflict situation such as:
- attempt to add object key then it already exists;
- attempt to operate with nonexistence object key;
- attempt to insert value to array at position beyond of it size;
- etc.
"""
class JsonPatchTestFailed(JsonPatchException, AssertionError):
""" A Test operation failed """
def apply_patch(doc, patch, in_place=False):
"""Apply list of patches to specified json document.
:param doc: Document object.
:type doc: dict
:param patch: JSON patch as list of dicts or raw JSON-encoded string.
:type patch: list or str
:param in_place: While :const:`True` patch will modify target document.
By default patch will be applied to document copy.
:type in_place: bool
:return: Patched document object.
:rtype: dict
>>> doc = {'foo': 'bar'}
>>> other = apply_patch(doc, [{'op': 'add', 'path': '/baz', 'value': 'qux'}])
>>> doc is not other
True
>>> other
{'foo': 'bar', 'baz': 'qux'}
>>> apply_patch(doc, [{'op': 'add', 'path': '/baz', 'value': 'qux'}], in_place=True)
{'foo': 'bar', 'baz': 'qux'}
>>> doc == other
True
"""
if isinstance(patch, basestring):
patch = JsonPatch.from_string(patch)
else:
patch = JsonPatch(patch)
return patch.apply(doc, in_place)
def make_patch(src, dst):
"""Generates patch by comparing of two document objects. Actually is
a proxy to :meth:`JsonPatch.from_diff` method.
:param src: Data source document object.
:type src: dict
:param dst: Data source document object.
:type dst: dict
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
>>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
>>> patch = make_patch(src, dst)
>>> new = patch.apply(src)
>>> new == dst
True
"""
return JsonPatch.from_diff(src, dst)
class JsonPatch(object):
"""A JSON Patch is a list of Patch Operations.
>>> patch = JsonPatch([
... {'op': 'add', 'path': '/foo', 'value': 'bar'},
... {'op': 'add', 'path': '/baz', 'value': [1, 2, 3]},
... {'op': 'remove', 'path': '/baz/1'},
... {'op': 'test', 'path': '/baz', 'value': [1, 3]},
... {'op': 'replace', 'path': '/baz/0', 'value': 42},
... {'op': 'remove', 'path': '/baz/1'},
... ])
>>> doc = {}
>>> result = patch.apply(doc)
>>> expected = {'foo': 'bar', 'baz': [42]}
>>> result == expected
True
JsonPatch object is iterable, so you could easily access to each patch
statement in loop:
>>> lpatch = list(patch)
>>> expected = {'op': 'add', 'path': '/foo', 'value': 'bar'}
>>> lpatch[0] == expected
True
>>> lpatch == patch.patch
True
Also JsonPatch could be converted directly to :class:`bool` if it contains
any operation statements:
>>> bool(patch)
True
>>> bool(JsonPatch([]))
False
This behavior is very handy with :func:`make_patch` to write more readable
code:
>>> old = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
>>> new = {'baz': 'qux', 'numbers': [1, 4, 7]}
>>> patch = make_patch(old, new)
>>> if patch:
... # document have changed, do something useful
... patch.apply(old) #doctest: +ELLIPSIS
{...}
"""
def __init__(self, patch):
self.patch = patch
self.operations = {
'remove': RemoveOperation,
'add': AddOperation,
'replace': ReplaceOperation,
'move': MoveOperation,
'test': TestOperation,
'copy': CopyOperation,
}
def __str__(self):
"""str(self) -> self.to_string()"""
return self.to_string()
def __bool__(self):
return bool(self.patch)
__nonzero__ = __bool__
def __iter__(self):
return iter(self.patch)
def __hash__(self):
return hash(frozenset(self.operations))
def __eq__(self, other):
if not isinstance(other, JsonPatch):
return False
return self.patch == other.patch
@classmethod
def from_string(cls, patch_str):
"""Creates JsonPatch instance from string source.
:param patch_str: JSON patch as raw string.
:type patch_str: str
:return: :class:`JsonPatch` instance.
"""
patch = json.loads(patch_str)
return cls(patch)
@classmethod
def from_diff(cls, src, dst):
"""Creates JsonPatch instance based on comparing of two document
objects. Json patch would be created for `src` argument against `dst`
one.
:param src: Data source document object.
:type src: dict
:param dst: Data source document object.
:type dst: dict
:return: :class:`JsonPatch` instance.
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
>>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
>>> patch = JsonPatch.from_diff(src, dst)
>>> new = patch.apply(src)
>>> new == dst
True
"""
def compare_values(path, value, other):
if value == other:
return
if isinstance(value, dict) and isinstance(other, dict):
for operation in compare_dict(path, value, other):
yield operation
elif isinstance(value, list) and isinstance(other, list):
for operation in compare_list(path, value, other):
yield operation
else:
yield {'op': 'replace', 'path': '/'.join(path), 'value': other}
def compare_dict(path, src, dst):
for key in src:
if key not in dst:
yield {'op': 'remove', 'path': '/'.join(path + [key])}
continue
current = path + [key]
for operation in compare_values(current, src[key], dst[key]):
yield operation
for key in dst:
if key not in src:
yield {'op': 'add', 'path': '/'.join(path + [key]), 'value': dst[key]}
def compare_list(path, src, dst):
lsrc, ldst = len(src), len(dst)
for idx in range(min(lsrc, ldst)):
current = path + [str(idx)]
for operation in compare_values(current, src[idx], dst[idx]):
yield operation
if lsrc < ldst:
for idx in range(lsrc, ldst):
current = path + [str(idx)]
yield {'op': 'add', 'path': '/'.join(current), 'value': dst[idx]}
elif lsrc > ldst:
for idx in reversed(range(ldst, lsrc)):
yield {'op': 'remove', 'path': '/'.join(path + [str(idx)])}
return cls(list(compare_dict([''], src, dst)))
def to_string(self):
"""Returns patch set as JSON string."""
return json.dumps(self.patch)
def apply(self, obj, in_place=False):
"""Applies the patch to given object.
:param obj: Document object.
:type obj: dict
:param in_place: Tweaks way how patch would be applied - directly to
specified `obj` or to his copy.
:type in_place: bool
:return: Modified `obj`.
"""
if not in_place:
obj = copy.deepcopy(obj)
for operation in self.patch:
operation = self._get_operation(operation)
operation.apply(obj)
return obj
def _get_operation(self, operation):
if 'op' not in operation:
raise JsonPatchException("Operation does not contain 'op' member")
raise Exception
op = operation['op']
if op not in self.operations:
raise JsonPatchException("Unknown operation '%s'" % op)
cls = self.operations[op]
return cls(operation)
class PatchOperation(object):
"""A single operation inside a JSON Patch."""
def __init__(self, operation):
self.location = operation['path']
self.operation = operation
def apply(self, obj):
"""Abstract method that applies patch operation to specified object."""
raise NotImplementedError('should implement patch operation.')
def locate(self, obj, location, last_must_exist=True):
"""Walks through the object according to location.
Returns the last step as (sub-object, last location-step)."""
parts = location.split('/')
if parts.pop(0) != '':
raise JsonPatchException('location must starts with /')
for part in parts[:-1]:
obj, _ = self._step(obj, part)
_, last_loc = self._step(obj, parts[-1], must_exist=last_must_exist)
return obj, last_loc
def _step(self, obj, loc_part, must_exist=True):
"""Goes one step in a locate() call."""
if isinstance(obj, dict):
part_variants = [loc_part]
for variant in part_variants:
if variant not in obj:
continue
return obj[variant], variant
elif isinstance(obj, list):
part_variants = [int(loc_part)]
for variant in part_variants:
if variant >= len(obj):
continue
return obj[variant], variant
else:
raise ValueError('list or dict expected, got %r' % type(obj))
if must_exist:
raise JsonPatchConflict('key %s not found' % loc_part)
else:
return obj, part_variants[0]
def __hash__(self):
return hash(frozenset(self.operation.items()))
def __eq__(self, other):
if not isinstance(other, PatchOperation):
return False
return self.operation == other.operation
class RemoveOperation(PatchOperation):
"""Removes an object property or an array element."""
def apply(self, obj):
subobj, part = self.locate(obj, self.location)
del subobj[part]
class AddOperation(PatchOperation):
"""Adds an object property or an array element."""
def apply(self, obj):
value = self.operation["value"]
subobj, part = self.locate(obj, self.location, last_must_exist=False)
if isinstance(subobj, list):
if part > len(subobj) or part < 0:
raise JsonPatchConflict("can't insert outside of list")
subobj.insert(part, value)
elif isinstance(subobj, dict):
if part in subobj:
raise JsonPatchConflict("object '%s' already exists" % part)
subobj[part] = value
else:
raise JsonPatchConflict("can't add to type '%s'"
"" % subobj.__class__.__name__)
class ReplaceOperation(PatchOperation):
"""Replaces an object property or an array element by new value."""
def apply(self, obj):
value = self.operation["value"]
subobj, part = self.locate(obj, self.location)
if isinstance(subobj, list):
if part > len(subobj) or part < 0:
raise JsonPatchConflict("can't replace outside of list")
elif isinstance(subobj, dict):
if not part in subobj:
raise JsonPatchConflict("can't replace non-existant object '%s'"
"" % part)
else:
raise JsonPatchConflict("can't replace in type '%s'"
"" % subobj.__class__.__name__)
subobj[part] = value
class MoveOperation(PatchOperation):
"""Moves an object property or an array element to new location."""
def apply(self, obj):
subobj, part = self.locate(obj, self.location)
value = subobj[part]
RemoveOperation({'op': 'remove', 'path': self.location}).apply(obj)
AddOperation({'op': 'add', 'path': self.operation['to'], 'value': value}).apply(obj)
class TestOperation(PatchOperation):
"""Test value by specified location."""
def apply(self, obj):
try:
subobj, part = self.locate(obj, self.location)
except JsonPatchConflict, c:
raise JsonPatchTestFailed(str(c))
val = subobj[part]
if 'value' in self.operation:
value = self.operation['value']
if val != value:
raise JsonPatchTestFailed('%s is not equal to tested value %s' % (val, value))
class CopyOperation(PatchOperation):
""" Copies an object property or an array element to a new location """
def apply(self, obj):
subobj, part = self.locate(obj, self.location)
value = copy.deepcopy(subobj[part])
AddOperation({'op': 'add', 'path': self.operation['to'], 'value': value}).apply(obj)
|