michael@0: r""" michael@0: Using simplejson from the shell to validate and michael@0: pretty-print:: michael@0: michael@0: $ echo '{"json":"obj"}' | python -msimplejson michael@0: { michael@0: "json": "obj" michael@0: } michael@0: $ echo '{ 1.2:3.4}' | python -msimplejson michael@0: Expecting property name: line 1 column 2 (char 2) michael@0: michael@0: Note that the JSON produced by this module's default settings michael@0: is a subset of YAML, so it may be used as a serializer for that as well. michael@0: """ michael@0: import simplejson michael@0: michael@0: # michael@0: # Pretty printer: michael@0: # curl http://mochikit.com/examples/ajax_tables/domains.json | python -msimplejson.tool michael@0: # michael@0: michael@0: def main(): michael@0: import sys michael@0: if len(sys.argv) == 1: michael@0: infile = sys.stdin michael@0: outfile = sys.stdout michael@0: elif len(sys.argv) == 2: michael@0: infile = open(sys.argv[1], 'rb') michael@0: outfile = sys.stdout michael@0: elif len(sys.argv) == 3: michael@0: infile = open(sys.argv[1], 'rb') michael@0: outfile = open(sys.argv[2], 'wb') michael@0: else: michael@0: raise SystemExit("%s [infile [outfile]]" % (sys.argv[0],)) michael@0: try: michael@0: obj = simplejson.load(infile) michael@0: except ValueError, e: michael@0: raise SystemExit(e) michael@0: simplejson.dump(obj, outfile, sort_keys=True, indent=4) michael@0: outfile.write('\n') michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: main()