2009-01-06 13:13:02 +08:00
|
|
|
r"""Using simplejson from the shell to validate and
|
2008-07-28 07:38:28 +08:00
|
|
|
pretty-print::
|
2009-01-06 13:13:02 +08:00
|
|
|
|
|
|
|
$ echo '{"json":"obj"}' | python -msimplejson.tool
|
2008-07-28 07:38:28 +08:00
|
|
|
{
|
|
|
|
"json": "obj"
|
|
|
|
}
|
2009-01-06 13:13:02 +08:00
|
|
|
$ echo '{ 1.2:3.4}' | python -msimplejson.tool
|
2008-07-28 07:38:28 +08:00
|
|
|
Expecting property name: line 1 column 2 (char 2)
|
|
|
|
"""
|
2009-01-06 13:13:02 +08:00
|
|
|
from django.utils import simplejson
|
2008-07-28 07:38:28 +08:00
|
|
|
|
|
|
|
def main():
|
|
|
|
import sys
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
infile = sys.stdin
|
|
|
|
outfile = sys.stdout
|
|
|
|
elif len(sys.argv) == 2:
|
|
|
|
infile = open(sys.argv[1], 'rb')
|
|
|
|
outfile = sys.stdout
|
|
|
|
elif len(sys.argv) == 3:
|
|
|
|
infile = open(sys.argv[1], 'rb')
|
|
|
|
outfile = open(sys.argv[2], 'wb')
|
|
|
|
else:
|
|
|
|
raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],))
|
|
|
|
try:
|
|
|
|
obj = simplejson.load(infile)
|
|
|
|
except ValueError, e:
|
|
|
|
raise SystemExit(e)
|
|
|
|
simplejson.dump(obj, outfile, sort_keys=True, indent=4)
|
|
|
|
outfile.write('\n')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|