@@ -258,7 +258,7 @@ def from_string(cls, patch_str):
258
258
return cls (patch )
259
259
260
260
@classmethod
261
- def from_diff (cls , src , dst , optimization = True ):
261
+ def from_diff (cls , src , dst , optimization = True , dumps = json . dumps ):
262
262
"""Creates JsonPatch instance based on comparing of two document
263
263
objects. Json patch would be created for `src` argument against `dst`
264
264
one.
@@ -269,6 +269,10 @@ def from_diff(cls, src, dst, optimization=True):
269
269
:param dst: Data source document object.
270
270
:type dst: dict
271
271
272
+ :param dumps: A function of one argument that produces a serialized
273
+ JSON string.
274
+ :type dumps: function
275
+
272
276
:return: :class:`JsonPatch` instance.
273
277
274
278
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
@@ -279,7 +283,7 @@ def from_diff(cls, src, dst, optimization=True):
279
283
True
280
284
"""
281
285
282
- builder = DiffBuilder ()
286
+ builder = DiffBuilder (dumps )
283
287
builder ._compare_values ('' , None , src , dst )
284
288
ops = list (builder .execute ())
285
289
return cls (ops )
@@ -637,7 +641,8 @@ def apply(self, obj):
637
641
638
642
class DiffBuilder (object ):
639
643
640
- def __init__ (self ):
644
+ def __init__ (self , dumps ):
645
+ self .dumps = dumps
641
646
self .index_storage = [{}, {}]
642
647
self .index_storage2 = [[], []]
643
648
self .__root = root = []
@@ -832,7 +837,7 @@ def _compare_values(self, path, key, src, dst):
832
837
# and ignore those that don't. The performance of this could be
833
838
# improved by doing more direct type checks, but we'd need to be
834
839
# careful to accept type changes that don't matter when JSONified.
835
- elif json .dumps (src ) == json .dumps (dst ):
840
+ elif self .dumps (src ) == self .dumps (dst ):
836
841
return
837
842
838
843
else :
0 commit comments