diff --git a/jsonpatch.py b/jsonpatch.py index 8faad2b..1c93ac3 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -409,16 +409,15 @@ class MoveOperation(PatchOperation): """Moves an object property or an array element to new location.""" def apply(self, obj): - subobj, part = self.pointer.to_last(obj) + from_ptr = jsonpointer.JsonPointer(self.operation['from']) + subobj, part = from_ptr.to_last(obj) value = subobj[part] - to_ptr = jsonpointer.JsonPointer(self.operation['to']) - - if self.pointer.contains(to_ptr): + if from_ptr.contains(self.pointer): raise JsonPatchException('Cannot move values into its own children') - RemoveOperation({'op': 'remove', 'path': self.location}).apply(obj) - AddOperation({'op': 'add', 'path': self.operation['to'], 'value': value}).apply(obj) + RemoveOperation({'op': 'remove', 'path': self.operation['from']}).apply(obj) + AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj) class TestOperation(PatchOperation): @@ -447,6 +446,7 @@ class CopyOperation(PatchOperation): """ Copies an object property or an array element to a new location """ def apply(self, obj): - subobj, part = self.pointer.to_last(obj) + from_ptr = jsonpointer.JsonPointer(self.operation['from']) + subobj, part = from_ptr.to_last(obj) value = copy.deepcopy(subobj[part]) - AddOperation({'op': 'add', 'path': self.operation['to'], 'value': value}).apply(obj) + AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj) diff --git a/test.py b/test.py new file mode 100644 index 0000000..e8c4c9e --- /dev/null +++ b/test.py @@ -0,0 +1,21 @@ + +import jsonpatch +import json + + +def run_test(test): + if 'comment' in test: + print test['comment'] + + if 'doc' in test and 'patch' in test: + try: + res = jsonpatch.apply_patch(test['doc'], test['patch']) + + + + + +def tests_from_obj(tests): + for test in tests: + run_test(test) +