hediet / vscode-debug-visualizer

An extension for VS Code that visualizes data during debugging.
https://marketplace.visualstudio.com/items?itemName=hediet.debug-visualizer
GNU General Public License v3.0
7.93k stars 413 forks source link

Java visualization: No rendering of a JSON tree #142

Closed Klummel69 closed 9 months ago

Klummel69 commented 2 years ago

Hello

I have used the VS Code Debug Visualizer a few times in python. It's a nice tool that helps well when debugging on nodes and edges. However, when using it with java I have a problem. It seems like the extention is not able to resolve a JSON string. I have tried different variations, the visualizer tries to resolve the Java data structure instead of the string content. If I use the string in the "Visualization Playground" it works.

public class TestJson {
    public static void main(String[] args) {
        String x = "{'kind':{'graph':true},'nodes':[{'id':'1'},{'id':'2'},{'id':'3'}],'edges':[{'from':'1','to':'2'},{'from':'2','to':'3'}]}";
        String y = x.replace('\'', '"'); // Helps to avoid the escape jungle of double quotes
        System.out.println(y);
    }      
}

Below are 2 examples. (The string is extra short, that there are no problems with a possible truncation of the text).

Expression: y or Expression: y.toString()

Debug Visualizer, Output in JSON Source:

{
    "kind": {
        "graph": true
    },
    "nodes": [
        {
            "id": "7",
            "label": "\"{\"kind\":{\"graph\":true},\"nodes\":[{\"id\":\"1\"},{\"id\":\"2\"},{\"id\":\"3\"}],\"edges\":[{\"from\":\"1\",\"to\":\"2\"},{\"from\":\"2\",\"to\":\"3\"}]}\"",
            "color": "lightblue",
            "shape": "box"
        },
        ...

(Sometimes the "ID" changes, the "label" text remains the same.)

Expression: y.toCharArray()

Debug Visualizer, Output in JSON Source:

{
    "kind": {
        "graph": true
    },
    "nodes": [
        {
            "id": "14",
            "label": "char[120]@30",
            "color": "lightblue",
            "shape": "box"
        },
        {
            "id": "__{@1__",
            "label": "{",
            "shape": "box"
        },
        ...

What am I doing wrong, or is there a workaround?

hediet commented 2 years ago

What did you select as Data Extractor? Can you show a screenshot?

Klummel69 commented 2 years ago

A Data Extractor is not set / available.

It is Java code. I thought until now that if no Data Extractor is used, then the expression is interpreted directly as a JSON string.

TestJson

hediet commented 2 years ago

It is Java code. I thought until now that if no Data Extractor is used, then the expression is interpreted directly as a JSON string.

I got a PR that changed this for the case that the string could not be parsed. Can you post the result of y.toString() in the debug console or watch window?

Klummel69 commented 2 years ago

Output of debug console:

y
"{"kind":{"graph":true},"nodes":[{"id":"1"},{"id":"2"},{"id":"3"}],"edges":[{"from":"1","to":"2"},{"from":"2","to":"3"}]}"
coder: 0
hash: 0
value: byte[120]@23

y.toString()
"{"kind":{"graph":true},"nodes":[{"id":"1"},{"id":"2"},{"id":"3"}],"edges":[{"from":"1","to":"2"},{"from":"2","to":"3"}]}"
coder: 0
hash: 0
value: byte[120]@23

TestJson2

The example app does not seem to work either:

TestJson3

Klummel69 commented 2 years ago

Quick update, the same game in C++ works like a charm.

viz1

Klummel69 commented 2 years ago

Could the problem be the outer quotation marks before the parenthesis? In the JSON source a quotation mark is displayed. Maybe the visualizer can't handle the fact that the Java debugger returns a " as the first character.

hediet commented 2 years ago

In the JSON source a quotation mark is displayed. Maybe the visualizer can't handle the fact that the Java debugger returns a " as the first character.

The extension tries to account for that: https://github.com/hediet/vscode-debug-visualizer/blob/9fb1e8127a39b94a362e57ee928b5ace9423259b/extension/src/VisualizationBackend/parseEvaluationResultFromGenericDebugAdapter.ts#L23-L34

However, reply.variablesReference seems to be set, which causes the extension do use the wrong code path: https://github.com/hediet/vscode-debug-visualizer/blob/9fb1e8127a39b94a362e57ee928b5ace9423259b/extension/src/VisualizationBackend/GenericVisualizationSupport.ts#L68

Afaik this used to work, maybe an update of the java extension changed that. Would be awesome if you could look into this by debugging it.

hediet commented 2 years ago

Instead of

y
"{"kind":{"graph":true},"nodes":[{"id":"1"},{"id":"2"},{"id":"3"}],"edges":[{"from":"1","to":"2"},{"from":"2","to":"3"}]}"
coder: 0
hash: 0
value: byte[120]@23

If would have expected the output to be just this:

y
"{"kind":{"graph":true},"nodes":[{"id":"1"},{"id":"2"},{"id":"3"}],"edges":[{"from":"1","to":"2"},{"from":"2","to":"3"}]}"
Klummel69 commented 2 years ago

OK, I will try it over the weekend. I have never debugged an extension within a debugging.

Klummel69 commented 2 years ago

In the meantime I have been working on debugging extensions. (First of all: I actually come from the embedded corner with C/C++. I only use Javascript for a bit of browser hacking, I haven't created Typescript yet. )

It is as you suspected.
variablesReference is != 0, so the function constructGraphFromVariablesReference() is called instead of parseEvaluationResultFromGenericDebugAdapter().

2022-03-15_180433

If I set the value=0 in debugging, the graph is generated correctly.

hediet commented 2 years ago

Probably, the extension should first try to decode is as JSON string and only if that fails use variablesReference.

I'm open for a PR, but try to look at it myself this or next month.