Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions deepdiff/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ def _deserializer(obj, safe_to_import=None):
for path, op_codes in result['_iterable_opcodes'].items():
_iterable_opcodes[path] = []
for op_code in op_codes:
_iterable_opcodes[path].append(
Opcode(
**op_code
)
)
# Serializers differ in how they encode the Opcode
# NamedTuple: orjson uses JSON_CONVERTOR and emits a
# mapping, but stdlib json (the fallback when orjson
# is not installed) encodes it as a positional array.
if isinstance(op_code, Mapping):
_iterable_opcodes[path].append(Opcode(**op_code))
else:
_iterable_opcodes[path].append(Opcode(*op_code))
result['_iterable_opcodes'] = _iterable_opcodes
return result

Expand Down
20 changes: 20 additions & 0 deletions tests/test_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2820,6 +2820,26 @@ def test_list_of_alphabet_and_its_delta(self):
assert l2 == l1 + delta5
assert l1 == l2 - delta5

def test_delta_iterable_opcodes_json_roundtrip_builtin_json(self):
# A list diff that goes through difflib opcodes (reorder / duplicates)
# serializes each Opcode NamedTuple. orjson emits it as a mapping, but
# the stdlib json fallback (used when orjson is not installed) emits it
# as a positional array; the deserializer must accept both. Forcing the
# builtin json path exercises the array shape even where orjson is
# installed, so a plain-install regression cannot hide behind orjson.
t1 = ['a', 'b', 'c', 'd', 'b', 'e']
t2 = ['b', 'c', 'x', 'b', 'e', 'f']
diff = DeepDiff(t1, t2)

def builtin_json_dumps(item):
return json_dumps(item, force_use_builtin_json=True)

dump = Delta(diff, bidirectional=True, serializer=builtin_json_dumps).dumps()
delta = Delta(dump, bidirectional=True, deserializer=json_loads)

assert t1 + delta == t2
assert t2 - delta == t1

def test_delta_flat_rows(self):
t1 = {"key1": "value1"}
t2 = {"field2": {"key2": "value2"}}
Expand Down