Add a hook for checking parseable json. · precommit/pre-commit-hooks@243fe50 · GitHub
Skip to content

Commit 243fe50

Browse files
committed
Add a hook for checking parseable json.
1 parent 7ec4853 commit 243fe50

7 files changed

Lines changed: 57 additions & 4 deletions

File tree

hooks.yaml

Lines changed: 8 additions & 2 deletions

pre_commit_hooks/check_json.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from __future__ import print_function
2+
3+
import argparse
4+
import sys
5+
import simplejson
6+
7+
from pre_commit_hooks.util import entry
8+
9+
10+
@entry
11+
def check_json(argv):
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument('filenames', nargs='*', help='JSON filenames to check.')
14+
args = parser.parse_args(argv)
15+
16+
retval = 0
17+
for filename in args.filenames:
18+
try:
19+
simplejson.load(open(filename))
20+
except simplejson.JSONDecodeError as e:
21+
print('{0}: Failed to json encode ({1})'.format(filename, e))
22+
retval = 1
23+
return retval
24+
25+
26+
if __name__ == '__main__':
27+
sys.exit(check_json())

pre_commit_hooks/check_yaml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@entry
1111
def check_yaml(argv):
1212
parser = argparse.ArgumentParser()
13-
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
13+
parser.add_argument('filenames', nargs='*', help='Yaml filenames to check.')
1414
args = parser.parse_args(argv)
1515

1616
retval = 0

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
name='pre_commit_hooks',
77
description='Some out-of-the-box hooks for pre-commit.',
88
url='https://github.com/pre-commit/pre-commit-hooks',
9-
version='0.1.1',
9+
version='0.2.0',
1010

1111
author='Anthony Sottile',
1212
author_email='asottile@umich.edu',
@@ -32,6 +32,7 @@
3232
],
3333
entry_points={
3434
'console_scripts': [
35+
'check-json = pre_commit_hooks.check_json:check_json',
3536
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
3637
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
3738
'end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:end_of_file_fixer',

testing/resources/bad_json.notjson

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "world",
3+
}

testing/resources/ok_json.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "world"
3+
}

tests/check_json_test.py

Lines changed: 13 additions & 0 deletions

0 commit comments

Comments
 (0)