New to using json with C++. Need to read json files of a certain format and get the data into C++ structures. I can do it with simple types like I find in documentation, but with a type I've defined, I'm doing something wrong.
Why does this not work? It crashes in the spot noted in my test program.
struct equupoly
{
std::string name;
std::vector<int> all_points_x;
std::vector<int> all_points_y;
};
void to_json(json& j, const equupoly& p)
{
j = json{ {"name", p.name}, {"all_points_x", p.all_points_x}, {"all_points_y", p.all_points_y} };
}
void from_json(const json& j, equupoly& p)
{
j.at("name").get_to(p.name);
j.at("all_points_x").get_to(p.all_points_x);
j.at("all_points_y").get_to(p.all_points_y);
}
class polyfinding
{
public:
polyfinding()
{}
polyfinding(equupoly p, std::string f)
{
poly = p;
Finding = f;
}
equupoly poly;
std::string Finding;
};
void to_json(json& j, const polyfinding& p)
{
j = json{ {"poly", p.poly}, {"Finding", p.Finding} };
}
void from_json(const json& j, polyfinding& p)
{
j.at("poly").get_to(p.poly);
j.at("Finding").get_to(p.Finding);
}
json ReadJSONfile(CString pn)
{
std::ifstream i(pn);
json j;
i >> j;
return j;
}
void MyTestRoutine()
{
json j = ReadJSONfile("C:\\test.json");
polyfinding TheData;
from_json(j, TheData); // crashes here
}
The test file contains:
[{"all_points_x":[1558,1551,1470,1432,1555],"all_points_y":[1337,1478,1474,1390,1340],"name":"polygon"},{"Findings":"Fragment"}]