66from abc import ABC
77from abc import abstractmethod
88from io import IOBase
9+ from typing import Dict
910from typing import Iterable
1011from typing import List
1112from typing import Mapping
2324from .pointer import UNDEFINED
2425from .pointer import JSONPointer
2526
26- # TODO: to_json()
27-
2827
2928class 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
4144class 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
8087class 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
113124class 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
147162class 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
193212class 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
231254class 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
252279Path = 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
538569def apply (
539570 patch : Union [str , IOBase , Iterable [Mapping [str , object ]], None ],
0 commit comments