Fix @testjson.cpp so it works with C++11 --- Content from referenced files --- Content…
Private research annotation
Sign in to add a private research label or note.
- User
Fix @test_json.cpp so it works with C++11 --- Content from referenced files --- Content from @C:\routenodes\test_json.cpp: #include #include #include #include #include
#include "routenodes.h" #include "third_party/nlohmann/json.hpp"
using json = nlohmann::json;
// Helper function to parse a string like "x,y" into a CustomPoint CustomPoint parsePoint(const std::string& s) { std::stringstream ss(s); std::string item; float x, y; std::getline(ss, item, ','); x = std::stof(item); std::getline(ss, item, ','); y = std::stof(item); return {x, y}; }
// JSON serialization/deserialization for CustomPoint void to_json(json& j, const CustomPoint& p) { j = json{{"x", p.x}, {"y", p.y}, {"direction", p.direction}}; }
void from_json(const json& j, CustomPoint& p) { if (j.is_string()) { p = parsePoint(j.getstd::string()); } else { j.at("x").get_to(p.x); j.at("y").get_to(p.y); j.at("direction").get_to(p.direction); } }
// JSON serialization/deserialization for CompPartInfo void to_json(json& j, const CompPartInfo& c) { j = json{{"width", c.width}, {"height", c.height}, {"pos", c.pos}}; }
void from_json(const json& j, CompPartInfo& c) { j.at("Width").get_to(c.width); j.at("Height").get_to(c.height); c.pos = parsePoint(j.at("Position").getstd::string()); }
// JSON serialization/deserialization for Path void to_json(json& j, const Path& p) { j = json::array(); for (const auto& point : p) { j.push_back(point); } }
void from_json(const json& j, Path& p) { for (const auto& item : j) { p.push_back(parsePoint(item.getstd::string())); } }
// JSON serialization/deserialization for CompAndCanvasInfo void to_json(json& j, const CompAndCanvasInfo& c) { j = json{{"graphicSceneWidth", c.graphicSceneWidth}, {"graphicSceneHeight", c.graphicSceneHeight}, {"compPartInfos", c.compPartInfos}, {"paths", c.paths}, {"startNode", c.startNode}, {"endNode", c.endNode}}; }
void from_json(const json& j, CompAndCanvasInfo& c) { j.at("Canvas").at("Width").get_to(c.graphicSceneWidth); j.at("Canvas").at("Height").get_to(c.graphicSceneHeight); j.at("Blocks").get_to(c.compPartInfos);
code// The "Lines" in the JSON are a bit different from the Path structure const auto& lines = j.at("Lines"); for (const auto& line_obj : lines) { Path p; const auto& line_points = line_obj.at("Line"); for (const auto& point_str : line_points) { p.push_back(parsePoint(point_str.get<std::string>())); } c.paths.push_back(p); } c.startNode = parsePoint(j.at("StartNode").get<std::string>()); c.endNode = parsePoint(j.at("EndNode").get<std::string>()); Assistant
Okay, I'll fix
test_json.cppto make it compatible with C++11. First, I need to understand the data structures used, so I'll examineroutenodes.h.Assistant
The issue is that
routenodes.huses in-class member initialization, a C++14 feature. To make it C++11 compatible, I'll replace this with constructors. I'll also updatetest_json.cppto explicitly call the new constructor for clarity.