Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_dict() method chokes on standard json.dumps() kwargs #490

Open
droneshire opened this issue Sep 24, 2024 · 3 comments
Open

to_dict() method chokes on standard json.dumps() kwargs #490

droneshire opened this issue Sep 24, 2024 · 3 comments
Assignees

Comments

@droneshire
Copy link

droneshire commented Sep 24, 2024

Describe the bug
Exception is thrown when using the sort_keys=True kwarg when using the to_json() method of a deepdiff.Deepdiff() object.

The bug is in the serialization.py code if orjson is defined/used:

def json_dumps(item, default_mapping=None, **kwargs):
    """
    Dump json with extra details that are not normally json serializable
    """
    if orjson:
        indent = kwargs.pop('indent', None)
        sort_keys = kwargs.pop('sort_keys', None)
        if indent:
            kwargs['option'] = orjson.OPT_INDENT_2
        return orjson.dumps(
                item,
                default=json_convertor_default(default_mapping=default_mapping),
                **kwargs).decode(encoding='utf-8')
    else:
        return json.dumps(
            item,
            default=json_convertor_default(default_mapping=default_mapping),
            **kwargs)

The fix would seem to be to handle all of the supported kwargs and translate them to their respective orjson opt. I.e. add in the following lines:

        if sort_keys:
            kwargs['sort_keys'] = orjson.OPT_SORT_KEYS
        ... etc

To Reproduce

NOTE: Need the orjson import to be defined (I'm not sure what allows this to happen, but the bug is hidden behind the if orjson: conditional

Otherwise can just do the following:

diff = deepdiff.DeepDiff(dict1, dict2)
if diff:
    diff.to_json(indent=4, sort_keys=True)

Expected behavior

diff.to_json() should not choke on standard json.dumps() kwargs (e.g. sort_keys)

OS, DeepDiff version and Python version (please complete the following information):

  • OS: Ubuntu
  • Version: Noble
  • Python Version: 3.11
  • DeepDiff Version deepdiff==7.0.1
@droneshire
Copy link
Author

My hack workaround in meantime:

diff_json = diff.to_json()
diff_json_sorted = json.dumps(json.loads(diff_json), indent=4, sort_keys=True)

@seperman seperman self-assigned this Dec 15, 2024
@seperman
Copy link
Owner

Hi @droneshire
Thanks for reporting the issue. Sorry it took me a while to look at it.
I don't think Orjson is designed to have exactly the same interface as Python's built-in Json library. It seems the solution is to let the user still use the standard json library if they have to use parameters such as sort_keys.

@seperman
Copy link
Owner

I'm adding a force_use_builtin_json flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants