From bdb8d2b7b3e8fdf1894ab1f57f01a7fbb7c21890 Mon Sep 17 00:00:00 2001 From: Michael Valladolid Date: Sun, 19 Jan 2025 04:34:11 +0900 Subject: [PATCH] Serialize empty tuple into '[]' instead of null (#4594) Signed-off-by: Michael Valladolid --- include/nlohmann/detail/conversions/from_json.hpp | 6 ++++++ include/nlohmann/detail/conversions/to_json.hpp | 7 +++++++ single_include/nlohmann/json.hpp | 13 +++++++++++++ tests/src/unit-regression2.cpp | 9 +++++++++ 4 files changed, 35 insertions(+) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 362383a9f..da0d99d4d 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -448,6 +448,12 @@ std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence< return std::make_tuple(std::forward(j).at(Idx).template get()...); } +template +std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/) +{ + return {}; +} + template < typename BasicJsonType, class A1, class A2 > std::pair from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index ec20cd7d6..c7d2f9558 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -426,6 +426,13 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence< j = { std::get(t)... }; } +template +inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/) +{ + using array_t = typename BasicJsonType::array_t; + j = array_t(); +} + template::value, int > = 0> inline void to_json(BasicJsonType& j, const T& t) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f0063fe8e..9339c7b28 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5190,6 +5190,12 @@ std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence< return std::make_tuple(std::forward(j).at(Idx).template get()...); } +template +std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/) +{ + return {}; +} + template < typename BasicJsonType, class A1, class A2 > std::pair from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { @@ -6019,6 +6025,13 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence< j = { std::get(t)... }; } +template +inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/) +{ + using array_t = typename BasicJsonType::array_t; + j = array_t(); +} + template::value, int > = 0> inline void to_json(BasicJsonType& j, const T& t) { diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 88a64a7b4..30c2a7881 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -814,6 +814,15 @@ TEST_CASE("regression tests 2") } } + SECTION("issue #4530 - Serialization of empty tuple") + { + const auto source_tuple = std::tuple<>(); + const nlohmann::json j = source_tuple; + + CHECK(j.get() == source_tuple); + CHECK("[]" == j.dump()); + } + SECTION("issue #2865 - ASAN detects memory leaks") { // the code below is expected to not leak memory