3

How does MVC asp.net serialization work for Json object on Controller actions ?

For example I have a custom object and if send an ajax request with JSON objects to the server action

public ActionResult(List<CustomObject> abc)
{
   // Object is serialized automatically how MVC is doing it is the question.
}

The reason why I am asking this is that some objects of mine are not properly serialzed and hence loss of data is there , then I have to revert to the old method of string value to serializaiton.

public ActionResult(string abc)
{ 
   JavaScriptSerializer serializer = new JavaScriptSerializer();

   List<CustomObject> lstabc = serializer.Deserialize<List<CustomObject>>(abc);
}

Which I would like to avoid and secondly which are the best libraries for doing JSON object serialization MVC Asp.net ?

Gudradain
  • 4,653
  • 2
  • 31
  • 40
Nikshep
  • 2,117
  • 4
  • 21
  • 30
  • I think you would need to write your own JsonResult class (with appropriate extension method for Controller). Afaik the best JSON library for .NET is Newtonsoft.JSON (available on NUGet) – MattDavey Sep 07 '11 at 08:07

1 Answers1

2

The deserialisation is taken care of by the model binder.

if you are finding it is not working you can create your own custom model binder and register it with the framework - this should mean you can avoid all that deserialisation noise in your controllers.

There are some links to explanations of this and how to implement a custom model binder in the following SO question:

ASP.Net MVC Custom Model Binding explanation

One of the more popular json serialisation libraries is the newtonsoft json.net library - I've used it effectively many times:

http://james.newtonking.com/pages/json-net.aspx

Good luck!

Community
  • 1
  • 1
Paul
  • 5,514
  • 2
  • 29
  • 38