Skip to content

Commit f5e5a38

Browse files
committed
Add starting_depth for reformat()
1 parent 67bad95 commit f5e5a38

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/fractured_json/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ def options(self, value: FracturedJsonOptions) -> None:
312312
prop = FormatterType.GetProperty("Options")
313313
prop.SetValue(self._dotnet_instance, value._dotnet_instance)
314314

315-
def reformat(self, json_text: str) -> str:
315+
def reformat(self, json_text: str, starting_depth: int = 0) -> str:
316316
"""Reformat a JSON string and return the formatted result."""
317317
if not isinstance(json_text, str):
318318
msg = "json_text must be a str"
319319
raise TypeError(msg)
320-
result = self._dotnet_instance.Reformat(String(json_text))
320+
result = self._dotnet_instance.Reformat(String(json_text), Int32(starting_depth))
321321
return str(result)
322322

323323
def minify(self, json_text: str) -> str:

src/fractured_json/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Formatter:
4141
def options(self) -> FracturedJsonOptions: ...
4242
@options.setter
4343
def options(self, value: FracturedJsonOptions) -> None: ...
44-
def reformat(self, json_text: str) -> str: ...
44+
def reformat(self, json_text: str, starting_depth: int) -> str: ...
4545
def minify(self, json_text: str) -> str: ...
4646
@property
4747
def string_length_func(self) -> Callable[[str], int]: ...

tests/test_formatter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def test_minify():
7373
assert test_output == ref_output
7474

7575

76+
def test_depth():
77+
json_input = Path("tests/data/test-bool.json").read_text()
78+
formatter = Formatter()
79+
test_output = formatter.reformat(json_input, 2)
80+
assert test_output == ' { "bools": {"true": true, "false": false} }\n'
81+
82+
7683
def test_exceptions():
7784
with pytest.raises(AttributeError, match="Unknown option 'non_existent_option'"):
7885
_ = FracturedJsonOptions(non_existent_option=True)

0 commit comments

Comments
 (0)