summaryrefslogtreecommitdiff
path: root/jsonpatch.py
diff options
context:
space:
mode:
authorAlexander Shorin <kxepal@gmail.com>2011-12-25 16:56:06 +0400
committerAlexander Shorin <kxepal@gmail.com>2011-12-25 16:56:06 +0400
commit6dd658934ac7c6d1eba7b726d73552c13e2ebc1d (patch)
tree79ff0ad9e2f9a31c689405bd951617f2bbd8d9d3 /jsonpatch.py
parentf3f3410ff91675215dfe531bf7596080885fe88d (diff)
downloadpython-json-patch-6dd658934ac7c6d1eba7b726d73552c13e2ebc1d.tar.gz
Do not hide any problems.
Diffstat (limited to 'jsonpatch.py')
-rw-r--r--jsonpatch.py29
1 files changed, 13 insertions, 16 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index be92fbe..14b2162 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -131,15 +131,20 @@ class PatchOperation(object):
def _step(self, obj, loc_part, must_exist=True):
""" Goes one step in a locate() call """
- # Its not clear if a location "1" should be considered as 1 or "1"
- # We prefer the integer-variant if possible
- part_variants = self._try_parse(loc_part) + [loc_part]
-
- for variant in part_variants:
- try:
+ 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
- except:
- continue
+ else:
+ raise ValueError('list or dict expected, got %r' % type(obj))
if must_exist:
raise JsonPatchConflict('key %s not found' % loc_part)
@@ -147,14 +152,6 @@ class PatchOperation(object):
return obj, part_variants[0]
- @staticmethod
- def _try_parse(val, cls=int):
- try:
- return [cls(val)]
- except:
- return []
-
-
class RemoveOperation(PatchOperation):
""" Removes an object property or an array element