MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / apply_patch

Function apply_patch

include/jsoncons_ext/jsonpatch/jsonpatch.hpp:304–568  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

302
303template <typename Json>
304void apply_patch(Json& target, const Json& patch, std::error_code& ec)
305{
306 if (!patch.is_array())
307 {
308 ec = jsonpatch_errc::invalid_patch;
309 return;
310 }
311
312 using char_type = typename Json::char_type;
313 using string_type = std::basic_string<char_type>;
314 using json_pointer_type = jsonpointer::basic_json_pointer<char_type>;
315
316 jsoncons::jsonpatch::detail::operation_unwinder<Json> unwinder(target);
317 std::error_code local_ec;
318
319 // Validate
320
321 for (const auto& operation : patch.array_range())
322 {
323 unwinder.state =jsoncons::jsonpatch::detail::state_type::begin;
324
325 auto it_op = operation.find(detail::jsonpatch_names<char_type>::op_name());
326 if (it_op == operation.object_range().end())
327 {
328 ec = jsonpatch_errc::invalid_patch;
329 unwinder.state =jsoncons::jsonpatch::detail::state_type::abort;
330 return;
331 }
332 string_type op = it_op->value().template as<string_type>();
333
334 auto it_path = operation.find(detail::jsonpatch_names<char_type>::path_name());
335 if (it_path == operation.object_range().end())
336 {
337 ec = jsonpatch_errc::invalid_patch;
338 unwinder.state =jsoncons::jsonpatch::detail::state_type::abort;
339 return;
340 }
341 string_type path = it_path->value().template as<string_type>();
342 auto location = json_pointer_type::parse(path, local_ec);
343 if (local_ec)
344 {
345 ec = jsonpatch_errc::invalid_patch;
346 unwinder.state =jsoncons::jsonpatch::detail::state_type::abort;
347 return;
348 }
349
350 if (op ==jsoncons::jsonpatch::detail::jsonpatch_names<char_type>::test_name())
351 {
352 Json val = jsonpointer::get(target,location,local_ec);
353 if (local_ec)
354 {
355 ec = jsonpatch_errc::test_failed;
356 unwinder.state =jsoncons::jsonpatch::detail::state_type::abort;
357 return;
358 }
359 auto it_value = operation.find(detail::jsonpatch_names<char_type>::value_name());
360 if (it_value == operation.object_range().end())
361 {

Callers 8

jsonpatch_testsFunction · 0.85
check_patchFunction · 0.85
jsonpatch_add_addFunction · 0.85
create_a_json_patchFunction · 0.85
defaults_exampleFunction · 0.85

Calls 11

parseFunction · 0.85
definite_pathFunction · 0.85
add_if_absentFunction · 0.85
jsonpatch_errorClass · 0.85
getFunction · 0.50
replaceFunction · 0.50
removeFunction · 0.50
is_arrayMethod · 0.45
findMethod · 0.45
endMethod · 0.45
valueMethod · 0.45

Tested by 2

jsonpatch_testsFunction · 0.68
check_patchFunction · 0.68