diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 15d613e1..45632dec 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -8347,7 +8347,7 @@ class basic_json result.push_back( { {"op", "add"}, - {"path", path + "/" + std::to_string(i)}, + {"path", path + "/-"}, {"value", target[i]} }); ++i; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 77b08c0f..eeb51864 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -23833,7 +23833,7 @@ class basic_json result.push_back( { {"op", "add"}, - {"path", path + "/" + std::to_string(i)}, + {"path", path + "/-"}, {"value", target[i]} }); ++i; diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 3d86d33d..7d72fc86 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -1896,6 +1896,14 @@ TEST_CASE("regression tests") static_assert(!std::is_constructible>::value, ""); static_assert(std::is_constructible>::value, ""); } + + SECTION("issue #1983 - JSON patch diff for op=add formation is not as per standard (RFC 6902)") + { + const auto source = R"({ "foo": [ "1", "2" ] })"_json; + const auto target = R"({"foo": [ "1", "2", "3" ]})"_json; + const auto result = json::diff(source, target); + CHECK(result.dump() == R"([{"op":"add","path":"/foo/-","value":"3"}])"); + } } #if not defined(JSON_NOEXCEPTION)