@@ -153,28 +153,53 @@ def to_last(self, doc):
153
153
if not self .parts :
154
154
return doc , None
155
155
156
- for part in self .parts [:- 1 ]:
157
- doc = self .walk (doc , part )
158
-
159
- return doc , self .get_part (doc , self .parts [- 1 ])
156
+ doc = self .resolve (doc , parts = self .parts [:- 1 ])
157
+ last = self .parts [- 1 ]
158
+ ptype = type (doc )
159
+ if ptype == dict :
160
+ pass
161
+ elif ptype == list or isinstance (doc , Sequence ):
162
+ if not RE_ARRAY_INDEX .match (str (last )):
163
+ raise JsonPointerException ("'%s' is not a valid list index" % (last , ))
164
+ last = int (last )
160
165
166
+ return doc , last
161
167
162
- def resolve (self , doc , default = _nothing ):
168
+ def resolve (self , doc , default = _nothing , parts = None ):
163
169
"""Resolves the pointer against doc and returns the referenced object"""
170
+ if parts is None :
171
+ parts = self .parts
164
172
165
- for part in self .parts :
166
-
167
- try :
168
- doc = self .walk (doc , part )
169
- except JsonPointerException :
170
- if default is _nothing :
171
- raise
173
+ try :
174
+ for part in parts :
175
+ ptype = type (doc )
176
+ if ptype == dict :
177
+ doc = doc [part ]
178
+ elif ptype == list or isinstance (doc , Sequence ):
179
+ if part == '-' :
180
+ doc = EndOfList (doc )
181
+ else :
182
+ if not RE_ARRAY_INDEX .match (str (part )):
183
+ raise JsonPointerException ("'%s' is not a valid list index" % (part , ))
184
+ doc = doc [int (part )]
172
185
else :
173
- return default
186
+ doc = doc [part ]
187
+ except KeyError :
188
+ if default is not _nothing :
189
+ return default
190
+ raise JsonPointerException ("member '%s' not found in %s" % (part , doc ))
191
+ except IndexError :
192
+ if default is not _nothing :
193
+ return default
194
+ raise JsonPointerException ("index '%s' is out of bounds" % (part , ))
195
+ except TypeError :
196
+ if default is not _nothing :
197
+ return default
198
+ raise JsonPointerException ("Document '%s' does not support indexing, "
199
+ "must be dict/list or support __getitem__" % type (doc ))
174
200
175
201
return doc
176
202
177
-
178
203
get = resolve
179
204
180
205
def set (self , doc , value , inplace = True ):
0 commit comments