MCPcopy Create free account
hub / github.com/simplejson/simplejson / raw_decode

Method raw_decode

simplejson/decoder.py:402–426  ·  view source on GitHub ↗

Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning with a JSON document) and return a 2-tuple of the Python representation and the index in ``s`` where the document ended. Optionally, ``idx`` can be used to specify an offset in ``s`` where the JSON

(self, s, idx=0, _w=WHITESPACE.match, _PY3=PY3)

Source from the content-addressed store, hash-verified

400 return obj
401
402 def raw_decode(self, s, idx=0, _w=WHITESPACE.match, _PY3=PY3):
403 """Decode a JSON document from ``s`` (a ``str`` or ``unicode``
404 beginning with a JSON document) and return a 2-tuple of the Python
405 representation and the index in ``s`` where the document ended.
406 Optionally, ``idx`` can be used to specify an offset in ``s`` where
407 the JSON document begins.
408
409 This can be used to decode a JSON document from a string that may
410 have extraneous data at the end.
411
412 """
413 if idx < 0:
414 # Ensure that raw_decode bails on negative indexes, the regex
415 # would otherwise mask this behavior. #98
416 raise JSONDecodeError('Expecting value', s, idx)
417 if _PY3 and not isinstance(s, str):
418 raise TypeError("Input string must be text, not bytes")
419 # strip UTF-8 bom
420 if len(s) > idx:
421 ord0 = ord(s[idx])
422 if ord0 == 0xfeff:
423 idx += 1
424 elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
425 idx += 3
426 return self.scan_once(s, idx=_w(s, idx).end())

Callers 2

decodeMethod · 0.95
test_raw_decodeMethod · 0.80

Calls 1

JSONDecodeErrorClass · 0.85

Tested by 1

test_raw_decodeMethod · 0.64