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