✨ added overload for std::vector<bool> #494
Adds a to_json function for std::vector<bool> to allow implicit conversion from bit vectors to basic_json.
This commit is contained in:
parent
758c4addc1
commit
f5f6dac800
3 changed files with 47 additions and 0 deletions
19
src/json.hpp
19
src/json.hpp
|
@ -324,6 +324,19 @@ struct external_constructor<value_t::array>
|
||||||
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename BasicJsonType>
|
||||||
|
static void construct(BasicJsonType& j, const std::vector<bool>& 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<>
|
template<>
|
||||||
|
@ -562,6 +575,12 @@ void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept
|
||||||
external_constructor<value_t::number_integer>::construct(j, e);
|
external_constructor<value_t::number_integer>::construct(j, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename BasicJsonType>
|
||||||
|
void to_json(BasicJsonType& j, std::vector<bool> e) noexcept
|
||||||
|
{
|
||||||
|
external_constructor<value_t::array>::construct(j, e);
|
||||||
|
}
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename BasicJsonType, typename CompatibleArrayType,
|
typename BasicJsonType, typename CompatibleArrayType,
|
||||||
enable_if_t <
|
enable_if_t <
|
||||||
|
|
|
@ -324,6 +324,19 @@ struct external_constructor<value_t::array>
|
||||||
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename BasicJsonType>
|
||||||
|
static void construct(BasicJsonType& j, const std::vector<bool>& 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<>
|
template<>
|
||||||
|
@ -562,6 +575,12 @@ void to_json(BasicJsonType& j, UnscopedEnumType e) noexcept
|
||||||
external_constructor<value_t::number_integer>::construct(j, e);
|
external_constructor<value_t::number_integer>::construct(j, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename BasicJsonType>
|
||||||
|
void to_json(BasicJsonType& j, std::vector<bool> e) noexcept
|
||||||
|
{
|
||||||
|
external_constructor<value_t::array>::construct(j, e);
|
||||||
|
}
|
||||||
|
|
||||||
template <
|
template <
|
||||||
typename BasicJsonType, typename CompatibleArrayType,
|
typename BasicJsonType, typename CompatibleArrayType,
|
||||||
enable_if_t <
|
enable_if_t <
|
||||||
|
|
|
@ -795,4 +795,13 @@ TEST_CASE("regression tests")
|
||||||
std::string s2 = j2.dump();
|
std::string s2 = j2.dump();
|
||||||
CHECK(s1 == s2);
|
CHECK(s1 == s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("issue #494 - conversion from vector<bool> to json fails to build")
|
||||||
|
{
|
||||||
|
std::vector<bool> boolVector = {false, true, false, false};
|
||||||
|
json j;
|
||||||
|
j["bool_vector"] = boolVector;
|
||||||
|
|
||||||
|
CHECK(j["bool_vector"].dump() == "[false,true,false,false]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue