|
0
|
1 |
r"""Using simplejson from the shell to validate and |
|
|
2 |
pretty-print:: |
|
|
3 |
|
|
|
4 |
$ echo '{"json":"obj"}' | python -msimplejson.tool |
|
|
5 |
{ |
|
|
6 |
"json": "obj" |
|
|
7 |
} |
|
|
8 |
$ echo '{ 1.2:3.4}' | python -msimplejson.tool |
|
|
9 |
Expecting property name: line 1 column 2 (char 2) |
|
|
10 |
""" |
|
|
11 |
from django.utils import simplejson |
|
|
12 |
|
|
|
13 |
def main(): |
|
|
14 |
import sys |
|
|
15 |
if len(sys.argv) == 1: |
|
|
16 |
infile = sys.stdin |
|
|
17 |
outfile = sys.stdout |
|
|
18 |
elif len(sys.argv) == 2: |
|
|
19 |
infile = open(sys.argv[1], 'rb') |
|
|
20 |
outfile = sys.stdout |
|
|
21 |
elif len(sys.argv) == 3: |
|
|
22 |
infile = open(sys.argv[1], 'rb') |
|
|
23 |
outfile = open(sys.argv[2], 'wb') |
|
|
24 |
else: |
|
|
25 |
raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],)) |
|
|
26 |
try: |
|
|
27 |
obj = simplejson.load(infile) |
|
|
28 |
except ValueError, e: |
|
|
29 |
raise SystemExit(e) |
|
|
30 |
simplejson.dump(obj, outfile, sort_keys=True, indent=4) |
|
|
31 |
outfile.write('\n') |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
if __name__ == '__main__': |
|
|
35 |
main() |