vscode-debug-visualizer icon indicating copy to clipboard operation
vscode-debug-visualizer copied to clipboard

Java visualization: No rendering of a JSON tree

Open Klummel69 opened this issue 3 years ago • 11 comments

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?

Klummel69 avatar Mar 10 '22 09:03 Klummel69

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

hediet avatar Mar 10 '22 09:03 hediet

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

Klummel69 avatar Mar 10 '22 11:03 Klummel69

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?

hediet avatar Mar 10 '22 13:03 hediet

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 avatar Mar 10 '22 15:03 Klummel69

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

viz1

Klummel69 avatar Mar 10 '22 16:03 Klummel69

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.

Klummel69 avatar Mar 10 '22 16:03 Klummel69

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 avatar Mar 11 '22 09:03 hediet

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"}]}"

hediet avatar Mar 11 '22 09:03 hediet

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

Klummel69 avatar Mar 11 '22 11:03 Klummel69

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.

Klummel69 avatar Mar 15 '22 17:03 Klummel69

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.

hediet avatar Mar 16 '22 10:03 hediet