Skip to content

Commit b4b29d5

Browse files
committed
fixing how we use to_json for commands
1 parent 4f34fe2 commit b4b29d5

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ temp*
6969

7070
# env file
7171
.env
72+
73+
pyrightconfig.json

deepdiff/commands.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ def diff(
112112
sys.stdout.buffer.write(delta.dumps())
113113
else:
114114
try:
115-
if orjson:
116-
print(diff.to_json(option=orjson.OPT_INDENT_2))
117-
else:
118-
print(diff.to_json(indent=2))
115+
print(diff.to_json(indent=2))
119116
except Exception:
120117
pprint(diff, indent=2)
121118

deepdiff/serialization.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def _save_content(content, path, file_type, keep_backup=True):
537537
if file_type == 'json':
538538
with open(path, 'w') as the_file:
539539
content = json_dumps(content)
540-
the_file.write(content)
540+
the_file.write(content) # type: ignore
541541
elif file_type in {'yaml', 'yml'}:
542542
try:
543543
import yaml
@@ -557,7 +557,7 @@ def _save_content(content, path, file_type, keep_backup=True):
557557
content = pickle_dump(content, file_obj=the_file)
558558
elif file_type in {'csv', 'tsv'}:
559559
try:
560-
import clevercsv
560+
import clevercsv # type: ignore
561561
dict_writer = clevercsv.DictWriter
562562
except ImportError: # pragma: no cover.
563563
import csv
@@ -642,7 +642,13 @@ def object_hook(self, obj):
642642
return obj
643643

644644

645-
def json_dumps(item, default_mapping=None, force_use_builtin_json: bool=False, **kwargs):
645+
def json_dumps(
646+
item,
647+
default_mapping=None,
648+
force_use_builtin_json: bool = False,
649+
return_bytes: bool = False,
650+
**kwargs,
651+
) -> str | bytes:
646652
"""
647653
Dump json with extra details that are not normally json serializable
648654
@@ -655,22 +661,29 @@ def json_dumps(item, default_mapping=None, force_use_builtin_json: bool=False, *
655661
"""
656662
if orjson and not force_use_builtin_json:
657663
indent = kwargs.pop('indent', None)
664+
kwargs['option'] = orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
658665
if indent:
659-
kwargs['option'] = orjson.OPT_INDENT_2
666+
kwargs['option'] |= orjson.OPT_INDENT_2
660667
if 'sort_keys' in kwargs:
661668
raise TypeError(
662669
"orjson does not accept the sort_keys parameter. "
663670
"If you need to pass sort_keys, set force_use_builtin_json=True "
664671
"to use Python's built-in json library instead of orjson.")
665-
return orjson.dumps(
672+
result = orjson.dumps(
666673
item,
667674
default=json_convertor_default(default_mapping=default_mapping),
668-
**kwargs).decode(encoding='utf-8')
675+
**kwargs)
676+
if return_bytes:
677+
return result
678+
return result.decode(encoding='utf-8')
669679
else:
670-
return json.dumps(
680+
result = json.dumps(
671681
item,
672682
default=json_convertor_default(default_mapping=default_mapping),
673683
**kwargs)
684+
if return_bytes:
685+
return result.encode(encoding='utf-8')
686+
return result
674687

675688

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

0 commit comments

Comments
 (0)