diff --git a/src/json.hpp b/src/json.hpp index ff0ce5add..432cf553b 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -324,6 +324,19 @@ struct external_constructor j.m_value.array = j.template create(begin(arr), end(arr)); j.assert_invariant(); } + + template + static void construct(BasicJsonType& j, const std::vector& arr) + { + j.m_type = value_t::array; + j.m_value = value_t::array; + j.m_value.array->reserve(arr.size()); + for (bool x : arr) + { + j.m_value.array->push_back(x); + } + j.assert_invariant(); + } }; template<> @@ -562,6 +575,12 @@ void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept external_constructor::construct(j, e); } +template +void to_json(BasicJsonType& j, std::vector e) noexcept +{ + external_constructor::construct(j, e); +} + template < typename BasicJsonType, typename CompatibleArrayType, enable_if_t < diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index fc7cf9653..21b4fd3ae 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -324,6 +324,19 @@ struct external_constructor j.m_value.array = j.template create(begin(arr), end(arr)); j.assert_invariant(); } + + template + static void construct(BasicJsonType& j, const std::vector& arr) + { + j.m_type = value_t::array; + j.m_value = value_t::array; + j.m_value.array->reserve(arr.size()); + for (bool x : arr) + { + j.m_value.array->push_back(x); + } + j.assert_invariant(); + } }; template<> @@ -562,6 +575,12 @@ void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept external_constructor::construct(j, e); } +template +void to_json(BasicJsonType& j, std::vector e) noexcept +{ + external_constructor::construct(j, e); +} + template < typename BasicJsonType, typename CompatibleArrayType, enable_if_t < diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 7980371bc..bfd2c9547 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -795,4 +795,13 @@ TEST_CASE("regression tests") std::string s2 = j2.dump(); CHECK(s1 == s2); } + + SECTION("issue #494 - conversion from vector to json fails to build") + { + std::vector boolVector = {false, true, false, false}; + json j; + j["bool_vector"] = boolVector; + + CHECK(j["bool_vector"].dump() == "[false,true,false,false]"); + } }