|
1 | 1 | #!/usr/bin/env python |
2 | | -# -*- coding: utf-8 -*- |
3 | 2 |
|
4 | 3 | # In order to run the docstrings: |
5 | 4 | # python3 -m deepdiff.diff |
|
10 | 9 | import difflib |
11 | 10 | import logging |
12 | 11 | import json |
13 | | -import jsonpickle |
14 | 12 | import warnings |
15 | 13 |
|
16 | 14 | from itertools import zip_longest |
|
27 | 25 | from deepdiff.model import RemapDict, ResultDict, TextResult, TreeResult, DiffLevel |
28 | 26 | from deepdiff.model import DictRelationship, AttributeRelationship |
29 | 27 | from deepdiff.model import SubscriptableIterableRelationship, NonSubscriptableIterableRelationship, SetRelationship |
30 | | -from deepdiff.deephash import DeepHash |
| 28 | +from deepdiff.deephash import DeepHash, BoolObj |
31 | 29 | from deepdiff.base import Base |
32 | 30 |
|
33 | 31 | logger = logging.getLogger(__name__) |
34 | 32 | warnings.simplefilter('once', DeprecationWarning) |
35 | 33 |
|
| 34 | +try: |
| 35 | + import jsonpickle |
| 36 | +except ImportError: |
| 37 | + jsonpickle = None |
| 38 | + logger.info('jsonpickle is not installed. The to_json_pickle and from_json_pickle functions will not work.' |
| 39 | + 'If you dont need those functions, there is nothing to do.') |
| 40 | + |
36 | 41 | TREE_VIEW = 'tree' |
37 | 42 | TEXT_VIEW = 'text' |
38 | 43 |
|
@@ -476,7 +481,13 @@ def __create_hashtable(self, t, level): |
476 | 481 | ignore_string_case=self.ignore_string_case, |
477 | 482 | number_to_string_func=self.number_to_string, |
478 | 483 | ) |
479 | | - item_hash = hashes_all[item] |
| 484 | + # import pytest; pytest.set_trace() |
| 485 | + key = item |
| 486 | + if item is True: |
| 487 | + key = BoolObj.TRUE |
| 488 | + elif item is False: |
| 489 | + key = BoolObj.FALSE |
| 490 | + item_hash = hashes_all[key] |
480 | 491 | except Exception as e: # pragma: no cover |
481 | 492 | logger.error("Can not produce a hash for %s." |
482 | 493 | "Not counting this object.\n %s" % |
@@ -642,45 +653,26 @@ def __diff(self, level, parents_ids=frozenset({})): |
642 | 653 | else: |
643 | 654 | self.__diff_obj(level, parents_ids) |
644 | 655 |
|
645 | | - @property |
646 | | - def json(self): |
647 | | - warnings.warn( |
648 | | - "json property will be deprecated. Instead use: to_json_pickle() to get the json pickle or to_json() for bare-bone json.", |
649 | | - DeprecationWarning |
650 | | - ) |
651 | | - if not hasattr(self, '_json'): |
652 | | - # copy of self removes all the extra attributes since it assumes |
653 | | - # we have only a simple dictionary. |
654 | | - copied = self.copy() |
655 | | - self._json = jsonpickle.encode(copied) |
656 | | - return self._json |
657 | | - |
658 | 656 | def to_json_pickle(self): |
659 | 657 | """ |
660 | 658 | Get the json pickle of the diff object. Unless you need all the attributes and functionality of DeepDiff, running to_json() is the safer option that json pickle. |
661 | 659 | """ |
662 | | - copied = self.copy() |
663 | | - return jsonpickle.encode(copied) |
664 | | - |
665 | | - @json.deleter |
666 | | - def json(self): |
667 | | - del self._json |
668 | | - |
669 | | - @classmethod |
670 | | - def from_json(cls, value): |
671 | | - warnings.warn( |
672 | | - "from_json is renamed to from_json_pickle", |
673 | | - DeprecationWarning |
674 | | - ) |
675 | | - return cls.from_json_pickle(value) |
| 660 | + if jsonpickle: |
| 661 | + copied = self.copy() |
| 662 | + return jsonpickle.encode(copied) |
| 663 | + else: |
| 664 | + logger.error('jsonpickle library needs to be installed in order to run to_json_pickle') |
676 | 665 |
|
677 | 666 | @classmethod |
678 | 667 | def from_json_pickle(cls, value): |
679 | 668 | """ |
680 | 669 | Load DeepDiff object with all the bells and whistles from the json pickle dump. |
681 | 670 | Note that json pickle dump comes from to_json_pickle |
682 | 671 | """ |
683 | | - return jsonpickle.decode(value) |
| 672 | + if jsonpickle: |
| 673 | + return jsonpickle.decode(value) |
| 674 | + else: |
| 675 | + logger.error('jsonpickle library needs to be installed in order to run from_json_pickle') |
684 | 676 |
|
685 | 677 | def to_json(self, default_mapping=None): |
686 | 678 | """ |
|
0 commit comments