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+Addcan be combined intoReplaceAddcan be removed if followed by aRemove(along with theRemoveitself, since there is no point in removing a path that was added in a patch)AddorRemovecan be removed if followed by aReplaceReplaceandRemoveoperations can be deduplicated- If a
Removeis done at a parent path, then all children without atest,copyormove(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.jsontestsadd+remove2.jsontestsadd+removewith everything negated3.jsontestsadd+removewith the root path deleted4.jsontestsparent+childremovalandreplacesubstitution for a previousremove5.jsontests more of the same, along withremoveremoval if followed byreplaceand deduplication ofreplace6.jsonhastestand ensure none of the above rules can be applied, say if atestlies in betweenremoveandadd7.jsontestscopyandmove8.jsontestscopy,move,test,addandremove
- Merge Json values on
ReplaceorAddfollowed by anotherAdd - Merge children values to the parent and create a single JSON (if the child operations are not
Test,CopyorMove) - Make the functions more functional, remove mutability
- Better and clear code - maybe use state machines?