Visual Studio how to serialize object from debugger
With any luck you have Json.Net in you appdomain already. In which case pop this into your Immediate window:
Newtonsoft.Json.JsonConvert.SerializeObject(someVariable)
Some time ago I wrote this one-liner serializing an object to a file on the disk. Copy/paste it to your Immediate window, and replace obj
(it's referenced twice) with your object. It'll save a text.xml
file to c:\temp
, change it to your liking.
(new System.Xml.Serialization.XmlSerializer(obj.GetType())).Serialize(new System.IO.StreamWriter(@"c:\temp\text.xml"), obj)
Don't expect any magic though, if the object cannot be serialized, it'll throw an exception.
Here is a Visual Studio extension which will let you do exactly that:
https://visualstudiogallery.msdn.microsoft.com/c6a21c68-f815-4895-999f-cd0885d8774f
You can output to JSON, XML or C#
Since .NET Core 3.0
you can use System.Text.Json
:
System.Text.Json.JsonSerializer.Serialize(obj)
Use this in Visual Studio's "Immediate" window, replacing c:\directory\file.json
with the full path to the file to which you'd like to write the JSON and myObject
with your variable to serialise:
System.IO.File.WriteAllText(@"c:\directory\file.json", Newtonsoft.Json.JsonConvert.SerializeObject(myObject))