From 7a95c5931ff0934adbacddd28555fb2d448c3e48 Mon Sep 17 00:00:00 2001 From: Tom Lachecki Date: Thu, 19 Oct 2023 00:53:29 +0100 Subject: [PATCH 1/3] Ran formatter --- single_include/nlohmann/json.hpp | 42 ---------------------------- single_include/nlohmann/json_fwd.hpp | 1 - tests/src/unit-bjdata.cpp | 14 +++++----- tests/src/unit-cbor.cpp | 14 +++++----- tests/src/unit-msgpack.cpp | 10 +++---- 5 files changed, 19 insertions(+), 62 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ac4a1d08b..1abc13b89 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -41,7 +41,6 @@ // SPDX-License-Identifier: MIT - #include // #include @@ -54,7 +53,6 @@ // SPDX-License-Identifier: MIT - // This file contains all macro definitions affecting or depending on the ABI #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK @@ -156,7 +154,6 @@ // SPDX-License-Identifier: MIT - #include // transform #include // array #include // forward_list @@ -179,7 +176,6 @@ // SPDX-License-Identifier: MIT - #include // nullptr_t #include // exception #if JSON_DIAGNOSTICS @@ -199,7 +195,6 @@ // SPDX-License-Identifier: MIT - #include // array #include // size_t #include // uint8_t @@ -215,7 +210,6 @@ // SPDX-License-Identifier: MIT - #include // declval, pair // #include // __ _____ _____ _____ @@ -227,7 +221,6 @@ // SPDX-License-Identifier: MIT - #include // #include @@ -240,7 +233,6 @@ // SPDX-License-Identifier: MIT - // #include @@ -2940,7 +2932,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - // #include @@ -3015,7 +3006,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // size_t // #include @@ -3058,7 +3048,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // array #include // size_t #include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type @@ -3231,7 +3220,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // numeric_limits #include // false_type, is_constructible, is_integral, is_same, true_type #include // declval @@ -3247,7 +3235,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // random_access_iterator_tag // #include @@ -3315,7 +3302,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - // #include @@ -3335,7 +3321,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - // #include @@ -4153,7 +4138,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // strlen #include // string #include // forward @@ -4539,7 +4523,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - // #include @@ -4563,7 +4546,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - // #include @@ -5069,7 +5051,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // copy #include // begin, end #include // string @@ -5089,7 +5070,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // size_t #include // input_iterator_tag #include // string, to_string @@ -5810,7 +5790,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // uint8_t, uint64_t #include // tie #include // move @@ -5922,7 +5901,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // uint8_t #include // size_t #include // hash @@ -6055,7 +6033,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // generate_n #include // array #include // ldexp @@ -6081,7 +6058,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // array #include // size_t #include // strlen @@ -6576,7 +6552,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include #include // string #include // move @@ -7308,7 +7283,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // array #include // localeconv #include // size_t @@ -8947,7 +8921,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // size_t #include // declval #include // string @@ -12099,7 +12072,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // isfinite #include // uint8_t #include // function @@ -12616,7 +12588,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - // #include // #include @@ -12629,7 +12600,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // ptrdiff_t #include // numeric_limits @@ -12788,7 +12758,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const @@ -13550,7 +13519,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // ptrdiff_t #include // reverse_iterator #include // declval @@ -13719,7 +13687,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // all_of #include // isdigit #include // errno, ERANGE @@ -14714,7 +14681,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include #include @@ -14806,7 +14772,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // reverse #include // array #include // map @@ -14832,7 +14797,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // copy #include // size_t #include // back_inserter @@ -16801,7 +16765,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // reverse, remove, fill, find, none_of #include // array #include // localeconv, lconv @@ -16826,7 +16789,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // array #include // signbit, isfinite #include // intN_t, uintN_t @@ -18921,7 +18883,6 @@ NLOHMANN_JSON_NAMESPACE_END // SPDX-License-Identifier: MIT - #include // equal_to, less #include // initializer_list #include // input_iterator_tag, iterator_traits @@ -24477,7 +24438,6 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC // SPDX-License-Identifier: MIT - // restore clang diagnostic settings #if defined(__clang__) #pragma clang diagnostic pop @@ -24522,7 +24482,6 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC // SPDX-License-Identifier: MIT - #undef JSON_HEDLEY_ALWAYS_INLINE #undef JSON_HEDLEY_ARM_VERSION #undef JSON_HEDLEY_ARM_VERSION_CHECK @@ -24673,5 +24632,4 @@ inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC #undef JSON_HEDLEY_FALL_THROUGH - #endif // INCLUDE_NLOHMANN_JSON_HPP_ diff --git a/single_include/nlohmann/json_fwd.hpp b/single_include/nlohmann/json_fwd.hpp index 98070dfc9..7a0c8a82a 100644 --- a/single_include/nlohmann/json_fwd.hpp +++ b/single_include/nlohmann/json_fwd.hpp @@ -25,7 +25,6 @@ // SPDX-License-Identifier: MIT - // This file contains all macro definitions affecting or depending on the ABI #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK diff --git a/tests/src/unit-bjdata.cpp b/tests/src/unit-bjdata.cpp index 919ddf84f..1aa3c22b0 100644 --- a/tests/src/unit-bjdata.cpp +++ b/tests/src/unit-bjdata.cpp @@ -339,13 +339,13 @@ TEST_CASE("BJData") std::vector const numbers { -32769, - -100000, - -1000000, - -10000000, - -100000000, - -1000000000, - -2147483647 - 1, // https://stackoverflow.com/a/29356002/266378 - }; + -100000, + -1000000, + -10000000, + -100000000, + -1000000000, + -2147483647 - 1, // https://stackoverflow.com/a/29356002/266378 + }; for (const auto i : numbers) { CAPTURE(i) diff --git a/tests/src/unit-cbor.cpp b/tests/src/unit-cbor.cpp index 0bf33e13d..9e9f999ae 100644 --- a/tests/src/unit-cbor.cpp +++ b/tests/src/unit-cbor.cpp @@ -241,13 +241,13 @@ TEST_CASE("CBOR") const std::vector numbers { -65537, - -100000, - -1000000, - -10000000, - -100000000, - -1000000000, - -4294967296, - }; + -100000, + -1000000, + -10000000, + -100000000, + -1000000000, + -4294967296, + }; for (const auto i : numbers) { CAPTURE(i) diff --git a/tests/src/unit-msgpack.cpp b/tests/src/unit-msgpack.cpp index ff4a25f4f..1686cc4a5 100644 --- a/tests/src/unit-msgpack.cpp +++ b/tests/src/unit-msgpack.cpp @@ -479,11 +479,11 @@ TEST_CASE("MessagePack") std::vector const numbers { -32769, - -65536, - -77777, - -1048576, - -2147483648LL, - }; + -65536, + -77777, + -1048576, + -2147483648LL, + }; for (auto i : numbers) { CAPTURE(i) From 356489659c10ee24cbdc585c92db4490f90aab2e Mon Sep 17 00:00:00 2001 From: Tom Lachecki Date: Wed, 6 Sep 2023 14:48:18 +0100 Subject: [PATCH 2/3] Fixed basic_json::diff and basic_json::patch not compiling with custom string type (#4134) --- include/nlohmann/json.hpp | 39 ++++++++++++++++++---------- single_include/nlohmann/json.hpp | 39 ++++++++++++++++++---------- tests/src/unit-alt-string.cpp | 44 +++++++++++++++++++++++++++++--- 3 files changed, 93 insertions(+), 29 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index f7fac1fb4..b6fb32db4 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -4702,7 +4702,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // the valid JSON Patch operations enum class patch_operations {add, remove, replace, move, copy, test, invalid}; - const auto get_op = [](const std::string & op) + const auto get_op = [](const StringType & op) { if (op == "add") { @@ -4839,8 +4839,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (const auto& val : json_patch) { // wrapper to get a value for an operation - const auto get_value = [&val](const std::string & op, - const std::string & member, + const auto get_value = [&val](const StringType & op, + const StringType & member, bool string_type) -> basic_json & { // find value @@ -4874,8 +4874,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // collect mandatory members - const auto op = get_value("op", "op", true).template get(); - const auto path = get_value(op, "path", true).template get(); + const auto op = get_value("op", "op", true).template get(); + const auto path = get_value(op, "path", true).template get(); json_pointer ptr(path); switch (get_op(op)) @@ -4901,7 +4901,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case patch_operations::move: { - const auto from_path = get_value("move", "from", true).template get(); + const auto from_path = get_value("move", "from", true).template get(); json_pointer from_ptr(from_path); // the "from" location must exist - use at() @@ -4918,7 +4918,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case patch_operations::copy: { - const auto from_path = get_value("copy", "from", true).template get(); + const auto from_path = get_value("copy", "from", true).template get(); const json_pointer from_ptr(from_path); // the "from" location must exist - use at() @@ -4978,7 +4978,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @sa https://json.nlohmann.me/api/basic_json/diff/ JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json diff(const basic_json& source, const basic_json& target, - const std::string& path = "") + const StringType& path = "") { // the patch basic_json result(value_t::array); @@ -5007,8 +5007,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec std::size_t i = 0; while (i < source.size() && i < target.size()) { + StringType array_index_str; + { + using namespace detail; + int_to_string(array_index_str, i); + } + // recursive call to compare array values at index i - auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i))); + auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', array_index_str)); result.insert(result.end(), temp_diff.begin(), temp_diff.end()); ++i; } @@ -5022,10 +5028,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec { // add operations in reverse order to avoid invalid // indices + + StringType array_index_str; + { + using namespace detail; + int_to_string(array_index_str, i); + } + result.insert(result.begin() + end_index, object( { {"op", "remove"}, - {"path", detail::concat(path, '/', std::to_string(i))} + {"path", detail::concat(path, '/', array_index_str)} })); ++i; } @@ -5036,7 +5049,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec result.push_back( { {"op", "add"}, - {"path", detail::concat(path, "/-")}, + {"path", detail::concat(path, "/-")}, {"value", target[i]} }); ++i; @@ -5051,7 +5064,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto path_key = detail::concat(path, '/', detail::escape(it.key())); + const auto path_key = detail::concat(path, '/', detail::escape(it.key())); if (target.find(it.key()) != target.end()) { @@ -5075,7 +5088,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto path_key = detail::concat(path, '/', detail::escape(it.key())); + const auto path_key = detail::concat(path, '/', detail::escape(it.key())); result.push_back( { {"op", "add"}, {"path", path_key}, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1abc13b89..59b6c7738 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -23877,7 +23877,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // the valid JSON Patch operations enum class patch_operations {add, remove, replace, move, copy, test, invalid}; - const auto get_op = [](const std::string & op) + const auto get_op = [](const StringType & op) { if (op == "add") { @@ -24014,8 +24014,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (const auto& val : json_patch) { // wrapper to get a value for an operation - const auto get_value = [&val](const std::string & op, - const std::string & member, + const auto get_value = [&val](const StringType & op, + const StringType & member, bool string_type) -> basic_json & { // find value @@ -24049,8 +24049,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // collect mandatory members - const auto op = get_value("op", "op", true).template get(); - const auto path = get_value(op, "path", true).template get(); + const auto op = get_value("op", "op", true).template get(); + const auto path = get_value(op, "path", true).template get(); json_pointer ptr(path); switch (get_op(op)) @@ -24076,7 +24076,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case patch_operations::move: { - const auto from_path = get_value("move", "from", true).template get(); + const auto from_path = get_value("move", "from", true).template get(); json_pointer from_ptr(from_path); // the "from" location must exist - use at() @@ -24093,7 +24093,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec case patch_operations::copy: { - const auto from_path = get_value("copy", "from", true).template get(); + const auto from_path = get_value("copy", "from", true).template get(); const json_pointer from_ptr(from_path); // the "from" location must exist - use at() @@ -24153,7 +24153,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @sa https://json.nlohmann.me/api/basic_json/diff/ JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json diff(const basic_json& source, const basic_json& target, - const std::string& path = "") + const StringType& path = "") { // the patch basic_json result(value_t::array); @@ -24182,8 +24182,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec std::size_t i = 0; while (i < source.size() && i < target.size()) { + StringType array_index_str; + { + using namespace detail; + int_to_string(array_index_str, i); + } + // recursive call to compare array values at index i - auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', std::to_string(i))); + auto temp_diff = diff(source[i], target[i], detail::concat(path, '/', array_index_str)); result.insert(result.end(), temp_diff.begin(), temp_diff.end()); ++i; } @@ -24197,10 +24203,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec { // add operations in reverse order to avoid invalid // indices + + StringType array_index_str; + { + using namespace detail; + int_to_string(array_index_str, i); + } + result.insert(result.begin() + end_index, object( { {"op", "remove"}, - {"path", detail::concat(path, '/', std::to_string(i))} + {"path", detail::concat(path, '/', array_index_str)} })); ++i; } @@ -24211,7 +24224,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec result.push_back( { {"op", "add"}, - {"path", detail::concat(path, "/-")}, + {"path", detail::concat(path, "/-")}, {"value", target[i]} }); ++i; @@ -24226,7 +24239,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto path_key = detail::concat(path, '/', detail::escape(it.key())); + const auto path_key = detail::concat(path, '/', detail::escape(it.key())); if (target.find(it.key()) != target.end()) { @@ -24250,7 +24263,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto path_key = detail::concat(path, '/', detail::escape(it.key())); + const auto path_key = detail::concat(path, '/', detail::escape(it.key())); result.push_back( { {"op", "add"}, {"path", path_key}, diff --git a/tests/src/unit-alt-string.cpp b/tests/src/unit-alt-string.cpp index 3d6b0704f..4b3932244 100644 --- a/tests/src/unit-alt-string.cpp +++ b/tests/src/unit-alt-string.cpp @@ -35,10 +35,33 @@ class alt_string alt_string(size_t count, char chr): str_impl(count, chr) {} alt_string() = default; - template - alt_string& append(TParams&& ...params) + alt_string& append(std::size_t count, char ch) { - str_impl.append(std::forward(params)...); + str_impl.append(count, ch); + return *this; + } + + alt_string& append(const alt_string& str) + { + str_impl.append(str.str_impl); + return *this; + } + + alt_string& append(const char* s, std::size_t count) + { + str_impl.append(s, count); + return *this; + } + + alt_string& append(const char* s) + { + str_impl.append(s); + return *this; + } + + alt_string& operator+=(char ch) + { + str_impl += ch; return *this; } @@ -74,6 +97,11 @@ class alt_string return str_impl.size(); } + void reserve (std::size_t n) + { + str_impl.reserve(n); + } + void resize (std::size_t n) { str_impl.resize(n); @@ -319,4 +347,14 @@ TEST_CASE("alternative string type") CHECK(j.at(alt_json::json_pointer("/foo/0")) == j["foo"][0]); CHECK(j.at(alt_json::json_pointer("/foo/1")) == j["foo"][1]); } + + SECTION("JSON patch") + { + // for now, just ensures successful compilation (see #4134) + alt_json base, target; + alt_json patch = alt_json::array(); + + base.patch(patch); + CHECK_NOTHROW(alt_json::diff(target, base)); + } } From 96129bd96d4929aa9818641f6f9b3b77da5379fa Mon Sep 17 00:00:00 2001 From: Tom Lachecki Date: Thu, 19 Oct 2023 00:53:36 +0100 Subject: [PATCH 3/3] Fixed json_pointer::to_string() failing to compile with custom string type --- include/nlohmann/detail/json_pointer.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- tests/src/unit-alt-string.cpp | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index eb712ed91..c5acabd10 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -71,7 +71,7 @@ class json_pointer string_t{}, [](const string_t& a, const string_t& b) { - return detail::concat(a, '/', detail::escape(b)); + return detail::concat(a, '/', detail::escape(b)); }); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 59b6c7738..6d8152fd2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -13755,7 +13755,7 @@ class json_pointer string_t{}, [](const string_t& a, const string_t& b) { - return detail::concat(a, '/', detail::escape(b)); + return detail::concat(a, '/', detail::escape(b)); }); } diff --git a/tests/src/unit-alt-string.cpp b/tests/src/unit-alt-string.cpp index 4b3932244..492620ab6 100644 --- a/tests/src/unit-alt-string.cpp +++ b/tests/src/unit-alt-string.cpp @@ -346,6 +346,9 @@ TEST_CASE("alternative string type") CHECK(j.at(alt_json::json_pointer("/foo/0")) == j["foo"][0]); CHECK(j.at(alt_json::json_pointer("/foo/1")) == j["foo"][1]); + + // ensures successful compilation + CHECK(alt_json::json_pointer("/foo/0").to_string() == "/foo/0"); } SECTION("JSON patch")