Mercurial > cpython-lru_cache-weakref
annotate Lib/json/tool.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 | fa277ab588de |
| children | ce583eb0bec2 |
| 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 r"""Command-line tool to validate and pretty-print JSON |
|
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 Usage:: |
|
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:
46685
diff
changeset
|
5 $ 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
|
6 { |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
7 "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
|
8 } |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46685
diff
changeset
|
9 $ echo '{ 1.2:3.4}' | python -m json.tool |
|
77842
7762816e3fcd
Issue #5067: improve some json error messages.
Antoine Pitrou <solipsis@pitrou.net>
parents:
52199
diff
changeset
|
10 Expecting property name enclosed in double quotes: line 1 column 2 (char 2) |
|
46685
7106ca871bfd
PEP 8 nits in json package
Benjamin Peterson <benjamin@python.org>
parents:
46676
diff
changeset
|
11 |
|
46676
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 import sys |
|
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 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 def main(): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
17 if len(sys.argv) == 1: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
18 infile = sys.stdin |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
19 outfile = sys.stdout |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 elif len(sys.argv) == 2: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 infile = open(sys.argv[1], 'rb') |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 outfile = sys.stdout |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
23 elif len(sys.argv) == 3: |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
24 infile = open(sys.argv[1], 'rb') |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 outfile = open(sys.argv[2], 'wb') |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 else: |
|
52199
d95e5add3ca4
merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents:
46685
diff
changeset
|
27 raise SystemExit(sys.argv[0] + " [infile [outfile]]") |
|
80635
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
28 with infile: |
|
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
29 try: |
|
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
30 obj = json.load(infile) |
|
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
31 except ValueError, e: |
|
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
32 raise SystemExit(e) |
|
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
33 with outfile: |
|
80639
fa277ab588de
#16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80635
diff
changeset
|
34 json.dump(obj, outfile, sort_keys=True, |
|
fa277ab588de
#16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
80635
diff
changeset
|
35 indent=4, separators=(',', ': ')) |
|
80635
08e5973fdc7e
#16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
77842
diff
changeset
|
36 outfile.write('\n') |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
37 |
|
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 if __name__ == '__main__': |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
40 main() |
