! @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
| 25940 | @since version 2.0.0 |
| 25941 | */ |
| 25942 | basic_json patch(const basic_json& json_patch) const |
| 25943 | { |
| 25944 | // make a working copy to apply the patch to |
| 25945 | basic_json result = *this; |
| 25946 | |
| 25947 | // the valid JSON Patch operations |
| 25948 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 25949 | |
| 25950 | const auto get_op = [](const std::string & op) |
| 25951 | { |
| 25952 | if (op == "add") |
| 25953 | { |
| 25954 | return patch_operations::add; |
| 25955 | } |
| 25956 | if (op == "remove") |
| 25957 | { |
| 25958 | return patch_operations::remove; |
| 25959 | } |
| 25960 | if (op == "replace") |
| 25961 | { |
| 25962 | return patch_operations::replace; |
| 25963 | } |
| 25964 | if (op == "move") |
| 25965 | { |
| 25966 | return patch_operations::move; |
| 25967 | } |
| 25968 | if (op == "copy") |
| 25969 | { |
| 25970 | return patch_operations::copy; |
| 25971 | } |
| 25972 | if (op == "test") |
| 25973 | { |
| 25974 | return patch_operations::test; |
| 25975 | } |
| 25976 | |
| 25977 | return patch_operations::invalid; |
| 25978 | }; |
| 25979 | |
| 25980 | // wrapper for "add" operation; add value at ptr |
| 25981 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 25982 | { |
| 25983 | // adding to the root of the target document means replacing it |
| 25984 | if (ptr.empty()) |
| 25985 | { |
| 25986 | result = val; |
| 25987 | return; |
| 25988 | } |
| 25989 | |
| 25990 | // make sure the top element of the pointer exists |
| 25991 | json_pointer top_pointer = ptr.top(); |
| 25992 | if (top_pointer != ptr) |
| 25993 | { |
| 25994 | result.at(top_pointer); |
| 25995 | } |
| 25996 | |
| 25997 | // get reference to parent of JSON pointer ptr |
| 25998 | const auto last_path = ptr.back(); |
| 25999 | ptr.pop_back(); |