! @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
| 22108 | @since version 2.0.0 |
| 22109 | */ |
| 22110 | basic_json patch(const basic_json& json_patch) const |
| 22111 | { |
| 22112 | // make a working copy to apply the patch to |
| 22113 | basic_json result = *this; |
| 22114 | |
| 22115 | // the valid JSON Patch operations |
| 22116 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 22117 | |
| 22118 | const auto get_op = [](const std::string & op) |
| 22119 | { |
| 22120 | if (op == "add") |
| 22121 | { |
| 22122 | return patch_operations::add; |
| 22123 | } |
| 22124 | if (op == "remove") |
| 22125 | { |
| 22126 | return patch_operations::remove; |
| 22127 | } |
| 22128 | if (op == "replace") |
| 22129 | { |
| 22130 | return patch_operations::replace; |
| 22131 | } |
| 22132 | if (op == "move") |
| 22133 | { |
| 22134 | return patch_operations::move; |
| 22135 | } |
| 22136 | if (op == "copy") |
| 22137 | { |
| 22138 | return patch_operations::copy; |
| 22139 | } |
| 22140 | if (op == "test") |
| 22141 | { |
| 22142 | return patch_operations::test; |
| 22143 | } |
| 22144 | |
| 22145 | return patch_operations::invalid; |
| 22146 | }; |
| 22147 | |
| 22148 | // wrapper for "add" operation; add value at ptr |
| 22149 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 22150 | { |
| 22151 | // adding to the root of the target document means replacing it |
| 22152 | if (ptr.empty()) |
| 22153 | { |
| 22154 | result = val; |
| 22155 | return; |
| 22156 | } |
| 22157 | |
| 22158 | // make sure the top element of the pointer exists |
| 22159 | json_pointer top_pointer = ptr.top(); |
| 22160 | if (top_pointer != ptr) |
| 22161 | { |
| 22162 | result.at(top_pointer); |
| 22163 | } |
| 22164 | |
| 22165 | // get reference to parent of JSON pointer ptr |
| 22166 | const auto last_path = ptr.back(); |
| 22167 | ptr.pop_back(); |