Mike Debela
Mike Debela

Reputation: 1950

Difference between JavaScriptSerializer and Controller Method Json

What Is the difference between JavaScriptSerializer().Serialize(Object data); and Json(Object data);. Should I be worried in picking one?

Upvotes: 2

Views: 1944

Answers (1)

Brian Rogers
Brian Rogers

Reputation: 129697

The Json() method in the MVC Controller class creates a JsonResult, which uses the JavaScriptSerializer class behind the scenes. You can see this in the source code. So there is really no difference between the two methods, except that it is a bit more code if you choose to use the JavaScriptSerializer manually.

To alleviate any confusion, I would point out that the built-in JavaScriptSerializer class is not part of Json.Net, which is a third-party open source library mentioned by @Rahul. It is possible to use this library with ASP.NET MVC if you want, but you may need to add a small amount of infrastructure to support it. See this question for details on how to do that. But unless you're working with huge amounts of data or have special serialization needs that aren't supported by the JavaScriptSerializer, there is usually no need to switch.

Upvotes: 4

Related Questions