3

We are using JSON.NET to serialize custom objects. I have written a unit test that performs JSON serialization of our custom object successfully:

// Generate custom object...
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }; // Serializing objects referred to by abstract type, see comment at http://www.tomdupont.net/2014/04/deserialize-abstract-classes-with.html
var json = JsonConvert.SerializeObject(object, settings);
// ...Deserialize object and compare are equal

However, in our APIController, which handles HTTP requests and has methods like this:

[HttpPost]
public ActionResult ProfileFile()
{
    // Code to handle HTTP request
}

performing the same serialization operation on a custom object generated the same way yields a stream of System.InvalidCastException for custom objects within the custom object being serialized (all of which have been tagged with JSON.NET attributes and have their own passing unit tests).

What would cause it to behave differently in our web project, while it works in the unit tests? Thanks in advance.

Scotty H
  • 6,432
  • 6
  • 41
  • 94
  • 2
    1) Can you share the complete `ToString()` of the exceptions? 2) Be sure you are setting up your global `JsonSerializerSettings` correctly, see http://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api or http://stackoverflow.com/questions/28552567/web-api-2-how-to-return-json-with-camelcased-property-names-on-objects-and-the – dbc Jul 03 '15 at 18:55
  • Thanks, I didn't realize until you pointed it out (via links) that this project is inherently using JSON.NET. 1) Since JSON.NET comes via a NuGet package, debugging with the source code of JSON.NET is difficult because I can't uninstall the NuGet package without having to deal with a tangle of other dependent packages key to its functioning as an ASP.NET web app. 2) Thanks for the links. Tried adjusting global settings as illustrated in the question, but to no avail. Still same first chance exception of type `System.InvalidCastException`. Any ideas? – Scotty H Jul 06 '15 at 21:14
  • To clarify my response to 1) above, without debugging into the source code I can't see the details of the error. – Scotty H Jul 06 '15 at 21:21
  • Have you tried including http://srv.symbolsource.org/pdb/Public in your symbol servers ( in visual studio, goto Tools -> Options -> Debugging-> Symbols). That might allow you to load the symbols for the nuget package and debug this issue further. Also could you provide some more information about what the object you are trying to serialize? – KnightFox Jul 10 '15 at 19:55

1 Answers1

0

Without knowing the exact content of the object you are trying to serialize, it is hard to provide an exact solution. However, if i were in your situation, I would create a rudimentary class that represents your custom object and try to serialize it. If you continue to see that error, I would start adding the JsonIgnore attribute to the different properties of the class to see which property is actually causing this failure. That will help you narrow down the problem.

KnightFox
  • 3,132
  • 4
  • 20
  • 35