Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | """Implementation of JSONDecoder |
| 2 | """ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 3 | import binascii |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 4 | import re |
| 5 | import sys |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 6 | import struct |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 8 | from json.scanner import make_scanner |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 9 | try: |
| 10 | from _json import scanstring as c_scanstring |
| 11 | except ImportError: |
| 12 | c_scanstring = None |
| 13 | |
| 14 | __all__ = ['JSONDecoder'] |
| 15 | |
| 16 | FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL |
| 17 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 18 | def _floatconstants(): |
| 19 | _BYTES = binascii.unhexlify(b'7FF80000000000007FF0000000000000') |
| 20 | if sys.byteorder != 'big': |
| 21 | _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1] |
| 22 | nan, inf = struct.unpack('dd', _BYTES) |
| 23 | return nan, inf, -inf |
| 24 | |
| 25 | NaN, PosInf, NegInf = _floatconstants() |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def linecol(doc, pos): |
Benjamin Peterson | a13d475 | 2008-10-16 21:17:24 +0000 | [diff] [blame] | 29 | if isinstance(doc, bytes): |
| 30 | newline = b'\n' |
| 31 | else: |
| 32 | newline = '\n' |
| 33 | lineno = doc.count(newline, 0, pos) + 1 |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 34 | if lineno == 1: |
| 35 | colno = pos |
| 36 | else: |
Benjamin Peterson | a13d475 | 2008-10-16 21:17:24 +0000 | [diff] [blame] | 37 | colno = pos - doc.rindex(newline, 0, pos) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 38 | return lineno, colno |
| 39 | |
| 40 | |
| 41 | def errmsg(msg, doc, pos, end=None): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 42 | # Note that this function is called from _json |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 43 | lineno, colno = linecol(doc, pos) |
| 44 | if end is None: |
| 45 | fmt = '{0}: line {1} column {2} (char {3})' |
| 46 | return fmt.format(msg, lineno, colno, pos) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 47 | #fmt = '%s: line %d column %d (char %d)' |
| 48 | #return fmt % (msg, lineno, colno, pos) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 49 | endlineno, endcolno = linecol(doc, end) |
| 50 | fmt = '{0}: line {1} column {2} - line {3} column {4} (char {5} - {6})' |
| 51 | return fmt.format(msg, lineno, colno, endlineno, endcolno, pos, end) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 52 | #fmt = '%s: line %d column %d - line %d column %d (char %d - %d)' |
| 53 | #return fmt % (msg, lineno, colno, endlineno, endcolno, pos, end) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | _CONSTANTS = { |
| 57 | '-Infinity': NegInf, |
| 58 | 'Infinity': PosInf, |
| 59 | 'NaN': NaN, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 63 | STRINGCHUNK = re.compile(r'(.*?)(["\\\x00-\x1f])', FLAGS) |
| 64 | BACKSLASH = { |
| 65 | '"': '"', '\\': '\\', '/': '/', |
| 66 | 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t', |
| 67 | } |
| 68 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 69 | def py_scanstring(s, end, strict=True, |
| 70 | _b=BACKSLASH, _m=STRINGCHUNK.match): |
| 71 | """Scan the string s for a JSON string. End is the index of the |
| 72 | character in s after the quote that started the JSON string. |
| 73 | Unescapes all valid JSON string escape sequences and raises ValueError |
| 74 | on attempt to decode an invalid string. If strict is False then literal |
| 75 | control characters are allowed in the string. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 76 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 77 | Returns a tuple of the decoded string and the index of the character in s |
| 78 | after the end quote.""" |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 79 | chunks = [] |
| 80 | _append = chunks.append |
| 81 | begin = end - 1 |
| 82 | while 1: |
| 83 | chunk = _m(s, end) |
| 84 | if chunk is None: |
| 85 | raise ValueError( |
| 86 | errmsg("Unterminated string starting at", s, begin)) |
| 87 | end = chunk.end() |
| 88 | content, terminator = chunk.groups() |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 89 | # Content is contains zero or more unescaped string characters |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 90 | if content: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 91 | _append(content) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 92 | # Terminator is the end of string, a literal control character, |
| 93 | # or a backslash denoting that an escape sequence follows |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 94 | if terminator == '"': |
| 95 | break |
| 96 | elif terminator != '\\': |
| 97 | if strict: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 98 | #msg = "Invalid control character %r at" % (terminator,) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 99 | msg = "Invalid control character {0!r} at".format(terminator) |
| 100 | raise ValueError(errmsg(msg, s, end)) |
| 101 | else: |
| 102 | _append(terminator) |
| 103 | continue |
| 104 | try: |
| 105 | esc = s[end] |
| 106 | except IndexError: |
| 107 | raise ValueError( |
| 108 | errmsg("Unterminated string starting at", s, begin)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 109 | # If not a unicode escape sequence, must be in the lookup table |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 110 | if esc != 'u': |
| 111 | try: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 112 | char = _b[esc] |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 113 | except KeyError: |
| 114 | msg = "Invalid \\escape: {0!r}".format(esc) |
| 115 | raise ValueError(errmsg(msg, s, end)) |
| 116 | end += 1 |
| 117 | else: |
| 118 | esc = s[end + 1:end + 5] |
| 119 | next_end = end + 5 |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 120 | if len(esc) != 4: |
| 121 | msg = "Invalid \\uXXXX escape" |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 122 | raise ValueError(errmsg(msg, s, end)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 123 | uni = int(esc, 16) |
| 124 | # Check for surrogate pair on UCS-4 systems |
| 125 | if 0xd800 <= uni <= 0xdbff and sys.maxunicode > 65535: |
| 126 | msg = "Invalid \\uXXXX\\uXXXX surrogate pair" |
| 127 | if not s[end + 5:end + 7] == '\\u': |
| 128 | raise ValueError(errmsg(msg, s, end)) |
| 129 | esc2 = s[end + 7:end + 11] |
| 130 | if len(esc2) != 4: |
| 131 | raise ValueError(errmsg(msg, s, end)) |
| 132 | uni2 = int(esc2, 16) |
| 133 | uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00)) |
| 134 | next_end += 6 |
| 135 | char = chr(uni) |
| 136 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 137 | end = next_end |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 138 | _append(char) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 139 | return ''.join(chunks), end |
| 140 | |
| 141 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 142 | # Use speedup if available |
| 143 | scanstring = c_scanstring or py_scanstring |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 144 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 145 | WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS) |
| 146 | WHITESPACE_STR = ' \t\n\r' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 147 | |
| 148 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 149 | def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook, |
| 150 | _w=WHITESPACE.match, _ws=WHITESPACE_STR): |
| 151 | s, end = s_and_end |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 152 | pairs = [] |
| 153 | pairs_append = pairs.append |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 154 | # Use a slice to prevent IndexError from being raised, the following |
| 155 | # check will raise a more specific ValueError if the string is empty |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 156 | nextchar = s[end:end + 1] |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 157 | # Normally we expect nextchar == '"' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 158 | if nextchar != '"': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 159 | if nextchar in _ws: |
| 160 | end = _w(s, end).end() |
| 161 | nextchar = s[end:end + 1] |
| 162 | # Trivial empty object |
| 163 | if nextchar == '}': |
| 164 | return pairs, end + 1 |
| 165 | elif nextchar != '"': |
| 166 | raise ValueError(errmsg("Expecting property name", s, end)) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 167 | end += 1 |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 168 | while True: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 169 | key, end = scanstring(s, end, strict) |
| 170 | # To skip some function call overhead we optimize the fast paths where |
| 171 | # the JSON key separator is ": " or just ":". |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 172 | if s[end:end + 1] != ':': |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 173 | end = _w(s, end).end() |
| 174 | if s[end:end + 1] != ':': |
| 175 | raise ValueError(errmsg("Expecting : delimiter", s, end)) |
| 176 | end += 1 |
| 177 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 178 | try: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 179 | if s[end] in _ws: |
| 180 | end += 1 |
| 181 | if s[end] in _ws: |
| 182 | end = _w(s, end + 1).end() |
| 183 | except IndexError: |
| 184 | pass |
| 185 | |
| 186 | try: |
| 187 | value, end = scan_once(s, end) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 188 | except StopIteration: |
| 189 | raise ValueError(errmsg("Expecting object", s, end)) |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 190 | pairs_append((key, value)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 191 | try: |
| 192 | nextchar = s[end] |
| 193 | if nextchar in _ws: |
| 194 | end = _w(s, end + 1).end() |
| 195 | nextchar = s[end] |
| 196 | except IndexError: |
| 197 | nextchar = '' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 198 | end += 1 |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 199 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 200 | if nextchar == '}': |
| 201 | break |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 202 | elif nextchar != ',': |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 203 | raise ValueError(errmsg("Expecting , delimiter", s, end - 1)) |
| 204 | end = _w(s, end).end() |
| 205 | nextchar = s[end:end + 1] |
| 206 | end += 1 |
| 207 | if nextchar != '"': |
| 208 | raise ValueError(errmsg("Expecting property name", s, end - 1)) |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 209 | if object_pairs_hook is not None: |
| 210 | result = object_pairs_hook(pairs) |
| 211 | return result, end |
| 212 | pairs = dict(pairs) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 213 | if object_hook is not None: |
| 214 | pairs = object_hook(pairs) |
| 215 | return pairs, end |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 216 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 217 | def JSONArray(s_and_end, scan_once, context, _w=WHITESPACE.match): |
| 218 | s, end = s_and_end |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 219 | values = [] |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 220 | nextchar = s[end:end + 1] |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 221 | if nextchar in _ws: |
| 222 | end = _w(s, end + 1).end() |
| 223 | nextchar = s[end:end + 1] |
| 224 | # Look-ahead for trivial empty array |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 225 | if nextchar == ']': |
| 226 | return values, end + 1 |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 227 | _append = values.append |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 228 | while True: |
| 229 | try: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 230 | value, end = scan_once(s, end) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 231 | except StopIteration: |
| 232 | raise ValueError(errmsg("Expecting object", s, end)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 233 | _append(value) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 234 | nextchar = s[end:end + 1] |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 235 | if nextchar in _ws: |
| 236 | end = _w(s, end + 1).end() |
| 237 | nextchar = s[end:end + 1] |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 238 | end += 1 |
| 239 | if nextchar == ']': |
| 240 | break |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 241 | elif nextchar != ',': |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 242 | raise ValueError(errmsg("Expecting , delimiter", s, end)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 243 | try: |
| 244 | if s[end] in _ws: |
| 245 | end += 1 |
| 246 | if s[end] in _ws: |
| 247 | end = _w(s, end + 1).end() |
| 248 | except IndexError: |
| 249 | pass |
| 250 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 251 | return values, end |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | class JSONDecoder(object): |
| 255 | """Simple JSON <http://json.org> decoder |
| 256 | |
| 257 | Performs the following translations in decoding by default: |
| 258 | |
| 259 | +---------------+-------------------+ |
| 260 | | JSON | Python | |
| 261 | +===============+===================+ |
| 262 | | object | dict | |
| 263 | +---------------+-------------------+ |
| 264 | | array | list | |
| 265 | +---------------+-------------------+ |
| 266 | | string | unicode | |
| 267 | +---------------+-------------------+ |
| 268 | | number (int) | int, long | |
| 269 | +---------------+-------------------+ |
| 270 | | number (real) | float | |
| 271 | +---------------+-------------------+ |
| 272 | | true | True | |
| 273 | +---------------+-------------------+ |
| 274 | | false | False | |
| 275 | +---------------+-------------------+ |
| 276 | | null | None | |
| 277 | +---------------+-------------------+ |
| 278 | |
| 279 | It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as |
| 280 | their corresponding ``float`` values, which is outside the JSON spec. |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 281 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 282 | """ |
| 283 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 284 | def __init__(self, object_hook=None, parse_float=None, |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 285 | parse_int=None, parse_constant=None, strict=True, |
| 286 | object_pairs_hook=None): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 287 | """``object_hook``, if specified, will be called with the result |
| 288 | of every JSON object decoded and its return value will be used in |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 289 | place of the given ``dict``. This can be used to provide custom |
| 290 | deserializations (e.g. to support JSON-RPC class hinting). |
| 291 | |
| 292 | ``parse_float``, if specified, will be called with the string |
| 293 | of every JSON float to be decoded. By default this is equivalent to |
| 294 | float(num_str). This can be used to use another datatype or parser |
| 295 | for JSON floats (e.g. decimal.Decimal). |
| 296 | |
| 297 | ``parse_int``, if specified, will be called with the string |
| 298 | of every JSON int to be decoded. By default this is equivalent to |
| 299 | int(num_str). This can be used to use another datatype or parser |
| 300 | for JSON integers (e.g. float). |
| 301 | |
| 302 | ``parse_constant``, if specified, will be called with one of the |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 303 | following strings: -Infinity, Infinity, NaN. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 304 | This can be used to raise an exception if invalid JSON numbers |
| 305 | are encountered. |
| 306 | |
| 307 | """ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 308 | self.object_hook = object_hook |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 309 | self.parse_float = parse_float or float |
| 310 | self.parse_int = parse_int or int |
| 311 | self.parse_constant = parse_constant or _CONSTANTS.__getitem__ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 312 | self.strict = strict |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 313 | self.object_pairs_hook = object_pairs_hook |
| 314 | self.parse_object = JSONObject |
| 315 | self.parse_array = JSONArray |
| 316 | self.parse_string = scanstring |
| 317 | self.scan_once = make_scanner(self) |
| 318 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 319 | |
| 320 | def decode(self, s, _w=WHITESPACE.match): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 321 | """Return the Python representation of ``s`` (a ``str`` or ``unicode`` |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 322 | instance containing a JSON document) |
| 323 | |
| 324 | """ |
| 325 | obj, end = self.raw_decode(s, idx=_w(s, 0).end()) |
| 326 | end = _w(s, end).end() |
| 327 | if end != len(s): |
| 328 | raise ValueError(errmsg("Extra data", s, end, len(s))) |
| 329 | return obj |
| 330 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 331 | def raw_decode(self, s, idx=0): |
| 332 | """Decode a JSON document from ``s`` (a ``str`` or ``unicode`` |
| 333 | beginning with a JSON document) and return a 2-tuple of the Python |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 334 | representation and the index in ``s`` where the document ended. |
| 335 | |
| 336 | This can be used to decode a JSON document from a string that may |
| 337 | have extraneous data at the end. |
| 338 | |
| 339 | """ |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 340 | try: |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 341 | obj, end = self.scan_once(s, idx) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 342 | except StopIteration: |
| 343 | raise ValueError("No JSON object could be decoded") |
| 344 | return obj, end |