deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

from_json_pickle doesn't return a DeepDiff object

Open gnu-lorien opened this issue 3 years ago • 8 comments

Describe the bug The documentation for from_json_pickle implies that it will return a DeepDiff object with similar values to the ones created by to_json_pickle. This is not what it does. It returns a dictionary form of the DeepDiff. I'm unable to find a way to construct an actual DeepDiff object from this dictionary.

To Reproduce

>>> t1 = {1: 1, 2: 2, 3: 3}
>>> t2 = {1: 1, 2: "2", 3: 3}
>>> ddiff = DeepDiff(t1, t2)
>>> jsoned = ddiff.to_json_pickle()
>>> jsoned
'{"type_changes": {"root[2]": {"new_type": {"py/type": "builtins.str"}, "new_value": "2", "old_type": {"py/type": "builtins.int"}, "old_value": 2}}}'
>>> ddiff_new = DeepDiff.from_json_pickle(jsoned)
>>> ddiff == ddiff_new
True
>>> type(ddiff) == type(ddiff_new)
False

Expected behavior The expectation is that the final lines of the doctest look like this:

>>> type(ddiff) == type(ddiff_new)
True

OS, DeepDiff version and Python version (please complete the following information):

  • OS: CentOS
  • Version 3.9

gnu-lorien avatar Feb 04 '22 18:02 gnu-lorien

@gnu-lorien Have you tried to use the json_pickle package to unpickle it? I will update the docs.

seperman avatar Feb 04 '22 20:02 seperman

The jsoned string doesn't have any class information in it, so I don't really know how to tell the jsonpickle to treat that dictionary as a DeepDiff, or to construct a DeepDiff from just one dictionary.

gnu-lorien avatar Feb 04 '22 22:02 gnu-lorien

Digging through the code it looks like I can do this with:

tmp = DeepDiff({}, {})
tmp.update(ddiff_new)

Based on the documentation something like this is probably what from_json_pickle should do.

gnu-lorien avatar Feb 04 '22 22:02 gnu-lorien

Turns out the method I expressed here doesn't actually work. Because t1 and t2 aren't set properly you can't actually use the class properly.

gnu-lorien avatar Apr 08 '22 18:04 gnu-lorien

Hey @gnu-lorien Sorry I didn't get back to you. So is this still a problem?

seperman avatar Apr 10 '22 00:04 seperman

Sort of. I wish there was a way to serialize the tree results so I could reload the DeepDiff object at a later time. Instead I've oriented the workflow out of the dictionary returned here, using Deltas, and then loading the previous base data to recreate the DeepDiff object.

gnu-lorien avatar Apr 12 '22 17:04 gnu-lorien

I see. So you want to serialize everything including your original objects in the diff object. Can I ask you what the use case is?

seperman avatar Apr 12 '22 17:04 seperman