@brief applies a JSON patch in-place without copying the object @sa https://json.nlohmann.me/api/basic_json/patch/
| 23844 | /// @brief applies a JSON patch in-place without copying the object |
| 23845 | /// @sa https://json.nlohmann.me/api/basic_json/patch/ |
| 23846 | void patch_inplace(const basic_json& json_patch) |
| 23847 | { |
| 23848 | basic_json& result = *this; |
| 23849 | // the valid JSON Patch operations |
| 23850 | enum class patch_operations {add, remove, replace, move, copy, test, invalid}; |
| 23851 | |
| 23852 | const auto get_op = [](const std::string & op) |
| 23853 | { |
| 23854 | if (op == "add") |
| 23855 | { |
| 23856 | return patch_operations::add; |
| 23857 | } |
| 23858 | if (op == "remove") |
| 23859 | { |
| 23860 | return patch_operations::remove; |
| 23861 | } |
| 23862 | if (op == "replace") |
| 23863 | { |
| 23864 | return patch_operations::replace; |
| 23865 | } |
| 23866 | if (op == "move") |
| 23867 | { |
| 23868 | return patch_operations::move; |
| 23869 | } |
| 23870 | if (op == "copy") |
| 23871 | { |
| 23872 | return patch_operations::copy; |
| 23873 | } |
| 23874 | if (op == "test") |
| 23875 | { |
| 23876 | return patch_operations::test; |
| 23877 | } |
| 23878 | |
| 23879 | return patch_operations::invalid; |
| 23880 | }; |
| 23881 | |
| 23882 | // wrapper for "add" operation; add value at ptr |
| 23883 | const auto operation_add = [&result](json_pointer & ptr, basic_json val) |
| 23884 | { |
| 23885 | // adding to the root of the target document means replacing it |
| 23886 | if (ptr.empty()) |
| 23887 | { |
| 23888 | result = val; |
| 23889 | return; |
| 23890 | } |
| 23891 | |
| 23892 | // make sure the top element of the pointer exists |
| 23893 | json_pointer top_pointer = ptr.top(); |
| 23894 | if (top_pointer != ptr) |
| 23895 | { |
| 23896 | result.at(top_pointer); |
| 23897 | } |
| 23898 | |
| 23899 | // get reference to parent of JSON pointer ptr |
| 23900 | const auto last_path = ptr.back(); |
| 23901 | ptr.pop_back(); |
| 23902 | // parent must exist when performing patch add per RFC6902 specs |
| 23903 | basic_json& parent = result.at(ptr); |