mirror of
https://github.com/nlohmann/json
synced 2024-11-23 20:40:08 +00:00
add to_json method for C arrays
If the array can be converted to basic_json::string_t, the overload in this commit is not chosen.
This commit is contained in:
parent
100bf3ef2c
commit
dbebf8de47
1521
src/json.hpp
1521
src/json.hpp
File diff suppressed because it is too large
Load Diff
@ -800,6 +800,13 @@ void to_json(BasicJsonType& j, const CompatibleObjectType& arr)
|
||||
external_constructor<value_t::object>::construct(j, arr);
|
||||
}
|
||||
|
||||
template <typename BasicJsonType, typename T, std::size_t N,
|
||||
enable_if_t<not std::is_constructible<
|
||||
typename BasicJsonType::string_t, T (&)[N]>::value,
|
||||
int> = 0>
|
||||
void to_json(BasicJsonType &j, T (&arr)[N]) {
|
||||
external_constructor<value_t::array>::construct(j, arr);
|
||||
}
|
||||
|
||||
///////////////
|
||||
// from_json //
|
||||
|
@ -190,6 +190,20 @@ TEST_CASE("value conversion")
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("built-in arrays")
|
||||
{
|
||||
const char str[] = "a string";
|
||||
const int nbs[] = {0, 1, 2};
|
||||
|
||||
json j2 = nbs;
|
||||
json j3 = str;
|
||||
|
||||
auto v = j2.get<std::vector<int>>();
|
||||
auto s = j3.get<std::string>();
|
||||
CHECK(std::equal(v.begin(), v.end(), std::begin(nbs)));
|
||||
CHECK(s == str);
|
||||
}
|
||||
|
||||
SECTION("std::deque<json>")
|
||||
{
|
||||
std::deque<json> a = j.get<std::deque<json>>();
|
||||
|
Loading…
Reference in New Issue
Block a user