diff --git a/src/json.hpp b/src/json.hpp index 9d6687ddc..09b6a800c 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -10002,13 +10002,16 @@ basic_json_parser_63: // in a second pass, traverse the remaining elements // remove my remaining elements + const auto end_index = static_cast(result.size()); while (i < source.size()) { - result.push_back(object( + // add operations in reverse order to avoid invalid + // indices + result.insert(result.begin() + end_index, { {"op", "remove"}, {"path", path + "/" + std::to_string(i)} - })); + }); ++i; } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 9681bed2f..c5de92463 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -9312,13 +9312,16 @@ class basic_json // in a second pass, traverse the remaining elements // remove my remaining elements + const auto end_index = static_cast(result.size()); while (i < source.size()) { - result.push_back(object( + // add operations in reverse order to avoid invalid + // indices + result.insert(result.begin() + end_index, { {"op", "remove"}, {"path", path + "/" + std::to_string(i)} - })); + }); ++i; }