| 302 | |
| 303 | template <typename Json> |
| 304 | void 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 | { |