MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / patch

Function patch

external/json/json.hpp:19651–19911  ·  view source on GitHub ↗

! @brief applies a JSON patch [JSON Patch](http://jsonpatch.com) defines a JSON document structure for expressing a sequence of operations to apply to a JSON) document. With this function, a JSON Patch is applied to the current JSON value by executing all operations from the patch. @param[in] json_patch JSON patch document @return patched document @note The appl

Source from the content-addressed store, hash-verified

19649 @since version 2.0.0
19650 */
19651 basic_json patch(const basic_json& json_patch) const
19652 {
19653 // make a working copy to apply the patch to
19654 basic_json result = *this;
19655
19656 // the valid JSON Patch operations
19657 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
19658
19659 const auto get_op = [](const std::string & op)
19660 {
19661 if (op == "add")
19662 {
19663 return patch_operations::add;
19664 }
19665 if (op == "remove")
19666 {
19667 return patch_operations::remove;
19668 }
19669 if (op == "replace")
19670 {
19671 return patch_operations::replace;
19672 }
19673 if (op == "move")
19674 {
19675 return patch_operations::move;
19676 }
19677 if (op == "copy")
19678 {
19679 return patch_operations::copy;
19680 }
19681 if (op == "test")
19682 {
19683 return patch_operations::test;
19684 }
19685
19686 return patch_operations::invalid;
19687 };
19688
19689 // wrapper for "add" operation; add value at ptr
19690 const auto operation_add = [&result](json_pointer & ptr, basic_json val)
19691 {
19692 // adding to the root of the target document means replacing it
19693 if (ptr.is_root())
19694 {
19695 result = val;
19696 }
19697 else
19698 {
19699 // make sure the top element of the pointer exists
19700 json_pointer top_pointer = ptr.top();
19701 if (top_pointer != ptr)
19702 {
19703 result.at(top_pointer);
19704 }
19705
19706 // get reference to parent of JSON pointer ptr
19707 const auto last_path = ptr.pop_back();
19708 basic_json& parent = result[ptr];

Callers

nothing calls this directly

Calls 13

createFunction · 0.85
to_stringFunction · 0.85
is_rootMethod · 0.80
topMethod · 0.80
atMethod · 0.80
pop_backMethod · 0.80
push_backMethod · 0.80
findMethod · 0.80
sizeMethod · 0.45
insertMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected