[O] Make indent customizable

This commit is contained in:
Hykilpikonna
2021-11-22 00:12:22 -05:00
parent 248ce6c7c8
commit 794008b182
2 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -289,7 +289,7 @@ def download_users_execute(api: API, n: float, base_dir: str, rate_limit: int,
meta = {'downloaded': downloaded, 'done_set': done_set, meta = {'downloaded': downloaded, 'done_set': done_set,
'current_set': current_set, 'next_set': next_set, 'current_set': current_set, 'next_set': next_set,
'n': n, 'rate_limit': rate_limit} 'n': n, 'rate_limit': rate_limit}
f.write(json.dumps(meta)) f.write(json_stringify(meta, indent=None))
debug(f'Finished saving friends of {screen_name}') debug(f'Finished saving friends of {screen_name}')
debug(f'============= Total {len(downloaded)} saved =============') debug(f'============= Total {len(downloaded)} saved =============')
+4 -2
View File
@@ -3,6 +3,7 @@ import json
import os import os
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, date from datetime import datetime, date
from typing import Union
import json5 import json5
@@ -101,12 +102,13 @@ class EnhancedJSONEncoder(json.JSONEncoder):
return super().default(o) return super().default(o)
def json_stringify(obj) -> str: def json_stringify(obj, indent: Union[int, None] = 1) -> str:
""" """
Serialize json string with support for dataclasses and datetime and sets and with custom Serialize json string with support for dataclasses and datetime and sets and with custom
configuration. configuration.
:param obj: Objects :param obj: Objects
:param indent: Indent size or none
:return: Json strings :return: Json strings
""" """
return json.dumps(obj, indent=1, cls=EnhancedJSONEncoder, ensure_ascii=False) return json.dumps(obj, indent=indent, cls=EnhancedJSONEncoder, ensure_ascii=False)