@brief applies a JSON patch in-place without copying the object @sa https://json.nlohmann.me/api/basic_json/patch/
| 23888 | /// @brief applies a JSON patch in-place without copying the object |
| 23889 | /// @sa https://json.nlohmann.me/api/basic_json/patch/ |
| 23890 | void patch_inplace(const basic_json& json_patch) |
| 23891 | { |
| 23892 | basic_json& result = *this; |
| 23893 | // the valid JSON Patch operations |
| 23894 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 23895 | |
| 23896 | const auto get_op = [](const std::string & op) |
| 23897 | { |
| 23898 | if (op == "add") |
| 23899 | { |
| 23900 | return patch_operations::add; |
| 23901 | } |
| 23902 | if (op == "remove") |
| 23903 | { |
| 23904 | return patch_operations::remove; |
| 23905 | } |
| 23906 | if (op == "replace") |
| 23907 | { |
| 23908 | return patch_operations::replace; |
| 23909 | } |
| 23910 | if (op == "move") |
| 23911 | { |
| 23912 | return patch_operations::move; |
| 23913 | } |
| 23914 | if (op == "copy") |
| 23915 | { |
| 23916 | return patch_operations::copy; |
| 23917 | } |
| 23918 | if (op == "test") |
| 23919 | { |
| 23920 | return patch_operations::test; |
| 23921 | } |
| 23922 | |
| 23923 | return patch_operations::invalid; |
| 23924 | }; |
| 23925 | |
| 23926 | // wrapper for "add" operation; add value at ptr |
| 23927 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 23928 | { |
| 23929 | // adding to the root of the target document means replacing it |
| 23930 | if (ptr.empty()) |
| 23931 | { |
| 23932 | result = val; |
| 23933 | return; |
| 23934 | } |
| 23935 | |
| 23936 | // make sure the top element of the pointer exists |
| 23937 | json_pointer const top_pointer = ptr.top(); |
| 23938 | if (top_pointer != ptr) |
| 23939 | { |
| 23940 | result.at(top_pointer); |
| 23941 | } |
| 23942 | |
| 23943 | // get reference to parent of JSON pointer ptr |
| 23944 | const auto last_path = ptr.back(); |
| 23945 | ptr.pop_back(); |
| 23946 | // parent must exist when performing patch add per RFC6902 specs |
| 23947 | basic_json& parent = result.at(ptr); |
nothing calls this directly
no test coverage detected