Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed178b9

Browse files
committed
gh-59598: Ignore leading whitespace in JSONDecoder.raw_decode
Whitespace is allowed before JSON objects according to RFC 4627.
1 parent bfc57d4 commit ed178b9

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Lib/json/decoder.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,23 +341,27 @@ def decode(self, s, _w=WHITESPACE.match):
341341
containing a JSON document).
342342
343343
"""
344-
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
344+
obj, end = self.raw_decode(s)
345345
end = _w(s, end).end()
346346
if end != len(s):
347347
raise JSONDecodeError("Extra data", s, end)
348348
return obj
349349

350-
def raw_decode(self, s, idx=0):
350+
def raw_decode(self, s, idx=0, _w=WHITESPACE.match):
351351
"""Decode a JSON document from ``s`` (a ``str`` beginning with
352352
a JSON document) and return a 2-tuple of the Python
353353
representation and the index in ``s`` where the document ended.
354+
Whitespace at the beginning of the document will be ignored.
355+
356+
Optionally, ``idx`` can be used to specify an offset in ``s``
357+
where the document begins.
354358
355359
This can be used to decode a JSON document from a string that may
356360
have extraneous data at the end.
357361
358362
"""
359363
try:
360-
obj, end = self.scan_once(s, idx)
364+
obj, end = self.scan_once(s, idx=_w(s, idx).end())
361365
except StopIteration as err:
362366
raise JSONDecodeError("Expecting value", s, err.value) from None
363367
return obj, end

Lib/test/test_json/test_decode.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ def test_limit_int(self):
103103
with self.assertRaises(ValueError):
104104
self.loads('1' * (maxdigits + 1))
105105

106+
class TestRawDecode:
107+
def test_whitespace(self):
108+
decoder = self.json.JSONDecoder()
109+
self.assertEqual(decoder.raw_decode(' {}'), ({}, 3))
110+
self.assertEqual(decoder.raw_decode(' []'), ([], 4))
111+
self.assertEqual(decoder.raw_decode(' ""'), ('', 5))
112+
s = ' { "key" : "value" , "k":"v" } \n' \
113+
' { "key": "value", "k" :"v"} '
114+
val1, n1 = decoder.raw_decode(s)
115+
val2, n2 = decoder.raw_decode(s[n1:])
116+
self.assertEqual(val1, {"key":"value", "k":"v"})
117+
self.assertEqual(val2, {"key":"value", "k":"v"})
106118

107119
class TestPyDecode(TestDecode, PyTest): pass
108120
class TestCDecode(TestDecode, CTest): pass
121+
class TestPyRawDecode(TestRawDecode, PyTest): pass
122+
class TestCRawDecode(TestRawDecode, CTest): pass

0 commit comments

Comments
 (0)