2

Swapping out the standard serializer for the JSON.NET serializer is a non-issue. Code below was taken directly out of Scott Hanselman's blog

http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

JsonSerializerSettings serializerSettings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
serializerSettings.Converters.Add(new IsoDateTimeConverter());
GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);

However, after doing this I can't save anything, ajax call fails. The error that I get back is:

This DataController does not support operation 'Insert' for entity 'JObject'.

JObject is what is being returned from the JSON.NET serializer. It isn't honoring the "__type" property that is being attached to the entity.

Example JSON:

[{"Id":"0","Operation":1,"Entity":{"__type":"Product:#Catalog.Models","Sku":"adsfadsf"}}]

Is there anything that I can do short of dipping into the JSON.NET source code to fix this? Is there an update coming to the ASP.NET MVC 4 Beta that will fix this?

Ryan Pedersen
  • 3,177
  • 27
  • 39

2 Answers2

0

That is already part of the WebAPI contrib. Have a look here. The code actually is quite straightforward.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • The WebAPI contrib project did exacly what I did but that is not the solution to the issue. The output of the method OnReadFromStreamAsync is a Task and the object that is returned is of type System.Web.Http.Data.ChangeSetEntry where the entity is of type JObject. It is the JObject type that is the fly in the ointment. – Ryan Pedersen Apr 16 '12 at 17:28
0

ASP.NET MVC 4 is still in beta and there are several features that don't quite work 100% yet. The last time I checked the integration of Newtonsoft.Json and the JsonSerializer is not complete. After talking with some folks on the ASP.NET team it is my understanding that it is being worked on but there are no timelines for completion.

Ryan Pedersen
  • 3,177
  • 27
  • 39