@@ -165,6 +165,9 @@ def make_patch(src, dst):
165165
166166
167167class JsonPatch (object ):
168+ json_dumper = staticmethod (json .dumps )
169+ json_loader = staticmethod (_jsonloads )
170+
168171 """A JSON Patch is a list of Patch Operations.
169172
170173 >>> patch = JsonPatch([
@@ -246,19 +249,23 @@ def __ne__(self, other):
246249 return not (self == other )
247250
248251 @classmethod
249- def from_string (cls , patch_str ):
252+ def from_string (cls , patch_str , loads = None ):
250253 """Creates JsonPatch instance from string source.
251254
252255 :param patch_str: JSON patch as raw string.
253256 :type patch_str: str
257+ :param loads: A function of one argument that loads a serialized
258+ JSON string.
259+ :type loads: function
254260
255261 :return: :class:`JsonPatch` instance.
256262 """
257- patch = _jsonloads (patch_str )
263+ json_loader = loads or cls .json_loader
264+ patch = json_loader (patch_str )
258265 return cls (patch )
259266
260267 @classmethod
261- def from_diff (cls , src , dst , optimization = True ):
268+ def from_diff (cls , src , dst , optimization = True , dumps = None ):
262269 """Creates JsonPatch instance based on comparison of two document
263270 objects. Json patch would be created for `src` argument against `dst`
264271 one.
@@ -269,6 +276,10 @@ def from_diff(cls, src, dst, optimization=True):
269276 :param dst: Data source document object.
270277 :type dst: dict
271278
279+ :param dumps: A function of one argument that produces a serialized
280+ JSON string.
281+ :type dumps: function
282+
272283 :return: :class:`JsonPatch` instance.
273284
274285 >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
@@ -278,15 +289,16 @@ def from_diff(cls, src, dst, optimization=True):
278289 >>> new == dst
279290 True
280291 """
281-
282- builder = DiffBuilder ()
292+ json_dumper = dumps or cls . json_dumper
293+ builder = DiffBuilder (json_dumper )
283294 builder ._compare_values ('' , None , src , dst )
284295 ops = list (builder .execute ())
285296 return cls (ops )
286297
287- def to_string (self ):
298+ def to_string (self , dumps = None ):
288299 """Returns patch set as JSON string."""
289- return json .dumps (self .patch )
300+ json_dumper = dumps or self .json_dumper
301+ return json_dumper (self .patch )
290302
291303 @property
292304 def _ops (self ):
@@ -646,7 +658,8 @@ def apply(self, obj):
646658
647659class DiffBuilder (object ):
648660
649- def __init__ (self ):
661+ def __init__ (self , dumps = json .dumps ):
662+ self .dumps = dumps
650663 self .index_storage = [{}, {}]
651664 self .index_storage2 = [[], []]
652665 self .__root = root = []
@@ -841,7 +854,7 @@ def _compare_values(self, path, key, src, dst):
841854 # and ignore those that don't. The performance of this could be
842855 # improved by doing more direct type checks, but we'd need to be
843856 # careful to accept type changes that don't matter when JSONified.
844- elif json .dumps (src ) == json .dumps (dst ):
857+ elif self .dumps (src ) == self .dumps (dst ):
845858 return
846859
847860 else :
0 commit comments