summaryrefslogtreecommitdiff
path: root/jsonpatch.py
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2016-02-13 15:40:03 +0100
committerStefan Kögl <stefan@skoegl.net>2016-02-13 15:40:03 +0100
commita33021bf5a87350abc225a15c2a12880d88ed383 (patch)
treead78c654333e07f201447d45fa3c97f0c3a6e6d2 /jsonpatch.py
parent32dcbb03d8c6b9aedefff026fda75e5d8b63b8d2 (diff)
downloadpython-json-patch-a33021bf5a87350abc225a15c2a12880d88ed383.tar.gz
Optimize "deep" ``replace`` operation, fixes #36
Diffstat (limited to 'jsonpatch.py')
-rw-r--r--jsonpatch.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index fb2b90d..838d66c 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -756,11 +756,18 @@ def _optimize(operations):
def _optimize_using_replace(prev, cur):
- """Optimises JSON patch by using ``replace`` operation instead of
- ``remove`` and ``add`` against the same path."""
+ """Optimises by replacing ``add``/``remove`` with ``replace`` on same path
+
+ For nested strucures, tries to recurse replacement, see #36 """
prev['op'] = 'replace'
if cur['op'] == 'add':
- prev['value'] = cur['value']
+ # make recursive patch
+ patch = make_patch(prev['value'], cur['value'])
+ if len(patch.patch) == 1:
+ prev['path'] = prev['path'] + patch.patch[0]['path']
+ prev['value'] = patch.patch[0]['value']
+ else:
+ prev['value'] = cur['value']
def _optimize_using_move(prev_item, item):