mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Merge 96963f1e97 into 7099af8f5e
This commit is contained in:
commit
9082a159c9
4 changed files with 25 additions and 3 deletions
|
|
@ -342,23 +342,27 @@ def decode(self, s, _w=WHITESPACE.match):
|
|||
containing a JSON document).
|
||||
|
||||
"""
|
||||
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
|
||||
obj, end = self.raw_decode(s)
|
||||
end = _w(s, end).end()
|
||||
if end != len(s):
|
||||
raise JSONDecodeError("Extra data", s, end)
|
||||
return obj
|
||||
|
||||
def raw_decode(self, s, idx=0):
|
||||
def raw_decode(self, s, idx=0, _w=WHITESPACE.match):
|
||||
"""Decode a JSON document from ``s`` (a ``str`` beginning with
|
||||
a JSON document) and return a 2-tuple of the Python
|
||||
representation and the index in ``s`` where the document ended.
|
||||
Whitespace at the beginning of the document will be ignored.
|
||||
|
||||
Optionally, ``idx`` can be used to specify an offset in ``s``
|
||||
where the document begins.
|
||||
|
||||
This can be used to decode a JSON document from a string that may
|
||||
have extraneous data at the end.
|
||||
|
||||
"""
|
||||
try:
|
||||
obj, end = self.scan_once(s, idx)
|
||||
obj, end = self.scan_once(s, idx=_w(s, idx).end())
|
||||
except StopIteration as err:
|
||||
raise JSONDecodeError("Expecting value", s, err.value) from None
|
||||
return obj, end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue