Debugging JSON.NET

I am using JSON.NET to deserialize some JSON returned from a web service. Unfortunately, I get the following error message:

Unable to deserialize the JSON array to type 'System.Collections.Generic.Dictionary`2 [System.String, System.String]'.

but I don’t know how to debug it. is there any way to tell me what a JSON intruder is? or even what he is trying to deserialize? unfortunately, this is part of a larger hierarchy of objects, so this is not just a simple part of JSON. I tried going through the code, but there are miles and a little documentation. If he could at least tell me the offensive part, then I can see, maybe I raised the definition of my object or something like that.

+6
source share
3 answers

It turned out where to look after passing tons of code. line 1118ish JsonSerializerInternalReader:

SetPropertyValue(property, reader, newObject); 

and if you put a breakpoint there and look at the “property”, you will see which property gets serialized. since he does it in json string order, you can look at it and see how the latter is successfully installed. then you at least know where the serializer crashes.

it would be useful, however, if JSON.net raised at least the name of the property in error, but until that happens, it looks like the next best.

+3
source

If you want to deserialize what you convert from json to C # classes, for something built into json.net. Just create a settings object and pass it to your deserialized 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 during the deserialization process (for example, a C # class object → json mismapped), it will be broken into a debugger. Inspect the args object and you will see a property called CurrentObject. This is a C # class that the parser tries to deserialize JSON (and will contain information about what might be an error).

Hope this helps.

+16
source

Based on the information you provided, I would start by looking for an object that has a property of type Dictionary. This is probably your problem class.

0
source

Source: https://habr.com/ru/post/899566/


All Articles