Mercurial > cpython-lru_cache-weakref
annotate Lib/json/__init__.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 78bad589f205 |
| children | ce583eb0bec2 |
| rev | line source |
|---|---|
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
1 r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
3 interchange format. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
4 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
5 :mod:`json` exposes an API familiar to users of the standard library |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
6 :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
7 version of the :mod:`json` library contained in Python 2.6, but maintains |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
8 compatibility with Python 2.4 and Python 2.5 and (currently) has |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
9 significant performance advantages, even without using the optional C |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
10 extension for speedups. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
12 Encoding basic Python object hierarchies:: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
13 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 >>> import json |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
15 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 '["foo", {"bar": ["baz", null, 1.0, 2]}]' |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
17 >>> print json.dumps("\"foo\bar") |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
18 "\"foo\bar" |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
19 >>> print json.dumps(u'\u1234') |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 "\u1234" |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 >>> print json.dumps('\\') |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 "\\" |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
23 >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
24 {"a": 0, "b": 0, "c": 0} |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 >>> from StringIO import StringIO |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 >>> io = StringIO() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
27 >>> json.dump(['streaming API'], io) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
28 >>> io.getvalue() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
29 '["streaming API"]' |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
30 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
31 Compact encoding:: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
32 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
33 >>> import json |
|
75124
04738f35e0ec
Let's sort the keys so that this test passes even with random hashes.
Barry Warsaw <barry@python.org>
parents:
46676
diff
changeset
|
34 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], sort_keys=True, separators=(',',':')) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
35 '[1,2,3,{"4":5,"6":7}]' |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
36 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
37 Pretty printing:: |
|
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 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
39 >>> import json |
|
80627
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
40 >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, |
|
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
41 ... indent=4, separators=(',', ': ')) |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
42 { |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
43 "4": 5, |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
44 "6": 7 |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
45 } |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
46 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
47 Decoding JSON:: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
48 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
49 >>> import json |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
50 >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
51 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
52 True |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
53 >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar' |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
54 True |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
55 >>> from StringIO import StringIO |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 >>> io = StringIO('["streaming API"]') |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
57 >>> json.load(io)[0] == 'streaming API' |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
58 True |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
59 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
60 Specializing JSON object decoding:: |
|
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 >>> import json |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
63 >>> def as_complex(dct): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
64 ... if '__complex__' in dct: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
65 ... return complex(dct['real'], dct['imag']) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
66 ... return dct |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
67 ... |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}', |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
69 ... object_hook=as_complex) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
70 (1+2j) |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
71 >>> from decimal import Decimal |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
72 >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1') |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
73 True |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
74 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
75 Specializing JSON object encoding:: |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
76 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
77 >>> import json |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
78 >>> def encode_complex(obj): |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
79 ... if isinstance(obj, complex): |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
80 ... return [obj.real, obj.imag] |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
81 ... raise TypeError(repr(o) + " is not JSON serializable") |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
82 ... |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
83 >>> json.dumps(2 + 1j, default=encode_complex) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
84 '[2.0, 1.0]' |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
85 >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
86 '[2.0, 1.0]' |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
87 >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j)) |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
88 '[2.0, 1.0]' |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
89 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
90 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
91 Using json.tool from the shell to validate and pretty-print:: |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
92 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
93 $ echo '{"json":"obj"}' | python -m json.tool |
|
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 { |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
95 "json": "obj" |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
96 } |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
97 $ echo '{ 1.2:3.4}' | python -m json.tool |
|
77842
7762816e3fcd
Issue #5067: improve some json error messages.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75125
diff
changeset
|
98 Expecting property name enclosed in double quotes: line 1 column 2 (char 2) |
|
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 """ |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
100 __version__ = '2.0.9' |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
101 __all__ = [ |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
102 'dump', 'dumps', 'load', 'loads', |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
103 'JSONDecoder', 'JSONEncoder', |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
104 ] |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
105 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
106 __author__ = 'Bob Ippolito <bob@redivi.com>' |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
107 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
108 from .decoder import JSONDecoder |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
109 from .encoder import JSONEncoder |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
110 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
111 _default_encoder = JSONEncoder( |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
112 skipkeys=False, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
113 ensure_ascii=True, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
114 check_circular=True, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
115 allow_nan=True, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
116 indent=None, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
117 separators=None, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
118 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
|
119 default=None, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
120 ) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
121 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
122 def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
123 allow_nan=True, cls=None, indent=None, separators=None, |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
124 encoding='utf-8', default=None, sort_keys=False, **kw): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
125 """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
126 ``.write()``-supporting file-like object). |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
127 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
128 If ``skipkeys`` is true then ``dict`` keys that are not basic types |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
129 (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
130 will be skipped instead of raising a ``TypeError``. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
131 |
|
78824
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
132 If ``ensure_ascii`` is true (the default), all non-ASCII characters in the |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
133 output are escaped with ``\uXXXX`` sequences, and the result is a ``str`` |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
134 instance consisting of ASCII characters only. If ``ensure_ascii`` is |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
135 ``False``, some chunks written to ``fp`` may be ``unicode`` instances. |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
136 This usually happens because the input contains unicode strings or the |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
137 ``encoding`` parameter is used. Unless ``fp.write()`` explicitly |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
138 understands ``unicode`` (as in ``codecs.getwriter``) this is likely to |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
139 cause an error. |
|
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 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
141 If ``check_circular`` is false, then the circular reference check |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
142 for container types will be skipped and a circular reference will |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
143 result in an ``OverflowError`` (or worse). |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
144 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
145 If ``allow_nan`` is false, then it will be a ``ValueError`` to |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
146 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
147 in strict compliance of the JSON specification, instead of using the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
148 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
149 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
150 If ``indent`` is a non-negative integer, then JSON array elements and |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
151 object members will be pretty-printed with that indent level. An indent |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
152 level of 0 will only insert newlines. ``None`` is the most compact |
|
80627
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
153 representation. Since the default item separator is ``', '``, the |
|
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
154 output might include trailing whitespace when ``indent`` is specified. |
|
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
155 You can use ``separators=(',', ': ')`` to avoid this. |
|
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 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
157 If ``separators`` is an ``(item_separator, dict_separator)`` tuple |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
158 then it will be used instead of the default ``(', ', ': ')`` separators. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
159 ``(',', ':')`` is the most compact JSON representation. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
160 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
161 ``encoding`` is the character encoding for str instances, default is 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
|
162 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
163 ``default(obj)`` is a function that should return a serializable version |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
164 of obj or raise TypeError. The default simply raises TypeError. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
165 |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
166 If *sort_keys* is ``True`` (default: ``False``), then the output of |
|
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
167 dictionaries will be sorted by key. |
|
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
168 |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
169 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
170 ``.default()`` method to serialize additional types), specify it with |
|
65375
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
171 the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
172 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
173 """ |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
174 # cached encoder |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
175 if (not skipkeys and ensure_ascii and |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
176 check_circular and allow_nan and |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
177 cls is None and indent is None and separators is None and |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
178 encoding == 'utf-8' and default is None and not sort_keys and not kw): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
179 iterable = _default_encoder.iterencode(obj) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
180 else: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
181 if cls 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
|
182 cls = JSONEncoder |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
183 iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
184 check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
185 separators=separators, encoding=encoding, |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
186 default=default, sort_keys=sort_keys, **kw).iterencode(obj) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
187 # could accelerate with writelines in some versions of Python, at |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
188 # a debuggability cost |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
189 for chunk in iterable: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
190 fp.write(chunk) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
191 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
192 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
193 def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
194 allow_nan=True, cls=None, indent=None, separators=None, |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
195 encoding='utf-8', default=None, sort_keys=False, **kw): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
196 """Serialize ``obj`` to a JSON formatted ``str``. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
197 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
198 If ``skipkeys`` is false then ``dict`` keys that are not basic types |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
199 (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
200 will be skipped instead of raising a ``TypeError``. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
201 |
|
78824
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
202 If ``ensure_ascii`` is false, all non-ASCII characters are not escaped, and |
|
a1884b3027c5
#13769: Enhance docs for ensure_ascii semantics in JSON decoding functions
Petri Lehtinen <petri@digip.org>
parents:
77842
diff
changeset
|
203 the return value may be a ``unicode`` instance. See ``dump`` for details. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
204 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
205 If ``check_circular`` is false, then the circular reference check |
|
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 for container types will be skipped and a circular reference will |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
207 result in an ``OverflowError`` (or worse). |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
208 |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
209 If ``allow_nan`` is false, then it will be a ``ValueError`` to |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
210 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
211 strict compliance of the JSON specification, instead of using the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
212 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
213 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
214 If ``indent`` is a non-negative integer, then JSON array elements and |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
215 object members will be pretty-printed with that indent level. An indent |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
216 level of 0 will only insert newlines. ``None`` is the most compact |
|
80627
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
217 representation. Since the default item separator is ``', '``, the |
|
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
218 output might include trailing whitespace when ``indent`` is specified. |
|
78bad589f205
#16333: document a way to get rid of trailing whitespace when indent is used.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80032
diff
changeset
|
219 You can use ``separators=(',', ': ')`` to avoid this. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
220 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
221 If ``separators`` is an ``(item_separator, dict_separator)`` tuple |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
222 then it will be used instead of the default ``(', ', ': ')`` separators. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
223 ``(',', ':')`` is the most compact JSON representation. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
224 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
225 ``encoding`` is the character encoding for str instances, default is 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
|
226 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
227 ``default(obj)`` is a function that should return a serializable version |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
228 of obj or raise TypeError. The default simply raises TypeError. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
229 |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
230 If *sort_keys* is ``True`` (default: ``False``), then the output of |
|
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
231 dictionaries will be sorted by key. |
|
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
232 |
|
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 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
234 ``.default()`` method to serialize additional types), specify it with |
|
65375
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
235 the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
236 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
237 """ |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
238 # cached encoder |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
239 if (not skipkeys and ensure_ascii and |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
240 check_circular and allow_nan and |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
241 cls is None and indent is None and separators is None and |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
242 encoding == 'utf-8' and default is None and not sort_keys and not kw): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
243 return _default_encoder.encode(obj) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
244 if cls 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
|
245 cls = JSONEncoder |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
246 return cls( |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
247 skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
248 check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
249 separators=separators, encoding=encoding, default=default, |
|
80032
7784008e9ade
Issue #14570: Document json sort_keys parameter properly.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
78824
diff
changeset
|
250 sort_keys=sort_keys, **kw).encode(obj) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
251 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
252 |
|
52225
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
253 _default_decoder = JSONDecoder(encoding=None, object_hook=None, |
|
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
254 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
|
255 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
256 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
257 def load(fp, encoding=None, cls=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
|
258 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
259 """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing |
|
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46676
diff
changeset
|
260 a JSON document) to a Python object. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
261 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
262 If the contents of ``fp`` is encoded with an ASCII based encoding other |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
263 than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
264 be specified. Encodings that are not ASCII based (such as UCS-2) are |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
265 not allowed, and should be wrapped with |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
266 ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode`` |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
267 object and passed to ``loads()`` |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
268 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
269 ``object_hook`` is an optional function that will be called with the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
270 result of any object literal decode (a ``dict``). The return value of |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
271 ``object_hook`` will be used instead of the ``dict``. This feature |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
272 can be used to implement custom decoders (e.g. 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
|
273 |
|
65375
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
274 ``object_pairs_hook`` is an optional function that will be called with the |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
275 result of any object literal decoded with an ordered list of pairs. The |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
276 return value of ``object_pairs_hook`` will be used instead of the ``dict``. |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
277 This feature can be used to implement custom decoders that rely on the |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
278 order that the key and value pairs are decoded (for example, |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
279 collections.OrderedDict will remember the order of insertion). If |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
280 ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority. |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
281 |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
282 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
|
65375
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
283 kwarg; otherwise ``JSONDecoder`` is used. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
284 |
|
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 return loads(fp.read(), |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
287 encoding=encoding, cls=cls, object_hook=object_hook, |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
288 parse_float=parse_float, parse_int=parse_int, |
|
52427
685f1791fbc5
Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents:
52225
diff
changeset
|
289 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, |
|
685f1791fbc5
Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents:
52225
diff
changeset
|
290 **kw) |
|
46676
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 def loads(s, encoding=None, cls=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
|
294 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
295 """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
296 document) to a Python object. |
|
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 If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
299 other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
300 must be specified. Encodings that are not ASCII based (such as UCS-2) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
301 are not allowed and should be decoded to ``unicode`` first. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
302 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
303 ``object_hook`` is an optional function that will be called with the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
304 result of any object literal decode (a ``dict``). The return value of |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
305 ``object_hook`` will be used instead of the ``dict``. This feature |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
306 can be used to implement custom decoders (e.g. 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
|
307 |
|
65375
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
308 ``object_pairs_hook`` is an optional function that will be called with the |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
309 result of any object literal decoded with an ordered list of pairs. The |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
310 return value of ``object_pairs_hook`` will be used instead of the ``dict``. |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
311 This feature can be used to implement custom decoders that rely on the |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
312 order that the key and value pairs are decoded (for example, |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
313 collections.OrderedDict will remember the order of insertion). If |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
314 ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority. |
|
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
315 |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
316 ``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
|
317 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
|
318 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
|
319 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
|
320 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
321 ``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
|
322 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
|
323 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
|
324 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
|
325 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
326 ``parse_constant``, if specified, will be called with one of the |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
327 following strings: -Infinity, Infinity, NaN, null, true, false. |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
328 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
|
329 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
|
330 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
331 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
|
65375
9347b0a85caf
Recorded merge of revisions 85543 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52427
diff
changeset
|
332 kwarg; otherwise ``JSONDecoder`` is used. |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
333 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
334 """ |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
335 if (cls is None and encoding is None and object_hook is None and |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
336 parse_int is None and parse_float is None and |
|
52225
a11a676fe6c0
Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents:
52199
diff
changeset
|
337 parse_constant is None and object_pairs_hook is None and not kw): |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
338 return _default_decoder.decode(s) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
339 if cls 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
|
340 cls = JSONDecoder |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
341 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
|
342 kw['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
|
343 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
|
344 kw['object_pairs_hook'] = object_pairs_hook |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
345 if parse_float 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
|
346 kw['parse_float'] = parse_float |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
347 if parse_int 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
|
348 kw['parse_int'] = parse_int |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
349 if parse_constant 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
|
350 kw['parse_constant'] = parse_constant |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
351 return cls(encoding=encoding, **kw).decode(s) |
