diff options
author | Kenn Knowles <kenn@kennknowles.com> | 2016-12-17 15:38:13 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-17 15:38:13 -0800 |
commit | 7ec055619e6cafb3f98a82713c7198847c36a4cc (patch) | |
tree | 1942fec60e2466ca537e31081bc7f305d7375e3f | |
parent | 6b897384f5b7e9f476f7a694f26aca80d868be23 (diff) | |
parent | 02e0e5ac40146c3d0d359a955a926838f070371d (diff) | |
download | jsonpath-rw-7ec055619e6cafb3f98a82713c7198847c36a4cc.tar.gz |
Merge pull request #33 from abloomston/nullObjs
Return empty result instead of crashing on index into None
-rw-r--r-- | jsonpath_rw/jsonpath.py | 4 | ||||
-rw-r--r-- | tests/test_jsonpath.py | 3 |
2 files changed, 4 insertions, 3 deletions
diff --git a/jsonpath_rw/jsonpath.py b/jsonpath_rw/jsonpath.py index 93d7b81..146a960 100644 --- a/jsonpath_rw/jsonpath.py +++ b/jsonpath_rw/jsonpath.py @@ -477,7 +477,7 @@ class Index(JSONPath): JSONPath that matches indices of the current datum, or none if not large enough. Concrete syntax is brackets. - WARNING: If the datum is not long enough, it will not crash but will not match anything. + WARNING: If the datum is None or not long enough, it will not crash but will not match anything. NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]` """ @@ -487,7 +487,7 @@ class Index(JSONPath): def find(self, datum): datum = DatumInContext.wrap(datum) - if len(datum.value) > self.index: + if datum.value and len(datum.value) > self.index: return [DatumInContext(datum.value[self.index], path=self, context=datum)] else: return [] diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index 2c01992..33c0ea6 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -130,7 +130,8 @@ class TestJsonPath(unittest.TestCase): self.check_cases([ ('[0]', [42], [42]), ('[5]', [42], []), - ('[2]', [34, 65, 29, 59], [29]) + ('[2]', [34, 65, 29, 59], [29]), + ('[0]', None, []) ]) def test_slice_value(self): |