diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c1d57c22be1d8c0167c645e363fcd949761d1fe..32688963eaab4db537bd163ff62627d52fbb7832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,4 @@ * deprecated `IteratorListAdapter` * fixed a bug that disabled json if ujson was not installed +* write_to_file_json has kwargs diff --git a/satella/__init__.py b/satella/__init__.py index 8258b3605a28d1d56942ed0efe413df889cb74b7..e797a16a71e111ab52155fe14952f9d2c5fe77ea 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.15.7a3' +__version__ = '2.15.7a4' diff --git a/satella/json.py b/satella/json.py index d29328a519af859615fa15580725fc97f13e5eb7..1a96cdcf59ae8fbef28d4dec3d784f8399160457 100644 --- a/satella/json.py +++ b/satella/json.py @@ -65,30 +65,32 @@ def json_encode(x: tp.Any) -> str: return JSONEncoder().encode(x) -def write_json_to_file(path: str, value: JSONAble) -> None: +def write_json_to_file(path: str, value: JSONAble, **kwargs) -> None: """ Write out a JSON to a file as UTF-8 encoded plain text. :param path: path to the file :param value: JSON-able content + :param kwargs: will be passed to ujson/json's dump """ if isinstance(value, JSONAble): value = value.to_json() with open(path, 'w') as f_out: try: import ujson - ujson.dump(value, f_out) + ujson.dump(value, f_out, **kwargs) except ImportError: - json.dump(value, f_out) + json.dump(value, f_out, **kwargs) -def write_json_to_file_if_different(path: str, value: JSONAble) -> bool: +def write_json_to_file_if_different(path: str, value: JSONAble, **kwargs) -> bool: """ Read JSON from a file. Write out a JSON to a file if it's value is different, as UTF-8 encoded plain text. :param path: path to the file :param value: JSON-able content + :param kwargs: will be passed to ujson/json dumps :return: whether the write actually happened """ if isinstance(value, JSONAble): @@ -96,11 +98,11 @@ def write_json_to_file_if_different(path: str, value: JSONAble) -> bool: try: val = read_json_from_file(path) if val != value: - write_json_to_file(path, value) + write_json_to_file(path, value, **kwargs) return True return False except (OSError, ValueError): - write_json_to_file(path, value) + write_json_to_file(path, value, **kwargs) return True