From f4d08227bc42b32001b06bf9ff324baf4bf9d3d1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 23 Sep 2026 23:16:17 +0200 Subject: [PATCH] Use the shared recursion limit in diff() diff_depth_limit() is gone in favor of detail::recursion_depth_limit(). Signed-off-by: Niels Lohmann --- include/nlohmann/json.hpp | 21 +++++++-------------- single_include/nlohmann/json.hpp | 21 +++++++-------------- tests/src/unit-json_patch.cpp | 2 +- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 3d3ecd704..5fd786c50 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5386,23 +5386,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // compares them with operator== on the way, which recurses as well, // so values nested deeply enough used to exhaust the call stack. // Both only descend as far as the source is nested, so a source - // nested no more than diff_depth_limit() levels deep - all but a - // vanishing minority - is diffed recursively as before; deeper ones - // are diffed without the call stack. - if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, diff_depth_limit()))) + // nested no more than detail::recursion_depth_limit() levels deep - + // all but a vanishing minority - is diffed recursively as before; + // deeper ones are diffed without the call stack. + if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, detail::recursion_depth_limit()))) { return diff_recursively(source, target, path); } return diff_iteratively(source, target, path); } - JSON_PRIVATE_UNLESS_TESTED: - /// the nesting depth up to which @ref diff recurses - static constexpr std::size_t diff_depth_limit() noexcept - { - return 128; - } - private: /*! @brief whether @a j is nested more than @a limit levels deep @@ -5479,7 +5472,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec basic_json added_ops{}; }; - /// @ref diff for a @a source nested no more than @ref diff_depth_limit levels deep + /// @ref diff for a @a source nested no more than @ref detail::recursion_depth_limit levels deep static basic_json diff_recursively(const basic_json& source, const basic_json& target, const string_t& path) { @@ -5719,7 +5712,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @brief @ref diff without the call stack Produces the same patch as @ref diff_recursively. Only used for a source - nested more deeply than @ref diff_depth_limit; any arrays and objects + nested more deeply than @ref detail::recursion_depth_limit; any arrays and objects below it that are not nested that deeply are diffed recursively. */ static basic_json diff_iteratively(const basic_json& source, const basic_json& target, @@ -5762,7 +5755,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // arrays and objects that are not nested too deeply for the call // stack are diffed recursively, which can skip equal parts - if (!nesting_exceeds(s, diff_depth_limit())) + if (!nesting_exceeds(s, detail::recursion_depth_limit())) { const basic_json partial = diff_recursively(s, t, current_path); result.insert(result.end(), partial.begin(), partial.end()); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3be42f956..937b70cda 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -29745,23 +29745,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // compares them with operator== on the way, which recurses as well, // so values nested deeply enough used to exhaust the call stack. // Both only descend as far as the source is nested, so a source - // nested no more than diff_depth_limit() levels deep - all but a - // vanishing minority - is diffed recursively as before; deeper ones - // are diffed without the call stack. - if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, diff_depth_limit()))) + // nested no more than detail::recursion_depth_limit() levels deep - + // all but a vanishing minority - is diffed recursively as before; + // deeper ones are diffed without the call stack. + if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, detail::recursion_depth_limit()))) { return diff_recursively(source, target, path); } return diff_iteratively(source, target, path); } - JSON_PRIVATE_UNLESS_TESTED: - /// the nesting depth up to which @ref diff recurses - static constexpr std::size_t diff_depth_limit() noexcept - { - return 128; - } - private: /*! @brief whether @a j is nested more than @a limit levels deep @@ -29838,7 +29831,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec basic_json added_ops{}; }; - /// @ref diff for a @a source nested no more than @ref diff_depth_limit levels deep + /// @ref diff for a @a source nested no more than @ref detail::recursion_depth_limit levels deep static basic_json diff_recursively(const basic_json& source, const basic_json& target, const string_t& path) { @@ -30078,7 +30071,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @brief @ref diff without the call stack Produces the same patch as @ref diff_recursively. Only used for a source - nested more deeply than @ref diff_depth_limit; any arrays and objects + nested more deeply than @ref detail::recursion_depth_limit; any arrays and objects below it that are not nested that deeply are diffed recursively. */ static basic_json diff_iteratively(const basic_json& source, const basic_json& target, @@ -30121,7 +30114,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // arrays and objects that are not nested too deeply for the call // stack are diffed recursively, which can skip equal parts - if (!nesting_exceeds(s, diff_depth_limit())) + if (!nesting_exceeds(s, detail::recursion_depth_limit())) { const basic_json partial = diff_recursively(s, t, current_path); result.insert(result.end(), partial.begin(), partial.end()); diff --git a/tests/src/unit-json_patch.cpp b/tests/src/unit-json_patch.cpp index 34ce492df..b57f3820a 100644 --- a/tests/src/unit-json_patch.cpp +++ b/tests/src/unit-json_patch.cpp @@ -1813,7 +1813,7 @@ TEST_CASE("JSON patch: diff of deeply nested values") SECTION("the diff reproduces the target at every depth") { // every depth on either side of the nesting depth up to which diff() - // recurses (basic_json::diff_depth_limit(), 128) + // recurses (detail::recursion_depth_limit(), 128) for (std::size_t depth = 0; depth <= 300; ++depth) { CAPTURE(depth);