Newtonsoft JSON

I created a new MVC4 application and by default Newton JSON is added to the package.

I read that this is useful for serializing and deserializing JSON. Is that all he does?

By default, we can send JSON to MVC using JSONResult. and using Stringify in jQuery, I can get as a class in C #.

I know that there must be a reason why they added Newton JSON.

As I am new to MVC and starting a new project, I want to know what idea of โ€‹โ€‹serialization / deserialization can be used?

thanks

+6
source share
3 answers

They added Newtonsoft so that your WebAPI controller can magically serialize the returned object. In MVC 3, we returned our object like this:

public ActionResult GetPerson(int id) { var person = _personRepo.Get(id); return Json(person); } 

In the Web API project, you can return the person, and he will be serialized for you:

 public Person GetPerson(int id) { var person = _personRepo.Get(id); return person } 
+6
source

Use JsonResult and return Json(yourObject) in the post operation, or Json(yourObject, JsonRequestBehavior.AllowGet) if you perform a GET operation.

If you want to deserialize Json using Newton Json.NET, see http://www.hanselman.com/blog/NuGetPackageOfTheWeek4DeserializingJSONWithJsonNET.aspx

0
source

If your project was just an MVC project without WebApi, then Newtonsoft.Json not added to return JsonResults , since the JsonResult returned by MVC uses the JavaScriptSerializer , as shown 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 binding and minimization services provided by MVC in System.Web.Optimization depend on WebGrease .

Thus, the default MVC application that does not have WebApi will have Newtonsoft.Json installed for the binding and minimization services, not WebApi.

To be clear, the JsonResult returned by WebApi in System.Web.Http uses Newtonsoft.Json to serialize it, as shown 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 not included in the MVC project by default, and if you can use any WebApi, it is there, because, as indicated above, WebGrease needs it. Not sure what they are doing in vNext, possibly Newtonsoft.Json .

0
source

All Articles