From e99d178396f69f8891a62e21434c2783b76146b2 Mon Sep 17 00:00:00 2001 From: Christian Lyder Jacobsen Date: Fri, 31 Jan 2020 11:58:59 +0100 Subject: Make it possible for from_diff to support custom types (issue #107) --- jsonpatch.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'jsonpatch.py') diff --git a/jsonpatch.py b/jsonpatch.py index 7f31ce5..ebaa8e3 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -258,7 +258,7 @@ class JsonPatch(object): return cls(patch) @classmethod - def from_diff(cls, src, dst, optimization=True): + def from_diff(cls, src, dst, optimization=True, dumps=json.dumps): """Creates JsonPatch instance based on comparing of two document objects. Json patch would be created for `src` argument against `dst` one. @@ -269,6 +269,10 @@ class JsonPatch(object): :param dst: Data source document object. :type dst: dict + :param dumps: A function of one argument that produces a serialized + JSON string. + :type dumps: function + :return: :class:`JsonPatch` instance. >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} @@ -279,7 +283,7 @@ class JsonPatch(object): True """ - builder = DiffBuilder() + builder = DiffBuilder(dumps) builder._compare_values('', None, src, dst) ops = list(builder.execute()) return cls(ops) @@ -637,7 +641,8 @@ class CopyOperation(PatchOperation): class DiffBuilder(object): - def __init__(self): + def __init__(self, dumps): + self.dumps = dumps self.index_storage = [{}, {}] self.index_storage2 = [[], []] self.__root = root = [] @@ -832,7 +837,7 @@ class DiffBuilder(object): # and ignore those that don't. The performance of this could be # improved by doing more direct type checks, but we'd need to be # careful to accept type changes that don't matter when JSONified. - elif json.dumps(src) == json.dumps(dst): + elif self.dumps(src) == self.dumps(dst): return else: -- cgit v1.2.1 From 0167d345ee9d7ef0f74b947ec3a7ea94def178be Mon Sep 17 00:00:00 2001 From: Christian Lyder Jacobsen Date: Fri, 6 Mar 2020 11:41:38 +0100 Subject: Subclassing can override json dumper and loader Additionally: * from_string gets a loads parameter * to_string gets a dumps_parameter * documentation added * added more tests --- jsonpatch.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'jsonpatch.py') diff --git a/jsonpatch.py b/jsonpatch.py index ebaa8e3..ce8a89f 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -165,6 +165,9 @@ def make_patch(src, dst): class JsonPatch(object): + json_dumper = staticmethod(json.dumps) + json_loader = staticmethod(_jsonloads) + """A JSON Patch is a list of Patch Operations. >>> patch = JsonPatch([ @@ -246,19 +249,23 @@ class JsonPatch(object): return not(self == other) @classmethod - def from_string(cls, patch_str): + def from_string(cls, patch_str, loads=None): """Creates JsonPatch instance from string source. :param patch_str: JSON patch as raw string. :type patch_str: str + :param loads: A function of one argument that loads a serialized + JSON string. + :type loads: function :return: :class:`JsonPatch` instance. """ - patch = _jsonloads(patch_str) + json_loader = loads or cls.json_loader + patch = json_loader(patch_str) return cls(patch) @classmethod - def from_diff(cls, src, dst, optimization=True, dumps=json.dumps): + def from_diff(cls, src, dst, optimization=True, dumps=None): """Creates JsonPatch instance based on comparing of two document objects. Json patch would be created for `src` argument against `dst` one. @@ -282,15 +289,16 @@ class JsonPatch(object): >>> new == dst True """ - - builder = DiffBuilder(dumps) + json_dumper = dumps or cls.json_dumper + builder = DiffBuilder(json_dumper) builder._compare_values('', None, src, dst) ops = list(builder.execute()) return cls(ops) - def to_string(self): + def to_string(self, dumps=None): """Returns patch set as JSON string.""" - return json.dumps(self.patch) + json_dumper = dumps or self.json_dumper + return json_dumper(self.patch) @property def _ops(self): -- cgit v1.2.1 From 29c989e815ade4aab25f42047c1ad003358b976d Mon Sep 17 00:00:00 2001 From: Christian Lyder Jacobsen Date: Mon, 16 Mar 2020 21:00:01 +0100 Subject: Make DiffBuilder's dumps argument optional --- jsonpatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'jsonpatch.py') diff --git a/jsonpatch.py b/jsonpatch.py index 63dcd97..21714c7 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -649,7 +649,7 @@ class CopyOperation(PatchOperation): class DiffBuilder(object): - def __init__(self, dumps): + def __init__(self, dumps=json.dumps): self.dumps = dumps self.index_storage = [{}, {}] self.index_storage2 = [[], []] -- cgit v1.2.1