stefankoegl / python-json-pointer

Resolve JSON Pointers in Python
https://python-json-pointer.readthedocs.org/
Other
140 stars 43 forks source link

jsonpointer.EndOfList class compain about indexing a list #17

Closed baihanliang closed 9 years ago

baihanliang commented 9 years ago

I am trying to index a list of dict object via resolve_pointer() and came across the following error. This happens with both version 1.1 and 1.7. The output is as below:

bliang@latite:~/lumberjack/python-json-pointer$ python Python 2.6.6 (r266:84292, Jun 18 2012, 09:57:52) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

from jsonpointer import resolve_pointer ranges = {'items': [{'start': 7, 'end': 7}, {'start': 23, 'end': 23}, {'start': 37, 'end': 37}, {'start': 107, 'end': 107}, {'start': 179, 'end': 179}, {'start': 513, 'end': 514}, {'start': 1494, 'end': 1494}, {'start': 1718, 'end': 1720}, {'start': 2000, 'end': 2003}, {'start': 2427, 'end': 2427}, {'start': 2598, 'end': 2598}, {'start': 2727, 'end': 2727}, {'start': 3389, 'end': 3389}, {'start': 5060, 'end': 5060}, {'start': 5631, 'end': 5631}, {'start': 5900, 'end': 5903}, {'start': 6000, 'end': 6000}], 'label': u'Interactive'} resolve_pointer(ranges, '/items/-') EndOfList([{'start': 7, 'end': 7}, {'start': 23, 'end': 23}, {'start': 37, 'end': 37}, {'start': 107, 'end': 107}, {'start': 179, 'end': 179}, {'start': 513, 'end': 514}, {'start': 1494, 'end': 1494}, {'start': 1718, 'end': 1720}, {'start': 2000, 'end': 2003}, {'start': 2427, 'end': 2427}, {'start': 2598, 'end': 2598}, {'start': 2727, 'end': 2727}, {'start': 3389, 'end': 3389}, {'start': 5060, 'end': 5060}, {'start': 5631, 'end': 5631}, {'start': 5900, 'end': 5903}, {'start': 6000, 'end': 6000}]) resolve_pointer(ranges, '/items/-/start') Traceback (most recent call last): File "", line 1, in File "jsonpointer.py", line 111, in resolve_pointer return pointer.resolve(doc, default) File "jsonpointer.py", line 168, in resolve doc = self.walk(doc, part) File "jsonpointer.py", line 224, in walk part = self.get_part(doc, part) File "jsonpointer.py", line 218, in get_part "must be dict/list or support getitem" % type(doc)) jsonpointer.JsonPointerException: Document '<class 'jsonpointer.EndOfList'>' does not support indexing, must be dict/list or support getitem

stefankoegl commented 9 years ago

I assume you'd expect a jsonpointer.JsonPointerException to be raised, right?

baihanliang commented 9 years ago

I was expecting to index "start" or "end" attribute via "-" array index, specifically the following call:

resolve_pointer(ranges, '/items/-/start')

Can it return something like the following based on the above input array? [7, 23, 37, 107, 179, 513, 1494, 1718, 2000, 2427, 2598, 2727, 3389, 5060, 5631, 5900, 6000]

We could index "start" or "end" attribute selectively using a fixed array index as following:

resolve_pointer(ranges.ranges, '/items/0/start') 7 resolve_pointer(ranges.ranges, '/items/1/start') 23

stefankoegl commented 9 years ago

From RFC 6901

  *  exactly the single character "-", making the new referenced
     value the (nonexistent) member after the last array element.

So when resolving - the result does (by definition) not exist, and therefore cannot be resolved any further.

baihanliang commented 9 years ago

Thanks for the clarification regarding "-".