Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Lib/json/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,16 @@ def decode(self, s, _w=WHITESPACE.match):
containing a JSON document).

"""
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
end = _w(s, end).end()
# Skip the WHITESPACE.match() call (and its match-object allocation)
# for the common case where there is no leading whitespace.
idx = _w(s, 0).end() if s and s[0] in ' \t\n\r' else 0
obj, end = self.raw_decode(s, idx=idx)
# Likewise avoid the trailing-whitespace match when the parse already
# consumed the whole string.
if end != len(s):
raise JSONDecodeError("Extra data", s, end)
end = _w(s, end).end()
if end != len(s):
raise JSONDecodeError("Extra data", s, end)
return obj

def raw_decode(self, s, idx=0):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :func:`json.loads` for documents without leading or trailing
whitespace by skipping the whitespace scan in that common case. Patch by Bernát
Gábor.
Loading