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