Reputation: 3803
I have created a New MVC4 Application and by default Newton JSON added to the Package.
I read that it is useful for serializing and deserializing JSON. Is this all it does ?
By default we can send JSON in MVC using JSONResult. and using Stringify in JQuery i can receive as a class in C#.
I know there should be some reason why they added Newton JSON.
As i am new to MVC and starting off new project want to know some insight of which serialize/deserialize to go for ?
Thanks
Upvotes: 6
Views: 12273
Reputation: 12142
If your project was just an MVC project with no WebApi, then Newtonsoft.Json
was not added for returning JsonResults
as the JsonResult
returned by MVC uses the JavaScriptSerializer
as below:
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
}
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
serializer.MaxJsonLength = MaxJsonLength.Value;
}
if (RecursionLimit.HasValue)
{
serializer.RecursionLimit = RecursionLimit.Value;
}
response.Write(serializer.Serialize(Data));
}
}
In this case it was added because WebGrease
has a dependency on it. And the bundling and minification services provided by MVC in System.Web.Optimization
have a dependency on WebGrease
.
So a default MVC app with no WebApi will have Newtonsoft.Json
installed for bundling and minification services not WebApi.
To be clear the JsonResult
returned by WebApi in System.Web.Http
does use Newtonsoft.Json
for it's serialization as below:
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Web.Http.Results
{
/// <summary>
/// Represents an action result that returns an <see cref="F:System.Net.HttpStatusCode.OK"/> response with JSON data.
/// </summary>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
public class JsonResult<T> : IHttpActionResult
But Newtonsoft.Json
is not included in a non WebApi, default MVC project just in case you might decide to use some WebApi, it's there because, as above, WebGrease
needs it. Not sure what they're doing in vNext, probably Newtonsoft.Json
.
Upvotes: 0
Reputation: 3526
Use JsonResult and return Json(yourObject)
on a post operation, or Json(yourObject, JsonRequestBehavior.AllowGet)
if you're doing a GET operation.
If you want to deserialize Json with the Newton Json.NET, check out http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx
Upvotes: 0
Reputation: 17118
They added Newtonsoft so that your WebAPI controller can magically serialize your returned object. In MVC 3 we used to return our object like so:
public ActionResult GetPerson(int id)
{
var person = _personRepo.Get(id);
return Json(person);
}
In a Web API project you can return person and it will be serialized for you:
public Person GetPerson(int id)
{
var person = _personRepo.Get(id);
return person
}
Upvotes: 6