0

I tried to use Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented) to convert variable to json when debugging, but vs immediate only show non-format string like image

I expect to get result like below LINQPad's result

Wei Lin
  • 3,591
  • 2
  • 20
  • 52
  • With immediate window you get result that you can copy-paste and it will be valid code. Add `Console.WriteLine` to display string with indentation – JL0PD May 06 '21 at 02:08
  • @JL0PD I try `Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented))` and it show `Expression has been evaluated and has no value` – Wei Lin May 06 '21 at 02:14

3 Answers3

2

If you would like to print newline and tab characters, you can type the variable name into the Immediate Window followed by the string ",nq".

So instead of typing Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented) into the Immediate Window, you would type Newtonsoft.Json.JsonConvert.SerializeObject(rows, Formatting.Indented),nq.

The ",nq" at the end serves as a "format specifier" for the Immediate Window. Find out more about that here.

Thanks to user davesem for pointing this out here.

Christian May
  • 127
  • 11
1

Personally I'm not using immediate window because find it less useful than other options.

To inspect value you can hover over variable and click on magnifier icon, it will display stored data with ToString() representation. Optionally you can choose other visualizer like json, xml or html with dropdown near icon. Downside is that you need declared variable.

enter image description here

Other option is to use Watch panel (during debug click debug -> windows -> watch -> watch 1). It allows you to inspect variables, override them and call methods

enter image description here

JL0PD
  • 3,698
  • 2
  • 15
  • 23
  • Thanks, I upvoted this, but it need to inject code into project. emm.. it looks like the better answer. – Wei Lin May 06 '21 at 03:02
0

Another approach could be saving json to file, it will greatly help to analyze and compare different outputs

System.IO.File.WriteAllText(@"c:\temp\debug_file.json", Newtonsoft.Json.JsonConvert.SerializeObject(tSet, Newtonsoft.Json.Formatting.Indented));
sairfan
  • 970
  • 2
  • 12
  • 20