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