Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Vizualising the json of C++ graph view is failing #228

Closed
arjunarjun07 opened this issue Jul 20, 2024 · 5 comments
Closed

Error: Vizualising the json of C++ graph view is failing #228

arjunarjun07 opened this issue Jul 20, 2024 · 5 comments

Comments

@arjunarjun07
Copy link

arjunarjun07 commented Jul 20, 2024

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:
image

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

@arjunarjun07
Copy link
Author

@hediet Kindly share your views on json parsing by Debug adapter: cppdebug for the above.

@arjunarjun07
Copy link
Author

UPDATE:

The vizualisation is broken when the json is big. I tried to visualize for small heap it is working fine.

std::vector<int> heap = {10, 20, 30};

"{\"edges\":[{\"from\":\"0\",\"to\":\"1\"},{\"from\":\"0\",\"to\":\"2\"}],\"kind\":{\"graph\":true},\"nodes\":[{\"id\":\"0\",\"label\":\"10\"},{\"id\":\"1\",\"label\":\"20\"},{\"id\":\"2\",\"label\":\"30\"}]}"

Looks like the plugin json parser is failing somehow for bigger json.

@arjunarjun07
Copy link
Author

arjunarjun07 commented Jul 25, 2024

Found a solution for this. from this comment
#53 (comment)

Instead of running this commands everytime manually in the debug console, I created a .gdbinit file with the following and it should be in the following location. %USERPROFILE%\.gdbinit

set print elements 0
set print repeats 0

@hediet
Copy link
Owner

hediet commented Jul 25, 2024

Thanks for posting the solution!

Because this extension is debugger-independent, it relies on the debugger returning the full string content when requesting an evaluation of a string variable/expression. In this case, the debugger had a limit where it would cut off the string (probably in the middle, which is difficult to spot in the initial screenshot). In other cases, the string is escaped in unexpected ways (there are already some double-escaping heuristics in place).

I'm not sure how this can be improved.

@arjunarjun07
Copy link
Author

Thanks for making this great plugin. This really makes the debugging interactive and lively. I hope more contributors will jump in to expand the support for the C++ community. @hediet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants