Skip to content

Commit 5b42ecf

Browse files
Merge pull request #95 from nextmv-io/merschformann/allow-control-over-json-serialization
Allows control over json serialization
2 parents bc8cd4e + 980c13e commit 5b42ecf

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

nextmv/nextmv/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "v0.26.1"
1+
__version__ = "v0.26.2.dev0"

nextmv/nextmv/output.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ class Output:
292292
"""Optional configuration for writing CSV files, to be used when the
293293
`output_format` is OutputFormat.CSV_ARCHIVE. These configurations are
294294
passed as kwargs to the `DictWriter` class from the `csv` module."""
295+
json_configurations: Optional[dict[str, Any]] = None
296+
"""Optional configuration for writing JSON files, to be used when the
297+
`output_format` is OutputFormat.JSON. These configurations are passed as
298+
kwargs to the `json.dumps` function."""
295299
assets: Optional[list[Asset]] = None
296300
"""Optional list of assets to be included in the output."""
297301

@@ -342,6 +346,8 @@ def to_dict(self) -> dict[str, any]:
342346

343347
if self.output_format == OutputFormat.CSV_ARCHIVE:
344348
output_dict["csv_configurations"] = self.csv_configurations
349+
elif self.output_format == OutputFormat.JSON:
350+
output_dict["json_configurations"] = self.json_configurations
345351

346352
return output_dict
347353

@@ -383,10 +389,23 @@ def _write_json(
383389
"assets": assets,
384390
}
385391

392+
json_configurations = {}
393+
if hasattr(output, "json_configurations") and output.json_configurations is not None:
394+
json_configurations = output.json_configurations
395+
396+
indent, custom_serial = 2, _custom_serial
397+
if "indent" in json_configurations:
398+
indent = json_configurations["indent"]
399+
del json_configurations["indent"]
400+
if "default" in json_configurations:
401+
custom_serial = json_configurations["default"]
402+
del json_configurations["default"]
403+
386404
serialized = json.dumps(
387405
final_output,
388-
indent=2,
389-
default=_custom_serial,
406+
indent=indent,
407+
default=custom_serial,
408+
**json_configurations,
390409
)
391410

392411
if path is None or path == "":

nextmv/tests/test_output.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ def test_local_writer_json_stdout(self):
7373

7474
self.assertDictEqual(got, expected)
7575

76+
def test_local_writer_json_stdout_with_configurations(self):
77+
output = nextmv.Output(
78+
output_format=nextmv.OutputFormat.JSON,
79+
solution={"empanadas": "are_life"},
80+
statistics={"foo": "bar"},
81+
json_configurations={
82+
"indent": None,
83+
"separators": (",", ":"),
84+
"sort_keys": True,
85+
},
86+
)
87+
output_writer = nextmv.LocalOutputWriter()
88+
89+
with patch("sys.stdout", new=StringIO()) as mock_stdout:
90+
output_writer.write(output, skip_stdout_reset=True)
91+
92+
self.assertEqual(
93+
mock_stdout.getvalue(),
94+
'{"assets":[],"options":{},"solution":{"empanadas":"are_life"},"statistics":{"foo":"bar"}}\n',
95+
)
96+
7697
def test_local_writer_json_stdout_with_options(self):
7798
options = nextmv.Options()
7899
options.duration = 5

0 commit comments

Comments
 (0)