@brief creates a diff as a JSON patch @sa https://json.nlohmann.me/api/basic_json/diff/
| 24279 | /// @brief creates a diff as a JSON patch |
| 24280 | /// @sa https://json.nlohmann.me/api/basic_json/diff/ |
| 24281 | JSON_HEDLEY_WARN_UNUSED_RESULT |
| 24282 | static basic_json diff(const basic_json& source, const basic_json& target, |
| 24283 | const std::string& path = "") |
| 24284 | { |
| 24285 | // the patch |
| 24286 | basic_json result(value_t::array); |
| 24287 | |
| 24288 | // if the values are the same, return empty patch |
| 24289 | if (source == target) |
| 24290 | { |
| 24291 | return result; |
| 24292 | } |
| 24293 | |
| 24294 | if (source.type() != target.type()) |
| 24295 | { |
| 24296 | // different types: replace value |
| 24297 | result.push_back( |
| 24298 | { |
| 24299 | {"op", "replace"}, {"path", path}, {"value", target} |
| 24300 | }); |
| 24301 | return result; |
| 24302 | } |
| 24303 | |
| 24304 | switch (source.type()) |
| 24305 | { |
| 24306 | case value_t::array: |
| 24307 | { |
| 24308 | // first pass: traverse common elements |
| 24309 | std::size_t i = 0; |
| 24310 | while (i < source.size() && i < target.size()) |
| 24311 | { |
| 24312 | // recursive call to compare array values at index i |
| 24313 | auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i))); |
| 24314 | result.insert(result.end(), temp_diff.begin(), temp_diff.end()); |
| 24315 | ++i; |
| 24316 | } |
| 24317 | |
| 24318 | // We now reached the end of at least one array |
| 24319 | // in a second pass, traverse the remaining elements |
| 24320 | |
| 24321 | // remove my remaining elements |
| 24322 | const auto end_index = static_cast<difference_type>(result.size()); |
| 24323 | while (i < source.size()) |
| 24324 | { |
| 24325 | // add operations in reverse order to avoid invalid |
| 24326 | // indices |
| 24327 | result.insert(result.begin() + end_index, object( |
| 24328 | { |
| 24329 | {"op", "remove"}, |
| 24330 | {"path", detail::concat(path, '/', std::to_string(i))} |
| 24331 | })); |
| 24332 | ++i; |
| 24333 | } |
| 24334 | |
| 24335 | // add other remaining elements |
| 24336 | while (i < target.size()) |
| 24337 | { |
| 24338 | result.push_back( |
nothing calls this directly
no test coverage detected