I frequently make tests for my services by real data. Is there some extension for Visual Studio 2015 to help convert Data Transfer Object in Debugger into XML or JSON representation (for later use in unit tests)? Of course, I can do this through some serialization code at some execution point, but this is dirty solution, that requires recompilation (this can take up to 10 minutes in my case).
-
[https://www.nuget.org/packages/newtonsoft.json/](https://www.nuget.org/packages/newtonsoft.json/) – Fabio Aug 11 '16 at 14:03
-
NS json don't have interactive debugger extension for this. – eocron Aug 11 '16 at 14:04
-
I'm not sure that I understood your goal... How such a plugin will help you in the context of UTs? – Old Fox Aug 11 '16 at 15:21
-
Make an extension method like `XmlSerializationHelper` from [this answer](https://stackoverflow.com/questions/32903839/can-i-have-null-attribute-and-other-attribute-at-the-same-tag-in-xml-created-by/32969446#32969446) and call it in the immediate window. – dbc Aug 11 '16 at 15:21
-
Are `System.Xml.dll` (for `XmlSerializer`) or Newtonsoft.Json.dll (for JSON) linked into your project? – dbc Aug 11 '16 at 15:44
1 Answers
Not sure about add-ins or extensions, but when all else fails you can use the Immediate Window to generate the output you need, defining temporary global variables for intermediate results if required.
Example 1: Generating JSON output when Newtonsoft.Json.dll
is not already linked into your project.
Say you want to generate JSON for an object and json.net is not linked into your current project but has been downloaded onto your machine. You can still use it to generate output to the Immediate Window as follows:
Open the Immediate Window if not open already.
Load the Newtonsoft DLL by typing
Assembly.LoadFrom(...)
at the prompt with an appropriate path:System.Reflection.Assembly.LoadFrom(Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Documents\Visual Studio 2015\Projects\Json90r1\Bin\Net45\Newtonsoft.Json.dll"));
This only need be done once per debugging session.
Now you will be able to call
JsonConvert.SerializeObject()
from the Immediate Window prompt, as long as you use fully qualified names. E.g. ifobj
is a variable for which you wish to see JSON, you can type:System.Diagnostics.Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented))
The debug output will now appear in the immediate window without escaping.
Extending this trick, you could create a small DLL that contains a single public static method My.Debug.Write(object o)
that dumps objects in whatever format you want, then save that DLL into a convenient location on your development machine, say "c:\My.DLL"
. Then when debugging you can load the DLL and invoke the debug output method with much less typing.
Example 2: Using XmlSerializer
directly.
Say you would like to be able to generate XML using XmlSerializer
, but unfortunately there's no override that returns an XML string; all the serialization methods take some sort of Stream
or Writer
.
You can handle the lack of an appropriate API by defining temporary global variables in the Immediate Window to hold intermediate objects (writers and settings in this case), and serializing into those objects:
Again open the Immediate Window.
In the Immediate Window, define a temporary global
StringBuilder
andXmlWriterSettings
by typing the following:System.Text.StringBuilder _sb = new System.Text.StringBuilder(); System.Xml.XmlWriterSettings _settings = new System.Xml.XmlWriterSettings(); _settings.Indent = true;
Do this once per debugging session.
Serialize your
obj
into theStringBuilder
and print the results by typing:_sb.Length = 0; new System.Xml.Serialization.XmlSerializer(obj.GetType()).Serialize(System.Xml.XmlWriter.Create(new System.IO.StringWriter(_sb), _settings), obj); System.Diagnostics.Debug.WriteLine(_sb);
This can be done multiple times per session.
Both tricks require a bit of typing but both are quicker than waiting 10 minutes for a rebuild. Copy/paste from some saved text file or note make both quicker also.

- 104,963
- 20
- 228
- 340
-
Perfect answer! Really helpful, I didn't even knew that I were able to load assembly from Immediate Window =) – eocron Aug 12 '16 at 05:24