@@ -145,6 +145,45 @@ DeepDiff by default uses a restricted Python pickle function to deserialize the |
145 | 145 |
|
146 | 146 | The user of Delta can decide to switch the serializer and deserializer to their custom ones. The serializer and deserializer parameters can be used exactly for that reason. The best way to come up with your own serializer and deserialier is to take a look at the `pickle_dump and pickle_load functions in the serializer module <https://github.com/seperman/deepdiff/serialization.py>`_ |
147 | 147 |
|
| 148 | +.. _delta_json_deserializer_label: |
| 149 | + |
| 150 | +Json Deserializer for Delta |
| 151 | +``````````````````````````` |
| 152 | + |
| 153 | +If all you deal with are Json serializable objects, you can use json for serialization. |
| 154 | + |
| 155 | +>>> from deepdiff import DeepDiff, Delta |
| 156 | +>>> import json |
| 157 | +>>> t1 = {"a": 1} |
| 158 | +>>> t2 = {"a": 2} |
| 159 | +>>> |
| 160 | +>>> diff = DeepDiff(t1, t2) |
| 161 | +>>> delta = Delta(diff, serializer=json.dumps) |
| 162 | +>>> dump = delta.dumps() |
| 163 | +>>> dump |
| 164 | +'{"values_changed": {"root[\'a\']": {"new_value": 2}}}' |
| 165 | +>>> delta_reloaded = Delta(dump, deserializer=json.loads) |
| 166 | +>>> t2 == delta_reloaded + t1 |
| 167 | +True |
| 168 | + |
| 169 | + |
| 170 | +.. note:: |
| 171 | + |
| 172 | + Json is very limited and easily you can get to deltas that are not json serializable. You will probably want to extend the Python's Json serializer to support your needs. |
| 173 | + |
| 174 | + >>> t1 = {"a": 1} |
| 175 | + >>> t2 = {"a": None} |
| 176 | + >>> diff = DeepDiff(t1, t2) |
| 177 | + >>> diff |
| 178 | + {'type_changes': {"root['a']": {'old_type': <class 'int'>, 'new_type': <class 'NoneType'>, 'old_value': 1, 'new_value': None}}} |
| 179 | + >>> Delta(diff, serializer=json.dumps) |
| 180 | + <Delta: {'type_changes': {"root['a']": {'old_type': <class 'int'>, 'new_type': <class 'NoneType'>, 'new_v...}> |
| 181 | + >>> delta = Delta(diff, serializer=json.dumps) |
| 182 | + >>> dump = delta.dumps() |
| 183 | + Traceback (most recent call last): |
| 184 | + File "lib/python3.8/json/encoder.py", line 179, in default |
| 185 | + raise TypeError(f'Object of type {o.__class__.__name__} ' |
| 186 | + TypeError: Object of type type is not JSON serializable |
148 | 187 |
|
149 | 188 | .. _delta_serializer_label: |
150 | 189 |
|
@@ -162,6 +201,7 @@ Delta by default uses Python's pickle to serialize and deserialize. While the un |
162 | 201 |
|
163 | 202 | In fact only a few Python object types are allowed by default. The user of DeepDiff can pass additional types using the :ref:`delta_safe_to_import_label` to allow further object types that need to be allowed. |
164 | 203 |
|
| 204 | + |
165 | 205 | .. _delta_mutate_label: |
166 | 206 |
|
167 | 207 | Delta Mutate parameter |
@@ -347,6 +387,12 @@ In order to let Delta know that this specific module is safe to import, you will |
347 | 387 |
|
348 | 388 | >>> delta = Delta(dump, safe_to_import={'mypackage.mymodule'}) |
349 | 389 |
|
| 390 | +.. note :: |
| 391 | +
|
| 392 | + If you pass a custom deserializer to Delta, DeepDiff will pass safe_to_import parameter to the custom deserializer if that deserializer takes safe_to_import as a parameter in its definition. |
| 393 | + For example if you just use json.loads as deserializer, the safe_to_import items won't be passed to it since json.loads does not have such a parameter. |
| 394 | +
|
| 395 | +
|
350 | 396 | .. _delta_verify_symmetry_label: |
351 | 397 |
|
352 | 398 | Delta Verify Symmetry parameter |
|