Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging JSON.NET

I'm using JSON.NET to deserialize some JSON returned from a web service. Unfortunately, i'm getting the following err:

Cannot deserialize JSON array into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'.

but i have no idea how to debug it. is there a way to have it tell me what the offending JSON is? or even what it's trying to deserialize it to? unfortunately this is part of a larger object hierarchy, so it's not just a simple piece of JSON. i've tried stepping through the code, but there's miles of it and little documentation. If it could at least tell me the offending piece, then i can see if maybe i cocked up my object definition or something.

like image 405
bryan costanich Avatar asked Oct 18 '11 22:10

bryan costanich


People also ask

How do I debug JSON serialization?

Debugging with Serialization Tracing. The Json.NET serializer supports logging and debugging using the ITraceWriter interface. By assigning a trace writer you can capture serialization messages and errors and debug what happens inside the Json.NET serializer when serializing and deserializing JSON.

How do I Run and debug NET applications in Visual Studio Code?

To develop, run and debug .NET 5.0 applications in VS Code you need the following installed: Visual Studio Code - free code editor / IDE that runs on Windows, Mac and Linux C# extension for Visual Studio Code - adds support to VS Code for developing and debugging .NET applications

How do I write JSON to a string in Visual Studio?

How to write .NET objects as JSON (serialize) To write JSON to a string or to a file, call the JsonSerializer.Serialize method. The following example creates JSON as a string: C#. string jsonString = JsonSerializer.Serialize (weatherForecast); Dim jsonString As String.

How do I run a program in debug mode?

Open the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to .NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.


1 Answers

If you want to deserialize what you're converting from json into C# classes there's something built into json.net for this. Just create a settings object and pass it into your deserializeobject call:

if (!string.IsNullOrEmpty(json))
{
   var settings = new JsonSerializerSettings
       {
           Error = (sender, args) =>
                   {
                       if (System.Diagnostics.Debugger.IsAttached)
                       {
                            System.Diagnostics.Debugger.Break();
                       }
                   }
       };

    result = JsonConvert.DeserializeObject<T>(json, settings);
}

Then when there are errors in the deserialization process (like a mismapped C# class -> json object) it'll break in the debugger. Inspect the args object and you'll see a property called CurrentObject. This is the C# class that the parser is trying to deserialize the JSON into (and will provide details about what the error might be).

Hope that helps.

like image 95
Bil Simser Avatar answered Oct 17 '22 01:10

Bil Simser