Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1668)

Side by Side Diff: Lib/json/tool.py

Issue 7311: [issue4136] merge json library with simplejson 2.0.3 Base URL: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: Created 5 years, 6 months ago , Downloaded from: http://bugs.python.org/file11822/json_issue4136_r66961.diff
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 r"""Command-line tool to validate and pretty-print JSON 1 r"""Command-line tool to validate and pretty-print JSON
2 2
3 Usage:: 3 Usage::
4 4
5 $ echo '{"json":"obj"}' | python -mjson.tool 5 $ echo '{"json":"obj"}' | python -mjson.tool
6 { 6 {
7 "json": "obj" 7 "json": "obj"
8 } 8 }
9 $ echo '{ 1.2:3.4}' | python -mjson.tool 9 $ echo '{ 1.2:3.4}' | python -mjson.tool
10 Expecting property name: line 1 column 2 (char 2) 10 Expecting property name: line 1 column 2 (char 2)
11
12 """ 11 """
13 import sys 12 import sys
14 import json 13 import json
15 14
16 def main(): 15 def main():
17 if len(sys.argv) == 1: 16 if len(sys.argv) == 1:
18 infile = sys.stdin 17 infile = sys.stdin
19 outfile = sys.stdout 18 outfile = sys.stdout
20 elif len(sys.argv) == 2: 19 elif len(sys.argv) == 2:
21 infile = open(sys.argv[1], 'rb') 20 infile = open(sys.argv[1], 'rb')
22 outfile = sys.stdout 21 outfile = sys.stdout
23 elif len(sys.argv) == 3: 22 elif len(sys.argv) == 3:
24 infile = open(sys.argv[1], 'rb') 23 infile = open(sys.argv[1], 'rb')
25 outfile = open(sys.argv[2], 'wb') 24 outfile = open(sys.argv[2], 'wb')
26 else: 25 else:
27 raise SystemExit("{0} [infile [outfile]]".format(sys.argv[0])) 26 raise SystemExit("{0} [infile [outfile]]".format(sys.argv[0]))
28 try: 27 try:
29 obj = json.load(infile) 28 obj = json.load(infile)
30 except ValueError, e: 29 except ValueError, e:
31 raise SystemExit(e) 30 raise SystemExit(e)
32 json.dump(obj, outfile, sort_keys=True, indent=4) 31 json.dump(obj, outfile, sort_keys=True, indent=4)
33 outfile.write('\n') 32 outfile.write('\n')
34 33
35 34
36 if __name__ == '__main__': 35 if __name__ == '__main__':
37 main() 36 main()
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1278:e6ce13d99bf5