From 9c31d543890ce0b1b5fe5a4bb07465a289cc60ec Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 13 Jun 2022 13:21:55 +0200 Subject: [PATCH] Add to_json() for std::vector::reference (#3534) --- include/nlohmann/detail/conversions/to_json.hpp | 7 +++++++ single_include/nlohmann/json.hpp | 7 +++++++ tests/src/unit-constructor1.cpp | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index fde46ef7c..b59077338 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -272,6 +272,13 @@ inline void to_json(BasicJsonType& j, T b) noexcept external_constructor::construct(j, b); } +template::reference&, typename BasicJsonType::boolean_t>::value, int> = 0> +inline void to_json(BasicJsonType& j, const std::vector::reference& b) noexcept +{ + external_constructor::construct(j, static_cast(b)); +} + template::value, int> = 0> inline void to_json(BasicJsonType& j, const CompatibleString& s) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c5fd3fd0b..4a73ce5d3 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5253,6 +5253,13 @@ inline void to_json(BasicJsonType& j, T b) noexcept external_constructor::construct(j, b); } +template::reference&, typename BasicJsonType::boolean_t>::value, int> = 0> +inline void to_json(BasicJsonType& j, const std::vector::reference& b) noexcept +{ + external_constructor::construct(j, static_cast(b)); +} + template::value, int> = 0> inline void to_json(BasicJsonType& j, const CompatibleString& s) diff --git a/tests/src/unit-constructor1.cpp b/tests/src/unit-constructor1.cpp index 195521226..21f21e380 100644 --- a/tests/src/unit-constructor1.cpp +++ b/tests/src/unit-constructor1.cpp @@ -474,6 +474,13 @@ TEST_CASE("constructors") json j(false); CHECK(j.type() == json::value_t::boolean); } + + SECTION("from std::vector::refrence") + { + std::vector v{true}; + json j(v[0]); + CHECK(j.type() == json::value_t::boolean); + } } SECTION("create a binary (explicit)")