]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Lib/json/tool.py
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 4/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Lib / json / tool.py
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Lib/json/tool.py b/AppPkg/Applications/Python/Python-2.7.10/Lib/json/tool.py
new file mode 100644 (file)
index 0000000..ea96f6b
--- /dev/null
@@ -0,0 +1,40 @@
+r"""Command-line tool to validate and pretty-print JSON\r
+\r
+Usage::\r
+\r
+    $ echo '{"json":"obj"}' | python -m json.tool\r
+    {\r
+        "json": "obj"\r
+    }\r
+    $ echo '{ 1.2:3.4}' | python -m json.tool\r
+    Expecting property name enclosed in double quotes: line 1 column 3 (char 2)\r
+\r
+"""\r
+import sys\r
+import json\r
+\r
+def main():\r
+    if len(sys.argv) == 1:\r
+        infile = sys.stdin\r
+        outfile = sys.stdout\r
+    elif len(sys.argv) == 2:\r
+        infile = open(sys.argv[1], 'rb')\r
+        outfile = sys.stdout\r
+    elif len(sys.argv) == 3:\r
+        infile = open(sys.argv[1], 'rb')\r
+        outfile = open(sys.argv[2], 'wb')\r
+    else:\r
+        raise SystemExit(sys.argv[0] + " [infile [outfile]]")\r
+    with infile:\r
+        try:\r
+            obj = json.load(infile)\r
+        except ValueError, e:\r
+            raise SystemExit(e)\r
+    with outfile:\r
+        json.dump(obj, outfile, sort_keys=True,\r
+                  indent=4, separators=(',', ': '))\r
+        outfile.write('\n')\r
+\r
+\r
+if __name__ == '__main__':\r
+    main()\r