Skip to content

Commit

Permalink
fixing how we use to_json for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
seperman committed Mar 5, 2025
1 parent 4f34fe2 commit b4b29d5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ temp*

# env file
.env

pyrightconfig.json
5 changes: 1 addition & 4 deletions deepdiff/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ def diff(
sys.stdout.buffer.write(delta.dumps())
else:
try:
if orjson:
print(diff.to_json(option=orjson.OPT_INDENT_2))
else:
print(diff.to_json(indent=2))
print(diff.to_json(indent=2))
except Exception:
pprint(diff, indent=2)

Expand Down
27 changes: 20 additions & 7 deletions deepdiff/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def _save_content(content, path, file_type, keep_backup=True):
if file_type == 'json':
with open(path, 'w') as the_file:
content = json_dumps(content)
the_file.write(content)
the_file.write(content) # type: ignore
elif file_type in {'yaml', 'yml'}:
try:
import yaml
Expand All @@ -557,7 +557,7 @@ def _save_content(content, path, file_type, keep_backup=True):
content = pickle_dump(content, file_obj=the_file)
elif file_type in {'csv', 'tsv'}:
try:
import clevercsv
import clevercsv # type: ignore
dict_writer = clevercsv.DictWriter
except ImportError: # pragma: no cover.
import csv
Expand Down Expand Up @@ -642,7 +642,13 @@ def object_hook(self, obj):
return obj


def json_dumps(item, default_mapping=None, force_use_builtin_json: bool=False, **kwargs):
def json_dumps(
item,
default_mapping=None,
force_use_builtin_json: bool = False,
return_bytes: bool = False,
**kwargs,
) -> str | bytes:
"""
Dump json with extra details that are not normally json serializable
Expand All @@ -655,22 +661,29 @@ def json_dumps(item, default_mapping=None, force_use_builtin_json: bool=False, *
"""
if orjson and not force_use_builtin_json:
indent = kwargs.pop('indent', None)
kwargs['option'] = orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
if indent:
kwargs['option'] = orjson.OPT_INDENT_2
kwargs['option'] |= orjson.OPT_INDENT_2
if 'sort_keys' in kwargs:
raise TypeError(
"orjson does not accept the sort_keys parameter. "
"If you need to pass sort_keys, set force_use_builtin_json=True "
"to use Python's built-in json library instead of orjson.")
return orjson.dumps(
result = orjson.dumps(
item,
default=json_convertor_default(default_mapping=default_mapping),
**kwargs).decode(encoding='utf-8')
**kwargs)
if return_bytes:
return result
return result.decode(encoding='utf-8')
else:
return json.dumps(
result = json.dumps(
item,
default=json_convertor_default(default_mapping=default_mapping),
**kwargs)
if return_bytes:
return result.encode(encoding='utf-8')
return result


json_loads = partial(json.loads, cls=JSONDecoder)

0 comments on commit b4b29d5

Please sign in to comment.