Visual Studio how to serialize object from debugger

C#DebuggingSerializationVisual Studio-2012

C# Problem Overview


I'm trying to investigate a bug in a crash dump (so I can not change the code). I have a really complicated object (thousands of lines in the serialized representation) and its state is inconsistent. To investigate its state the Visual Studio debugger view is useless. But the object has a data contract. I'd like to serialize it and then use my favorite text editor to navigate across the object. Is it possible to do the from the debugger?

C# Solutions


Solution 1 - C#

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)

Solution 2 - C#

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.

Solution 3 - C#

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#

Solution 4 - C#

Since .NET Core 3.0 you can use System.Text.Json:

System.Text.Json.JsonSerializer.Serialize(obj)

Solution 5 - C#

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))

Solution 6 - C#

It might be possible to use the immediate window to serialize it and then copy the content to your favorite editor.

Another option is to override the ToString() method and call it while in debug mode.

You can also write the contents out to a file shortly before the crash, or wrap the code into a try/catch and write the file then. I'm assuming you can identify when it's crashing.

Solution 7 - C#

I have an extension method I use:

public static void ToSerializedObjectForDebugging(this object o, FileInfo saveTo)
{
    Type t = o.GetType();
    XmlSerializer s = new XmlSerializer(t);
    using (FileStream fs = saveTo.Create())
    {
        s.Serialize(fs, o);
    }
}

I overload it with a string for saveTo and call it from the immediate window:

public static void ToSerializedObjectForDebugging(this object o, string saveTo)
{
    ToSerializedObjectForDebugging(o, new FileInfo(saveTo));
}

Solution 8 - C#

I've been using ObjectDumper.Net.

It works well, especially if you have live unit testing. I can easily view a variable's value in the console when a test runs, saving me from debugging manually.

This may help if you're using XUnit.

Solution 9 - C#

A variation on the answer from Omar Elabd --

It's not free, but there's a free trial for OzCode
(https://marketplace.visualstudio.com/items?itemName=CodeValueLtd.OzCode).

There's built-in exporting to JSON within the context/hover menu there, and it works a bit better than the Object Export extension (the trade-off for it not being free).

http://o.oz-code.com/features#export (demo)

I know this is a few years after the fact, but I'm leaving an answer here because this worked for me, and someone else may find it useful.

Solution 10 - C#

In case you have a circular reference, run this in the immediate Window:

Newtonsoft.Json.JsonConvert.SerializeObject(app, Newtonsoft.Json.Formatting.Indented,
new Newtonsoft.Json.JsonSerializerSettings
{
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize
});

Solution 11 - C#

A variation on Alexey's answer. Slightly more complicated but doesn't involve writing to a text file:

  1. In the Immediate Window enter:

    System.IO.StringWriter stringWriter = new System.IO.StringWriter();

  2. In the Watch Window enter two watches:

    a. stringWriter

    b. new System.Xml.Serialization.XmlSerializer(obj.GetType()).Serialize(stringWriter, obj)

After you enter the second watch (the Serialize one) the stringWriter watch value will be set to obj serialized to XML. Copy and paste it. Note that the XML will be enclosed in curly braces, {...}, so you'll need to remove them if you want to use the XML for anything.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxvorsxView Question on Stackoverflow
Solution 1 - C#mcintyre321View Answer on Stackoverflow
Solution 2 - C#AlexeyView Answer on Stackoverflow
Solution 3 - C#Omar ElabdView Answer on Stackoverflow
Solution 4 - C#Łukasz KurzyniecView Answer on Stackoverflow
Solution 5 - C#Chris PeacockView Answer on Stackoverflow
Solution 6 - C#Chuck ConwayView Answer on Stackoverflow
Solution 7 - C#Mike CheelView Answer on Stackoverflow
Solution 8 - C#DharmaTurtleView Answer on Stackoverflow
Solution 9 - C#Michael ArmesView Answer on Stackoverflow
Solution 10 - C#InGeekView Answer on Stackoverflow
Solution 11 - C#Simon TewsiView Answer on Stackoverflow