fix: Sort keys for illegal JSON

This commit is contained in:
Gavin D'souza 2022-03-21 17:49:26 +05:30
parent 4f1d00442c
commit 1c8d2fd536

View file

@ -1524,11 +1524,13 @@ def get_value(*args, **kwargs):
def as_json(obj: Union[Dict, List], indent=1) -> str:
from frappe.utils.response import json_handler
if isinstance(obj, dict) and None in obj:
obj.pop(None)
return json.dumps(obj, indent=indent, sort_keys=True, default=json_handler, separators=(',', ': '))
try:
return json.dumps(obj, indent=indent, sort_keys=True, default=json_handler, separators=(',', ': '))
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=(',', ': '))
def are_emails_muted():
from frappe.utils import cint