summaryrefslogtreecommitdiff
path: root/jsonpatch.py
diff options
context:
space:
mode:
authorkostya <suffering.death@gmail.com>2017-03-08 13:18:16 +0200
committerkostya <suffering.death@gmail.com>2017-03-08 13:18:16 +0200
commitd1f317a2b796175154ca5e21c7b32eab1c5800fe (patch)
tree03d8772c9a8ca97bf885912e23230e0b30dcfcee /jsonpatch.py
parent27f1f987e0c9d101a8dc01cc322d5f31ca89d074 (diff)
downloadpython-json-patch-d1f317a2b796175154ca5e21c7b32eab1c5800fe.tar.gz
Fix: optimization bugs #55, #54, add more tests
Diffstat (limited to 'jsonpatch.py')
-rw-r--r--jsonpatch.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index a9574ae..1e2e3ac 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -770,7 +770,16 @@ def _optimize_using_replace(prev, cur):
if cur['op'] == 'add':
# make recursive patch
patch = make_patch(prev['value'], cur['value'])
- if len(patch.patch) == 1 and patch.patch[0]['op'] != 'remove':
+ # check case when dict "remove" is less than "add" and has a same key
+ if isinstance(prev['value'], dict) and isinstance(cur['value'], dict) and len(prev['value'].keys()) == 1:
+ prev_set = set(prev['value'].keys())
+ cur_set = set(cur['value'].keys())
+ if prev_set & cur_set == prev_set:
+ patch = make_patch(cur['value'], prev['value'])
+
+ if len(patch.patch) == 1 and \
+ patch.patch[0]['op'] != 'remove' and \
+ patch.patch[0]['path'] and patch.patch[0]['path'].split('/')[1] in prev['value']:
prev['path'] = prev['path'] + patch.patch[0]['path']
prev['value'] = patch.patch[0]['value']
else: