Replies: 2 comments
-
void Dimensions(const json& j, std::vector<int> &v)
{
int n = 0;
json tmp;
for(auto it : j)
{
n++;
tmp = it;
}
if(tmp.is_array())
{
Dimensions(tmp, v);
}
v.push_back(n);
}
template<typename T>
void ProcData(const json& j, std::vector<T> &v)
{
for(auto it : j)
{
if(it.is_array())
{
ProcData(it, v);
}else
{
T tmp = it.get<T>();
v.push_back(tmp);
}
}
}
template<typename T>
void PrcoArray(T value);
template<typename T>
void PrcoElement(T value, const json& j, void *c, int size) {
std::cout << typeid(value).name() << ' ' << value << std::endl;
std::vector<T> data;
ProcData(j, data);
memcpy(c,data.data(), size);
}
template<typename T>
void PrcoArray(T value, const json& j, void *c, int size) {
PrcoElement(value, j,c, size);
}
template<typename T>
void PrcoArray(T arr[], const json& j, void *c, int size) {
PrcoArray(arr[0], j,c, size);
}
namespace nlohmann
{
template<typename T>
void from_json(const json& j, T *c) {
int size = 1;
std::vector<int> Dim;
Dimensions(j, Dim);
for(auto it : Dim)
{
size *= it;
}
PrcoArray(c, j,c,size);
}
} This solves the problem |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
nlohmann
-
But the above code will cause one-dimensional situation and cannot be used |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
double array[2][2] = {0}; json f = array; f.get_to(sssss);
It will report an error:
main.cpp:11:7: error: no matching member function for call to 'get_to'
json.hpp:1798:17: note: candidate template ignored: requirement 'detail::has_from_json<nlohmann::json_abi_v3_11_3::basic_json<map, vector, std::__cxx11::basic_string, bool, long, unsigned long, double, allocator, adl_serializer, std::vector<unsigned char, std::allocator >, void>, double [2][2], void>::value' was not satisfied [with ValueType = double [2][2]]
json.hpp:1811:17: note: candidate template ignored: requirement 'detail::is_basic_json<double [2][2]>::value' was not satisfied [with ValueType = double [2][2]]
json.hpp:1822:11: note: candidate template ignored: requirement 'detail::has_from_json<nlohmann::json_abi_v3_11_3::basic_json<map, vector, std::__cxx11::basic_string, bool, long, unsigned long, double, allocator, adl_serializer, std::vector<unsigned char, std::allocator >, void>, double (&)[2][2], void>::value' was not satisfied [with T = double [2], N = 2, Array = double (&)[2][2]]
How to solve THANKS!
Beta Was this translation helpful? Give feedback.
All reactions