diff --git a/deepdiff/commands.py b/deepdiff/commands.py index 0543aa18..0f04a026 100644 --- a/deepdiff/commands.py +++ b/deepdiff/commands.py @@ -118,7 +118,8 @@ def diff( if kwargs["view"] in {'colored', 'colored_compact'}: print(diff) else: - print(diff.to_json(indent=2)) + json_options = {} if orjson else {'ensure_ascii': False} + print(diff.to_json(indent=2, **json_options)) except Exception: pprint(diff, indent=2) diff --git a/tests/test_command.py b/tests/test_command.py index bbf5ada3..5e0481c9 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -1,4 +1,5 @@ import os +import json import pytest from shutil import copyfile from click.testing import CliRunner @@ -10,6 +11,28 @@ @pytest.mark.skipif(pypy3, reason='clevercsv is not supported in pypy3') class TestCommands: + @pytest.mark.parametrize('use_orjson', [False, True]) + def test_diff_command_preserves_unicode(self, tmp_path, monkeypatch, use_orjson): + if use_orjson: + pytest.importorskip('orjson') + else: + monkeypatch.setattr('deepdiff.commands.orjson', None) + monkeypatch.setattr('deepdiff.serialization.orjson', None) + t1 = tmp_path / 'before.json' + t2 = tmp_path / 'after.json' + t1.write_text(json.dumps({'名前': '東京'}), encoding='utf-8') + t2.write_text(json.dumps({'名前': '大阪 😀'}), encoding='utf-8') + + result = CliRunner().invoke(diff, [str(t1), str(t2)]) + + assert result.exit_code == 0 + assert '東京' in result.output + assert '大阪 😀' in result.output + assert '名前' in result.output + assert json.loads(result.output) == { + 'values_changed': {"root['名前']": {'old_value': '東京', 'new_value': '大阪 😀'}} + } + @pytest.mark.parametrize('name1, name2, expected_in_stdout, expected_exit_code', [ ('t1.json', 't2.json', """dictionary_item_added": {\n "root[0]['key3']": "value3\"""", 0), ('t1_corrupt.json', 't2.json', "Error when loading t1:", 1),