-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot convert from json array to std::array #553
Comments
Not to say that it can't be done, but the hardest parts of this are that Assuming that you know that the size of the std::string r = R"([1, 2, 3, 4])";
auto j = json::parse(r);
std::array<int, 4> arr = {j[0], j[1], j[2], j[3]} but it's probably better to do something like this: std::string r = R"([1, 2, 3, 4])";
std::array<int, 4> arr;
populateArrayFromJsonString(arr, r);
template<typename T, int N> void populateArrayFromJsonString(std::array<T, N> &arr, std::string const &r)
{
auto j = json::parse(r);
// TODO: Check that j is an array of size N
for(int i = 0; i < N; ++i)
{
// TODO check that j[i] is of type T
arr[i] = j.get<T>(i);
}
} (All code here was written here and not compiled or tested.) |
I think it's the user's responsibility to make sure the sizes of json array and std::transform(j.begin(), j.begin() + std::min(N, j.size()), begin(arr), [](const BasicJsonType & i) {
// get<BasicJsonType>() returns *this, this won't call a from_json
// method when value_type is BasicJsonType
return i.template get<typename std::array<T, N>::value_type>();
}); |
I think we should not address this. |
In #625, we address |
With #624, the library now also supports the conversion from/to |
Conversion from
std::array
to json array works well, but not in reverse.clang++ gives compiling errors:
It seems we use
std::inserter
to transform json array tostd::array
, butstd::array
doesn't support insertion.Is it designed so?
It seems unreasonable if we can convert
std::array
to json array but cannot do the same in reverse(and this could be easily supported, isn't it?).My environment:
The text was updated successfully, but these errors were encountered: