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