Skip to content

Commit b54262e

Browse files
committed
JSONPatch to list of dicst for JSON encoding.
1 parent 0477f5b commit b54262e

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎jsonpath/patch.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from abc import ABC
77
from abc import abstractmethod
88
from io import IOBase
9+
from typing import Dict
910
from typing import Iterable
1011
from typing import List
1112
from typing import Mapping
@@ -23,8 +24,6 @@
2324
from .pointer import UNDEFINED
2425
from .pointer import JSONPointer
2526

26-
# TODO: to_json()
27-
2827

2928
class Op(ABC):
3029
"""One of the JSON Patch operations."""
@@ -37,6 +36,10 @@ def apply(
3736
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
3837
"""Apply this patch operation to _data_."""
3938

39+
@abstractmethod
40+
def asdict(self) -> Dict[str, object]:
41+
"""Return a dictionary representation of this operation."""
42+
4043

4144
class OpAdd(Op):
4245
"""The JSON Patch _add_ operation."""
@@ -76,6 +79,10 @@ def apply(
7679
)
7780
return data
7881

82+
def asdict(self) -> Dict[str, object]:
83+
"""Return a dictionary representation of this operation."""
84+
return {"op": self.name, "path": str(self.path), "value": self.value}
85+
7986

8087
class OpRemove(Op):
8188
"""The JSON Patch _remove_ operation."""
@@ -109,6 +116,10 @@ def apply(
109116
)
110117
return data
111118

119+
def asdict(self) -> Dict[str, object]:
120+
"""Return a dictionary representation of this operation."""
121+
return {"op": self.name, "path": str(self.path)}
122+
112123

113124
class OpReplace(Op):
114125
"""The JSON Patch _replace_ operation."""
@@ -143,6 +154,10 @@ def apply(
143154
)
144155
return data
145156

157+
def asdict(self) -> Dict[str, object]:
158+
"""Return a dictionary representation of this operation."""
159+
return {"op": self.name, "path": str(self.path), "value": self.value}
160+
146161

147162
class OpMove(Op):
148163
"""The JSON Patch _move_ operation."""
@@ -189,6 +204,10 @@ def apply(
189204

190205
return data
191206

207+
def asdict(self) -> Dict[str, object]:
208+
"""Return a dictionary representation of this operation."""
209+
return {"op": self.name, "from": str(self.source), "path": str(self.dest)}
210+
192211

193212
class OpCopy(Op):
194213
"""The JSON Patch _copy_ operation."""
@@ -227,6 +246,10 @@ def apply(
227246

228247
return data
229248

249+
def asdict(self) -> Dict[str, object]:
250+
"""Return a dictionary representation of this operation."""
251+
return {"op": self.name, "from": str(self.source), "path": str(self.dest)}
252+
230253

231254
class OpTest(Op):
232255
"""The JSON Patch _test_ operation."""
@@ -248,6 +271,10 @@ def apply(
248271
raise JSONPatchTestFailure
249272
return data
250273

274+
def asdict(self) -> Dict[str, object]:
275+
"""Return a dictionary representation of this operation."""
276+
return {"op": self.name, "path": str(self.path), "value": self.value}
277+
251278

252279
Path = Union[str, JSONPointer]
253280
"""A string representation of a JSON Pointer, or one that has already been parsed."""
@@ -534,6 +561,10 @@ def apply(
534561
raise JSONPatchError(f"{err} ({op.name}:{i})") from err
535562
return _data
536563

564+
def asdicts(self) -> List[Dict[str, object]]:
565+
"""Return a list of this patch's operations as dictionaries."""
566+
return [op.asdict() for op in self.ops]
567+
537568

538569
def apply(
539570
patch: Union[str, IOBase, Iterable[Mapping[str, object]], None],

‎tests/test_json_patch.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,17 @@ def test_apply_to_file_like() -> None:
248248

249249
data_doc = StringIO(json.dumps({}))
250250
assert patch.apply(patch_doc, data_doc) == {"foo": {"bar": [1]}}
251+
252+
253+
def test_asdict() -> None:
254+
patch_doc = [
255+
{"op": "add", "path": "/foo/bar", "value": "foo"},
256+
{"op": "remove", "path": "/foo/bar"},
257+
{"op": "replace", "path": "/foo/bar", "value": "foo"},
258+
{"op": "move", "from": "/baz/foo", "path": "/foo/bar"},
259+
{"op": "copy", "from": "/baz/foo", "path": "/foo/bar"},
260+
{"op": "test", "path": "/foo/bar", "value": "foo"},
261+
]
262+
263+
patch = JSONPatch(patch_doc)
264+
assert patch.asdicts() == patch_doc

0 commit comments

Comments
 (0)