! @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 funcion, 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 appli
| 10098 | @since version 2.0.0 |
| 10099 | */ |
| 10100 | basic_json patch(const basic_json& json_patch) const |
| 10101 | { |
| 10102 | // make a working copy to apply the patch to |
| 10103 | basic_json result = *this; |
| 10104 | |
| 10105 | // the valid JSON Patch operations |
| 10106 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 10107 | |
| 10108 | const auto get_op = [](const std::string op) |
| 10109 | { |
| 10110 | if (op == "add") |
| 10111 | { |
| 10112 | return patch_operations::add; |
| 10113 | } |
| 10114 | if (op == "remove") |
| 10115 | { |
| 10116 | return patch_operations::remove; |
| 10117 | } |
| 10118 | if (op == "replace") |
| 10119 | { |
| 10120 | return patch_operations::replace; |
| 10121 | } |
| 10122 | if (op == "move") |
| 10123 | { |
| 10124 | return patch_operations::move; |
| 10125 | } |
| 10126 | if (op == "copy") |
| 10127 | { |
| 10128 | return patch_operations::copy; |
| 10129 | } |
| 10130 | if (op == "test") |
| 10131 | { |
| 10132 | return patch_operations::test; |
| 10133 | } |
| 10134 | |
| 10135 | return patch_operations::invalid; |
| 10136 | }; |
| 10137 | |
| 10138 | // wrapper for "add" operation; add value at ptr |
| 10139 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 10140 | { |
| 10141 | // adding to the root of the target document means replacing it |
| 10142 | if (ptr.is_root()) |
| 10143 | { |
| 10144 | result = val; |
| 10145 | } |
| 10146 | else |
| 10147 | { |
| 10148 | // make sure the top element of the pointer exists |
| 10149 | json_pointer top_pointer = ptr.top(); |
| 10150 | if (top_pointer != ptr) |
| 10151 | { |
| 10152 | result.at(top_pointer); |
| 10153 | } |
| 10154 | |
| 10155 | // get reference to parent of JSON pointer ptr |
| 10156 | const auto last_path = ptr.pop_back(); |
| 10157 | basic_json& parent = result[ptr]; |
nothing calls this directly
no test coverage detected