Skip to content

Commit 91f6124

Browse files
authored
Merge pull request #106 from blakehilliard/only-type-diff
Fix issue not recognizing that, in JSON, 1 != 1.0 != True
2 parents b3726f3 + c1fce71 commit 91f6124

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

jsonpatch.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,17 +817,24 @@ def _compare_lists(self, path, src, dst):
817817
self._item_added(path, key, dst[key])
818818

819819
def _compare_values(self, path, key, src, dst):
820-
if src == dst:
821-
return
822-
823-
elif isinstance(src, MutableMapping) and \
820+
if isinstance(src, MutableMapping) and \
824821
isinstance(dst, MutableMapping):
825822
self._compare_dicts(_path_join(path, key), src, dst)
826823

827824
elif isinstance(src, MutableSequence) and \
828825
isinstance(dst, MutableSequence):
829826
self._compare_lists(_path_join(path, key), src, dst)
830827

828+
# To ensure we catch changes to JSON, we can't rely on a simple
829+
# src == dst, or it would not recognize the difference between
830+
# 1 and True, among other things. Using json.dumps is the most
831+
# fool-proof way to ensure we catch type changes that matter to JSON
832+
# and ignore those that don't. The performance of this could be
833+
# improved by doing more direct type checks, but we'd need to be
834+
# careful to accept type changes that don't matter when JSONified.
835+
elif json.dumps(src) == json.dumps(dst):
836+
return
837+
831838
else:
832839
self._item_replaced(path, key, dst)
833840

tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,24 @@ def test_move_from_numeric_to_alpha_dict_key(self):
427427
res = jsonpatch.apply_patch(src, patch)
428428
self.assertEqual(res, dst)
429429

430+
def test_issue90(self):
431+
"""In JSON 1 is different from True even though in python 1 == True"""
432+
src = {'A': 1}
433+
dst = {'A': True}
434+
patch = jsonpatch.make_patch(src, dst)
435+
res = jsonpatch.apply_patch(src, patch)
436+
self.assertEqual(res, dst)
437+
self.assertIsInstance(res['A'], bool)
438+
439+
def test_issue103(self):
440+
"""In JSON 1 is different from 1.0 even though in python 1 == 1.0"""
441+
src = {'A': 1}
442+
dst = {'A': 1.0}
443+
patch = jsonpatch.make_patch(src, dst)
444+
res = jsonpatch.apply_patch(src, patch)
445+
self.assertEqual(res, dst)
446+
self.assertIsInstance(res['A'], float)
447+
430448

431449

432450
class OptimizationTests(unittest.TestCase):

0 commit comments

Comments
 (0)