MCPcopy Create free account
hub / github.com/AllentDan/LibtorchTutorials / patch

Function patch

lesson6-Segmentation/json.hpp:24662–24919  ·  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

24660 @since version 2.0.0
24661 */
24662 basic_json patch(const basic_json& json_patch) const
24663 {
24664 // make a working copy to apply the patch to
24665 basic_json result = *this;
24666
24667 // the valid JSON Patch operations
24668 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
24669
24670 const auto get_op = [](const std::string & op)
24671 {
24672 if (op == "add")
24673 {
24674 return patch_operations::add;
24675 }
24676 if (op == "remove")
24677 {
24678 return patch_operations::remove;
24679 }
24680 if (op == "replace")
24681 {
24682 return patch_operations::replace;
24683 }
24684 if (op == "move")
24685 {
24686 return patch_operations::move;
24687 }
24688 if (op == "copy")
24689 {
24690 return patch_operations::copy;
24691 }
24692 if (op == "test")
24693 {
24694 return patch_operations::test;
24695 }
24696
24697 return patch_operations::invalid;
24698 };
24699
24700 // wrapper for "add" operation; add value at ptr
24701 const auto operation_add = [&result](json_pointer & ptr, basic_json val)
24702 {
24703 // adding to the root of the target document means replacing it
24704 if (ptr.empty())
24705 {
24706 result = val;
24707 return;
24708 }
24709
24710 // make sure the top element of the pointer exists
24711 json_pointer top_pointer = ptr.top();
24712 if (top_pointer != ptr)
24713 {
24714 result.at(top_pointer);
24715 }
24716
24717 // get reference to parent of JSON pointer ptr
24718 const auto last_path = ptr.back();
24719 ptr.pop_back();

Callers

nothing calls this directly

Calls 11

createFunction · 0.85
pop_backMethod · 0.80
push_backMethod · 0.80
insertMethod · 0.80
beginMethod · 0.80
endMethod · 0.80
eraseMethod · 0.80
dumpMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected