This is a small project that demonstrates how to remove redundant operations in the json patch
The project takes in a json patch and tries to simplify the patch by removing redundant operations. It also transforms an operation based on the previous operation and deletes appropriate children if a parent path is deleted.
Remove
+Add
can be combined intoReplace
Add
can be removed if followed by aRemove
(along with theRemove
itself, since there is no point in removing a path that was added in a patch)Add
orRemove
can be removed if followed by aReplace
Replace
andRemove
operations can be deduplicated- If a
Remove
is done at a parent path, then all children without atest
,copy
ormove
(as the last operation) can be removed
There are 2 data-structures:
- Operation tree: As each operation is processed in the patch, we build up the tree. Each node in the tree contains the node value (the path that ends here), the last operation that happened on the node and pointers to the children nodes.
- Result set: Contains the simplified operations. As each new operation is processed, the result set is updated to delete the previous operation as per the rules above (we actually do a soft delete and clean up the result set later)
Simplifier.scala contains the complete code. Tests are here. The input
and output
folders contains the input patch and the simplified patch respectively.
1.json
testsadd
+remove
2.json
testsadd
+remove
with everything negated3.json
testsadd
+remove
with the root path deleted4.json
testsparent
+child
removal
andreplace
substitution for a previousremove
5.json
tests more of the same, along withremove
removal if followed byreplace
and deduplication ofreplace
6.json
hastest
and ensure none of the above rules can be applied, say if atest
lies in betweenremove
andadd
7.json
testscopy
andmove
8.json
testscopy
,move
,test
,add
andremove
- Merge Json values on
Replace
orAdd
followed by anotherAdd
- Merge children values to the parent and create a single JSON (if the child operations are not
Test
,Copy
orMove
) - Make the functions more functional, remove mutability
- Better and clear code - maybe use state machines?