Home > Enterprise >  Using structured C type with nlohmann/JSON
Using structured C type with nlohmann/JSON

Time:06-10

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"}]

CodePudding user response:

It crashes in line
j.at("poly").get_to(p.poly); Your test file contains array of 2 objects: {"all_points_x":[1558,1551,1470,1432,1555],"all_points_y":[1337,1478,1474,1390,1340],"name":"polygon"} and {"Findings":"Fragment"}. What do you excpect when trying to execute j.at("poly")? In that case u can use, for example, j[0].get_to(p.poly). And be careful, in your code variable name is Finding, but in json field name Findings

CodePudding user response:

Changed my code to this.....


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);
}


struct findings
{
    std::string Findings;
};

void to_json(json& j, const findings& p)
{
    j = json{ {"Findings", p.Findings} };
}

void from_json(const json& j, findings& p)
{
    j.at("Findings").get_to(p.Findings);
}


class polyfinding
{
public:

    polyfinding()
    {}

    polyfinding(equupoly p, findings f)
    {
        poly = p;
        found = f;
    }

    equupoly poly;
    findings found;
};

void to_json(json& j, const polyfinding& p) 
{
    j = json{ {"poly", p.poly}, {"found", p.found} };
}

void from_json(const json& j, polyfinding& p) 
{
    j[0].get_to(p.poly);             
    j[1].get_to(p.found);  
}

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);   // no more crash here
}

Thank you to cycxyz for helpful comment!

  • Related