|
| 1 | +# type: ignore |
| 2 | + |
1 | 3 | """A patched version of jsonpatch
|
2 | 4 |
|
3 | 5 | We need this because of: https://github.com/stefankoegl/python-json-patch/issues/138
|
|
9 | 11 | from jsonpatch import _ST_REMOVE
|
10 | 12 | from jsonpatch import DiffBuilder as _DiffBuilder
|
11 | 13 | from jsonpatch import JsonPatch as _JsonPatch
|
12 |
| -from jsonpatch import RemoveOperation, _path_join |
| 14 | +from jsonpatch import RemoveOperation, _path_join, basestring |
| 15 | +from jsonpointer import JsonPointer |
13 | 16 |
|
14 | 17 |
|
15 |
| -def apply_patch(doc, patch, in_place=False): |
16 |
| - if isinstance(patch, (str, bytes)): |
17 |
| - patch = JsonPatch.from_string(patch) |
| 18 | +def apply_patch(doc, patch, in_place=False, pointer_cls=JsonPointer): |
| 19 | + if isinstance(patch, basestring): |
| 20 | + patch = JsonPatch.from_string(patch, pointer_cls=pointer_cls) |
18 | 21 | else:
|
19 |
| - patch = JsonPatch(patch) |
| 22 | + patch = JsonPatch(patch, pointer_cls=pointer_cls) |
20 | 23 | return patch.apply(doc, in_place)
|
21 | 24 |
|
22 | 25 |
|
23 |
| -def make_patch(src, dst): |
24 |
| - return JsonPatch.from_diff(src, dst) |
| 26 | +def make_patch(src, dst, pointer_cls=JsonPointer): |
| 27 | + return JsonPatch.from_diff(src, dst, pointer_cls=pointer_cls) |
25 | 28 |
|
26 | 29 |
|
27 | 30 | class JsonPatch(_JsonPatch):
|
28 | 31 | @classmethod
|
29 |
| - def from_diff(cls, src, dst, optimization=True): |
30 |
| - builder = DiffBuilder() |
| 32 | + def from_diff( |
| 33 | + cls, |
| 34 | + src, |
| 35 | + dst, |
| 36 | + optimization=True, |
| 37 | + dumps=None, |
| 38 | + pointer_cls=JsonPointer, |
| 39 | + ): |
| 40 | + json_dumper = dumps or cls.json_dumper |
| 41 | + builder = DiffBuilder(src, dst, json_dumper, pointer_cls=pointer_cls) |
31 | 42 | builder._compare_values("", None, src, dst)
|
32 | 43 | ops = list(builder.execute())
|
33 |
| - return cls(ops) |
| 44 | + return cls(ops, pointer_cls=pointer_cls) |
34 | 45 |
|
35 | 46 |
|
36 | 47 | class DiffBuilder(_DiffBuilder):
|
|
0 commit comments