diff --git a/jsonpointer.py b/jsonpointer.py index 2285ffc..fc26ad6 100644 --- a/jsonpointer.py +++ b/jsonpointer.py @@ -223,6 +223,9 @@ def get_part(cls, doc, part): if isinstance(doc, Mapping): return part + elif isinstance(doc, str): + raise JsonPointerException("Cannot apply token '%s' to non-container type %s" % (part, type(doc))) + elif isinstance(doc, Sequence): if part == '-': diff --git a/tests.py b/tests.py index 2cfd049..19bd621 100755 --- a/tests.py +++ b/tests.py @@ -219,6 +219,13 @@ def test_leading_zero(self): doc = [0, 1, 2] self.assertRaises(JsonPointerException, resolve_pointer, doc, '/01') + def test_string_not_indexable(self): + doc = {"foo": "should-not-be-indexable"} + self.assertRaises(JsonPointerException, resolve_pointer, doc, "/foo/0") + + ptr = JsonPointer("/foo/0") + self.assertRaises(JsonPointerException, ptr.resolve, doc) + class ToLastTests(unittest.TestCase):