@brief applies a JSON patch in-place without copying the object @sa https://json.nlohmann.me/api/basic_json/patch/
| 23999 | /// @brief applies a JSON patch in-place without copying the object |
| 24000 | /// @sa https://json.nlohmann.me/api/basic_json/patch/ |
| 24001 | void patch_inplace(const basic_json& json_patch) |
| 24002 | { |
| 24003 | basic_json& result = *this; |
| 24004 | // the valid JSON Patch operations |
| 24005 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 24006 | |
| 24007 | const auto get_op = [](const std::string & op) |
| 24008 | { |
| 24009 | if (op == "add") |
| 24010 | { |
| 24011 | return patch_operations::add; |
| 24012 | } |
| 24013 | if (op == "remove") |
| 24014 | { |
| 24015 | return patch_operations::remove; |
| 24016 | } |
| 24017 | if (op == "replace") |
| 24018 | { |
| 24019 | return patch_operations::replace; |
| 24020 | } |
| 24021 | if (op == "move") |
| 24022 | { |
| 24023 | return patch_operations::move; |
| 24024 | } |
| 24025 | if (op == "copy") |
| 24026 | { |
| 24027 | return patch_operations::copy; |
| 24028 | } |
| 24029 | if (op == "test") |
| 24030 | { |
| 24031 | return patch_operations::test; |
| 24032 | } |
| 24033 | |
| 24034 | return patch_operations::invalid; |
| 24035 | }; |
| 24036 | |
| 24037 | // wrapper for "add" operation; add value at ptr |
| 24038 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 24039 | { |
| 24040 | // adding to the root of the target document means replacing it |
| 24041 | if (ptr.empty()) |
| 24042 | { |
| 24043 | result = val; |
| 24044 | return; |
| 24045 | } |
| 24046 | |
| 24047 | // make sure the top element of the pointer exists |
| 24048 | json_pointer const top_pointer = ptr.top(); |
| 24049 | if (top_pointer != ptr) |
| 24050 | { |
| 24051 | result.at(top_pointer); |
| 24052 | } |
| 24053 | |
| 24054 | // get reference to parent of JSON pointer ptr |
| 24055 | const auto last_path = ptr.back(); |
| 24056 | ptr.pop_back(); |
| 24057 | // parent must exist when performing patch add per RFC6902 specs |
| 24058 | basic_json& parent = result.at(ptr); |