MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / basic_json diff

Function basic_json diff

extern/json/json.hpp:24170–24301  ·  view source on GitHub ↗

@brief creates a diff as a JSON patch @sa https://json.nlohmann.me/api/basic_json/diff/

Source from the content-addressed store, hash-verified

24168 /// @brief creates a diff as a JSON patch
24169 /// @sa https://json.nlohmann.me/api/basic_json/diff/
24170 JSON_HEDLEY_WARN_UNUSED_RESULT
24171 static basic_json diff(const basic_json& source, const basic_json& target,
24172 const std::string& path = "")
24173 {
24174 // the patch
24175 basic_json result(value_t::array);
24176
24177 // if the values are the same, return empty patch
24178 if (source == target)
24179 {
24180 return result;
24181 }
24182
24183 if (source.type() != target.type())
24184 {
24185 // different types: replace value
24186 result.push_back(
24187 {
24188 {"op", "replace"}, {"path", path}, {"value", target}
24189 });
24190 return result;
24191 }
24192
24193 switch (source.type())
24194 {
24195 case value_t::array:
24196 {
24197 // first pass: traverse common elements
24198 std::size_t i = 0;
24199 while (i < source.size() && i < target.size())
24200 {
24201 // recursive call to compare array values at index i
24202 auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i)));
24203 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
24204 ++i;
24205 }
24206
24207 // We now reached the end of at least one array
24208 // in a second pass, traverse the remaining elements
24209
24210 // remove my remaining elements
24211 const auto end_index = static_cast<difference_type>(result.size());
24212 while (i < source.size())
24213 {
24214 // add operations in reverse order to avoid invalid
24215 // indices
24216 result.insert(result.begin() + end_index, object(
24217 {
24218 {"op", "remove"},
24219 {"path", detail::concat(path, '/', std::to_string(i))}
24220 }));
24221 ++i;
24222 }
24223
24224 // add other remaining elements
24225 while (i < target.size())
24226 {
24227 result.push_back(

Callers

nothing calls this directly

Calls 15

escapeFunction · 0.85
concatFunction · 0.70
to_stringFunction · 0.70
objectFunction · 0.50
typeMethod · 0.45
push_backMethod · 0.45
sizeMethod · 0.45
insertMethod · 0.45
endMethod · 0.45
beginMethod · 0.45
cbeginMethod · 0.45
cendMethod · 0.45

Tested by

no test coverage detected