Is there any problem in @routenodescli.cpp ? --- Content from referenced files ---…
Private research annotation
Sign in to add a private research label or note.
- User
Is there any problem in @routenodescli.cpp ? --- Content from referenced files --- Content from @C:\routenodes\routenodescli.cpp: #include #include #include #include #include
#include "routenodes.hpp" #include "third_party/nlohmann/json.hpp"
using json = nlohmann::ordered_json;
namespace routenodes {
// CustomPoint序列化 void to_json(json& j, const CustomPoint& p) { std::ostringstream ss; ss << p.x << "," << p.y; j = ss.str(); }
void from_json(const json& j, CustomPoint& p) { if (j.is_string()) { // 解析"x,y" float x, y; char comma; std::istringstream ss(j.getstd::string()); ss >> x >> comma >> y; p.x = x; p.y = y; } else { // 不但读取x y,也读取direction j.at("x").get_to(p.x); j.at("y").get_to(p.y); j.at("direction").get_to(p.direction); } }
// CompPartInfo序列化 void to_json(json& j, const CompPartInfo& c) { if (!c.blockType.empty()) { j["BlockType"] = c.blockType; }
codej["Width"] = c.width; j["Height"] = c.height; j["Position"] = c.pos;}
void from_json(const json& j, CompPartInfo& c) { if (j.contains("BlockType")) { j.at("BlockType").get_to(c.blockType); }
codej.at("Width").get_to(c.width); j.at("Height").get_to(c.height); j.at("Position").get_to(c.pos);}
// CompAndCanvasInfo序列化 void to_json(json& j, const CompAndCanvasInfo& c) { j["Canvas"] = {{"Width", c.graphicSceneWidth}, {"Height", c.graphicSceneHeight}}; j["Blocks"] = c.compPartInfos;
codeif (!c.paths.empty()) { j["Lines"] = json::array(); for (const auto& path : c.paths) { j["Lines"].push_back(json{{"Line", path}}); } } j["StartNode"] = c.startNode; j["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);
codeif (j.contains("Lines")) { for (const auto& line : j.at("Lines")) { c.paths.push_back(line.at("Line")); } } j.at("StartNode").get_to(c.startNode); j.at("EndNode").get_to(c.endNode); Assistant
Yes, there is a potential data loss issue in
routenodescli.cppin the wayCustomPointobjects are handled.The
from_jsonfunction forCustomPointis capable of reading a JSON object that containsx,y, anddirectionfields. This is likely used to deserialize theStartNodeandEndNodefrom the input file.cpp// routenodescli.cpp: from_json for CustomPoint void from_json(const json& j, CustomPoint& p) { if (j.is_string()) { // ... parses "x,y" } else { // Reads x, y, and direction j.at("x").get_to(p.x); j.at("y").get_to(p.y); j.at("direction").get_to(p.direction); } }