Skip to content

Commit 8bcf0e7

Browse files
feat: pr feedback json kwargs
Signed-off-by: jaapschoutenalliander <[email protected]>
1 parent c588156 commit 8bcf0e7

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/power_grid_model_ds/_core/model/grids/base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,16 @@ def from_txt_file(cls, txt_file_path: Path):
439439
txt_lines = f.readlines()
440440
return TextSource(grid_class=cls).load_from_txt(*txt_lines)
441441

442-
def to_json(self, path: Path) -> Path:
443-
"""Serialize the grid to JSON format."""
444-
return _save_grid_to_json(grid=self, path=path)
442+
def to_json(self, path: Path, **kwargs) -> Path:
443+
"""Serialize the grid to JSON format.
444+
445+
Args:
446+
path: Destination file path to write JSON to.
447+
**kwargs: Additional keyword arguments forwarded to ``json.dump``
448+
Returns:
449+
Path: The path where the file was saved.
450+
"""
451+
return _save_grid_to_json(grid=self, path=path, **kwargs)
445452

446453
@classmethod
447454
def from_json(cls: Type[Self], path: Path) -> Self:

src/power_grid_model_ds/_core/utils/serialization.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import logging
1010
from pathlib import Path
11-
from typing import TYPE_CHECKING, Dict, Optional, Type, TypeVar
11+
from typing import TYPE_CHECKING, Dict, Type, TypeVar
1212

1313
from power_grid_model_ds._core.model.arrays.base.array import FancyArray
1414

@@ -52,14 +52,15 @@ def _restore_grid_values(grid, json_data: Dict) -> None:
5252
def _save_grid_to_json(
5353
grid,
5454
path: Path,
55-
indent: Optional[int] = None,
55+
**kwargs,
5656
) -> Path:
5757
"""Save a Grid object to JSON format using power-grid-model serialization with extensions support.
5858
5959
Args:
6060
grid: The Grid object to serialize
6161
path: The file path to save to
62-
indent: JSON indentation (None for compact, positive int for indentation)
62+
**kwargs: Keyword arguments forwarded to json.dump (for example, indent, sort_keys,
63+
ensure_ascii, etc.).
6364
Returns:
6465
Path: The path where the file was saved
6566
"""
@@ -87,7 +88,7 @@ def _save_grid_to_json(
8788

8889
# Write to file
8990
with open(path, "w", encoding="utf-8") as f:
90-
json.dump(serialized_data, f, indent=indent if indent and indent > 0 else None)
91+
json.dump(serialized_data, f, **kwargs)
9192

9293
return path
9394

0 commit comments

Comments
 (0)