! @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
| 8439 | @since version 2.0.0 |
| 8440 | */ |
| 8441 | basic_json patch(const basic_json& json_patch) const |
| 8442 | { |
| 8443 | // make a working copy to apply the patch to |
| 8444 | basic_json result = *this; |
| 8445 | |
| 8446 | // the valid JSON Patch operations |
| 8447 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 8448 | |
| 8449 | const auto get_op = [](const std::string & op) |
| 8450 | { |
| 8451 | if (op == "add") |
| 8452 | { |
| 8453 | return patch_operations::add; |
| 8454 | } |
| 8455 | if (op == "remove") |
| 8456 | { |
| 8457 | return patch_operations::remove; |
| 8458 | } |
| 8459 | if (op == "replace") |
| 8460 | { |
| 8461 | return patch_operations::replace; |
| 8462 | } |
| 8463 | if (op == "move") |
| 8464 | { |
| 8465 | return patch_operations::move; |
| 8466 | } |
| 8467 | if (op == "copy") |
| 8468 | { |
| 8469 | return patch_operations::copy; |
| 8470 | } |
| 8471 | if (op == "test") |
| 8472 | { |
| 8473 | return patch_operations::test; |
| 8474 | } |
| 8475 | |
| 8476 | return patch_operations::invalid; |
| 8477 | }; |
| 8478 | |
| 8479 | // wrapper for "add" operation; add value at ptr |
| 8480 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 8481 | { |
| 8482 | // adding to the root of the target document means replacing it |
| 8483 | if (ptr.empty()) |
| 8484 | { |
| 8485 | result = val; |
| 8486 | return; |
| 8487 | } |
| 8488 | |
| 8489 | // make sure the top element of the pointer exists |
| 8490 | json_pointer top_pointer = ptr.top(); |
| 8491 | if (top_pointer != ptr) |
| 8492 | { |
| 8493 | result.at(top_pointer); |
| 8494 | } |
| 8495 | |
| 8496 | // get reference to parent of JSON pointer ptr |
| 8497 | const auto last_path = ptr.back(); |
| 8498 | ptr.pop_back(); |