vendredi 3 juillet 2015

JSON.NET Serialization Behaves Differently in ApiController

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://ift.tt/1hS4emJ
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.

Aucun commentaire:

Enregistrer un commentaire