Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pre_commit_hooks/pretty_format_json.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

import argparse
import json
import sys
from difflib import unified_diff
from typing import Mapping
from typing import Sequence

import simplejson as json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, don't want to pull in any more dependencies



def _get_pretty_format(
contents: str,
Expand All @@ -23,7 +24,7 @@ def pairs_first(pairs: Sequence[tuple[str, str]]) -> Mapping[str, str]:
after.sort()
return dict(before + after)
json_pretty = json.dumps(
json.loads(contents, object_pairs_hook=pairs_first),
json.loads(contents, object_pairs_hook=pairs_first, use_decimal=True),
indent=indent,
ensure_ascii=ensure_ascii,
)
Expand Down
3 changes: 3 additions & 0 deletions testing/resources/high_precision_numbers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": 4.4257052820783003
}
2 changes: 2 additions & 0 deletions tests/pretty_format_json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def test_parse_num_to_int():
('unsorted_pretty_formatted_json.json', 1),
('non_ascii_pretty_formatted_json.json', 1),
('pretty_formatted_json.json', 0),
# numbers with high precision should not be modified.
('high_precision_numbers.json', 0),
),
)
def test_main(filename, expected_retval):
Expand Down