#16549: Add tests for json.tools.  Initial patch by Berker Peksag and Serhiy Storchaka.
diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index b2c786d..9ec3440 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -25,12 +25,14 @@
         outfile = open(sys.argv[2], 'wb')
     else:
         raise SystemExit(sys.argv[0] + " [infile [outfile]]")
-    try:
-        obj = json.load(infile)
-    except ValueError, e:
-        raise SystemExit(e)
-    json.dump(obj, outfile, sort_keys=True, indent=4)
-    outfile.write('\n')
+    with infile:
+        try:
+            obj = json.load(infile)
+        except ValueError, e:
+            raise SystemExit(e)
+    with outfile:
+        json.dump(obj, outfile, sort_keys=True, indent=4)
+        outfile.write('\n')
 
 
 if __name__ == '__main__':