! @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
| 20219 | @since version 2.0.0 |
| 20220 | */ |
| 20221 | basic_json patch(const basic_json& json_patch) const |
| 20222 | { |
| 20223 | // make a working copy to apply the patch to |
| 20224 | basic_json result = *this; |
| 20225 | |
| 20226 | // the valid JSON Patch operations |
| 20227 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 20228 | |
| 20229 | const auto get_op = [](const std::string & op) |
| 20230 | { |
| 20231 | if (op == "add") |
| 20232 | { |
| 20233 | return patch_operations::add; |
| 20234 | } |
| 20235 | if (op == "remove") |
| 20236 | { |
| 20237 | return patch_operations::remove; |
| 20238 | } |
| 20239 | if (op == "replace") |
| 20240 | { |
| 20241 | return patch_operations::replace; |
| 20242 | } |
| 20243 | if (op == "move") |
| 20244 | { |
| 20245 | return patch_operations::move; |
| 20246 | } |
| 20247 | if (op == "copy") |
| 20248 | { |
| 20249 | return patch_operations::copy; |
| 20250 | } |
| 20251 | if (op == "test") |
| 20252 | { |
| 20253 | return patch_operations::test; |
| 20254 | } |
| 20255 | |
| 20256 | return patch_operations::invalid; |
| 20257 | }; |
| 20258 | |
| 20259 | // wrapper for "add" operation; add value at ptr |
| 20260 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 20261 | { |
| 20262 | // adding to the root of the target document means replacing it |
| 20263 | if (ptr.empty()) |
| 20264 | { |
| 20265 | result = val; |
| 20266 | return; |
| 20267 | } |
| 20268 | |
| 20269 | // make sure the top element of the pointer exists |
| 20270 | json_pointer top_pointer = ptr.top(); |
| 20271 | if (top_pointer != ptr) |
| 20272 | { |
| 20273 | result.at(top_pointer); |
| 20274 | } |
| 20275 | |
| 20276 | // get reference to parent of JSON pointer ptr |
| 20277 | const auto last_path = ptr.back(); |
| 20278 | ptr.pop_back(); |
nothing calls this directly
no test coverage detected