From 1c8d2fd5369f76700130fd4c33d31bcaed0f779c Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Mon, 21 Mar 2022 17:49:26 +0530 Subject: [PATCH] fix: Sort keys for illegal JSON --- frappe/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index 60189a2565..df8e1fbfb1 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -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