Merge pull request #186 from pre-commit/attempt_to_fix_json_hook
Attempt to fix json hook
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 193b00e..fed900a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,5 +1,5 @@
- repo: https://github.com/pre-commit/pre-commit-hooks
- sha: 4c3cc8f3edc3c673b3a4abe2054e64f21d26772c
+ sha: dc50b7f09c9612624c97e7f11fa346937c37f444
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
diff --git a/pre_commit_hooks/check_json.py b/pre_commit_hooks/check_json.py
index e1578ff..b403f4b 100644
--- a/pre_commit_hooks/check_json.py
+++ b/pre_commit_hooks/check_json.py
@@ -1,10 +1,10 @@
from __future__ import print_function
import argparse
+import io
+import json
import sys
-import simplejson
-
def check_json(argv=None):
parser = argparse.ArgumentParser()
@@ -14,8 +14,8 @@
retval = 0
for filename in args.filenames:
try:
- simplejson.load(open(filename))
- except (simplejson.JSONDecodeError, UnicodeDecodeError) as exc:
+ json.load(io.open(filename, encoding='UTF-8'))
+ except (ValueError, UnicodeDecodeError) as exc:
print('{}: Failed to json decode ({})'.format(filename, exc))
retval = 1
return retval
diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py
index 76b8cfb..5e04230 100644
--- a/pre_commit_hooks/pretty_format_json.py
+++ b/pre_commit_hooks/pretty_format_json.py
@@ -27,9 +27,9 @@
)) + "\n" # dumps does not end with a newline
-def _autofix(filename, new_contents, encoding=None):
+def _autofix(filename, new_contents):
print("Fixing file {}".format(filename))
- with io.open(filename, 'w', encoding=encoding) as f:
+ with io.open(filename, 'w', encoding='UTF-8') as f:
f.write(new_contents)
@@ -100,7 +100,7 @@
status = 0
for json_file in args.filenames:
- with io.open(json_file, encoding='utf-8') as f:
+ with io.open(json_file, encoding='UTF-8') as f:
contents = f.read()
try:
@@ -113,10 +113,7 @@
print("File {} is not pretty-formatted".format(json_file))
if args.autofix:
- _autofix(
- json_file, pretty_contents,
- encoding='utf-8' if args.no_ensure_ascii else None,
- )
+ _autofix(json_file, pretty_contents)
status = 1