Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ericremoreynolds committed Feb 25, 2016
2 parents cda557c + 0a51e7e commit 958effe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
14 changes: 7 additions & 7 deletions jsondiff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ def __init__(self, syntax='compact', load=False, dump=False, marshal=False, load
self.options.loader = loader
self.options.dumper = dumper
self._symbol_map = {
symbol.label: symbol
for symbol in (add, discard, insert, delete, update)
'$' + symbol.label: symbol
for symbol in _all_symbols_
}

def _list_diff_0(self, C, X, Y, i, j):
Expand Down Expand Up @@ -564,10 +564,10 @@ def unmarshal(self, d):
for k, v in d.items()
}
elif isinstance(d, (list, tuple)):
return [
self.marshal(x)
return type(d)(
self.unmarshal(x)
for x in d
]
)
else:
return self._unescape(d)

Expand All @@ -585,10 +585,10 @@ def marshal(self, d):
for k, v in d.items()
}
elif isinstance(d, (list, tuple)):
return [
return type(d)(
self.marshal(x)
for x in d
]
)
else:
return self._escape(d)

Expand Down
16 changes: 15 additions & 1 deletion jsondiff/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def __str__(self):
left = Symbol('left')
right = Symbol('right')

_all_symbols_ = [
missing,
identical,
delete,
insert,
update,
add,
discard,
replace,
left,
right
]

__all__ = [
'missing',
'identical',
Expand All @@ -30,5 +43,6 @@ def __str__(self):
'discard',
'replace',
'left',
'right'
'right',
'_all_symbols_'
]
21 changes: 21 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def test_a(self):
diff(['x', 'a', {'u': 10, 'v': 11}, 'c', 'x'], ['a', {'u': 10, 'v': 20}, 'b', 'c'])
)

def test_marshal(self):
differ = JsonDiffer()

d = {
delete: 3,
'$delete': 4,
insert: 4,
'$$something': 1
}

dm = differ.marshal(d)

self.assertEqual(d, differ.unmarshal(dm))

def generate_scenario(self, rng):
a = generate_random_json(rng, sets=True)
b = perturbate_json(a, rng, sets=True)
Expand All @@ -77,13 +91,18 @@ def test_compact_syntax(self, scenario):
differ = JsonDiffer(syntax='compact')
d = differ.diff(a, b)
self.assertEqual(b, differ.patch(a, d))
dm = differ.marshal(d)
self.assertEqual(d, differ.unmarshal(dm))


@randomize(1000, generate_scenario)
def test_explicit_syntax(self, scenario):
a, b = scenario
differ = JsonDiffer(syntax='explicit')
d = differ.diff(a, b)
# self.assertEqual(b, differ.patch(a, d))
dm = differ.marshal(d)
self.assertEqual(d, differ.unmarshal(dm))

@randomize(1000, generate_scenario)
def test_symmetric_syntax(self, scenario):
Expand All @@ -92,3 +111,5 @@ def test_symmetric_syntax(self, scenario):
d = differ.diff(a, b)
self.assertEqual(b, differ.patch(a, d))
self.assertEqual(a, differ.unpatch(b, d))
dm = differ.marshal(d)
self.assertEqual(d, differ.unmarshal(dm))

0 comments on commit 958effe

Please sign in to comment.