Skip to content

Commit d34222b

Browse files
authored
Merge pull request #571 from jack1142/patch-1
Update `pretty-format-json` to write to stdout with utf-8 encoding
2 parents 0c033f7 + 1de4fe6 commit d34222b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import json
3+
import sys
34
from difflib import unified_diff
45
from typing import List
56
from typing import Mapping
@@ -111,24 +112,22 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
111112
contents, args.indent, ensure_ascii=not args.no_ensure_ascii,
112113
sort_keys=not args.no_sort_keys, top_keys=args.top_keys,
113114
)
114-
115-
if contents != pretty_contents:
116-
if args.autofix:
117-
_autofix(json_file, pretty_contents)
118-
else:
119-
print(
120-
get_diff(contents, pretty_contents, json_file),
121-
end='',
122-
)
123-
124-
status = 1
125115
except ValueError:
126116
print(
127117
f'Input File {json_file} is not a valid JSON, consider using '
128118
f'check-json',
129119
)
130120
return 1
131121

122+
if contents != pretty_contents:
123+
if args.autofix:
124+
_autofix(json_file, pretty_contents)
125+
else:
126+
diff_output = get_diff(contents, pretty_contents, json_file)
127+
sys.stdout.buffer.write(diff_output.encode())
128+
129+
status = 1
130+
132131
return status
133132

134133

0 commit comments

Comments
 (0)