fix: Improve JSON export format readability (#19429)

* Improve JSON export format readability

* fix: Enable ensure_ascii flag on export_json

* style: fmt

[skip ci]

Co-authored-by: Alfredo Altamirano <Ahuahuachi@users.noreply.github.com>
Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
Alfredo Altamirano 2023-01-04 01:03:45 -06:00 committed by GitHub
parent b78452dc00
commit d0f880e5c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -1907,7 +1907,7 @@ def get_value(*args, **kwargs):
return db.get_value(*args, **kwargs)
def as_json(obj: dict | list, indent=1, separators=None) -> str:
def as_json(obj: dict | list, indent=1, separators=None, ensure_ascii=True) -> str:
from frappe.utils.response import json_handler
if separators is None:
@ -1915,13 +1915,24 @@ def as_json(obj: dict | list, indent=1, separators=None) -> str:
try:
return json.dumps(
obj, indent=indent, sort_keys=True, default=json_handler, separators=separators
obj,
indent=indent,
sort_keys=True,
default=json_handler,
separators=separators,
ensure_ascii=ensure_ascii,
)
except TypeError:
# this would break in case the keys are not all os "str" type - as defined in the JSON
# adding this to ensure keys are sorted (expected behaviour)
sorted_obj = dict(sorted(obj.items(), key=lambda kv: str(kv[0])))
return json.dumps(sorted_obj, indent=indent, default=json_handler, separators=separators)
return json.dumps(
sorted_obj,
indent=indent,
default=json_handler,
separators=separators,
ensure_ascii=ensure_ascii,
)
def are_emails_muted():

View file

@ -260,7 +260,7 @@ def export_json(doctype, path, filters=None, or_filters=None, name=None, order_b
path = os.path.join("..", path)
with open(path, "w") as outfile:
outfile.write(frappe.as_json(out))
outfile.write(frappe.as_json(out, ensure_ascii=False))
def export_csv(doctype, path):