Closed
Description
I am trying to visualize a graph view of a heap using a cpp std::vector. Following is the code snippet:
std::string serialize_as_heap(const std::vector<int>& heap) {
if (heap.empty()) {
return json().dump();
}
json serialized = {
{"kind", {{"graph", true}}},
{"nodes", json::array()},
{"edges", json::array()}
};
for (size_t i = 0; i < heap.size(); ++i) {
serialized["nodes"].push_back({{"id", std::to_string(i)}, {"label", std::to_string(heap[i])}});
}
for (size_t i = 0; i < heap.size(); ++i) {
size_t left_child_index = 2 * i + 1;
size_t right_child_index = 2 * i + 2;
if (left_child_index < heap.size()) {
serialized["edges"].push_back({{"from", std::to_string(i)}, {"to", std::to_string(left_child_index)}});
}
if (right_child_index < heap.size()) {
serialized["edges"].push_back({{"from", std::to_string(i)}, {"to", std::to_string(right_child_index)}});
}
}
return serialized.dump();
}
int main() {
auto res = std::string("");
std::vector<int> heap = {10, 20, 30, 40, 50, 60, 70};
res = serialize_as_heap(heap); //Passed "res" to the DebugVisualizer textbox
return 0;
}
res value: //Escaped json given by cpp debugger
"{\"edges\":[{\"from\":\"0\",\"to\":\"1\"},{\"from\":\"0\",\"to\":\"2\"},{\"from\":\"1\",\"to\":\"3\"},{\"from\":\"1\",\"to\":\"4\"},{\"from\":\"2\",\"to\":\"5\"},{\"from\":\"2\",\"to\":\"6\"}],\"kind\":{\"graph\":true},\"nodes\":[{\"id\":\"0\",\"label\":\"10\"},{\"id\":\"1\",\"label\":\"20\"},{\"id\":\"2\",\"label\":\"30\"},{\"id\":\"3\",\"label\":\"40\"},{\"id\":\"4\",\"label\":\"50\"},{\"id\":\"5\",\"label\":\"60\"},{\"id\":\"6\",\"label\":\"70\"}]}"
It is showing the following error:
P.S: I tried to unescape the above json and tested the result in the playground. It is working fine
https://hediet.github.io/visualization/?darkTheme=1
Environment details:
vscode: 1.91.1
cppstd: 20
compiler details:
g++ --version
g++.exe (Rev6, Built by MSYS2 project) 13.2.0
Metadata
Metadata
Assignees
Labels
No labels