Using the Json.NET Serializer in an MVC4 Project

I am starting to learn Json.NET, but I am unable to use its serializer. I have a new MVC4 project with Web.API service:

public class PTE_TestsController : ApiController { PTE_TestsRepository _repository = new PTE_TestsRepository(); // GET /api/PTE_Tests/5 public HttpResponseMessage<string> Get(int id) { try { PTE_Test test = _repository.GetTest(id); return new HttpResponseMessage<string>(JsonConvert.SerializeObject(test)); } catch { return new HttpResponseMessage<string>(HttpStatusCode.NotFound); } } } 

JsonConvert.SerializeObject () works as expected and returns a string. My Web.API controller returns this as part of an HttpResponseMessage. The end result, when viewed in Fiddler, is not JSON data, but the JSON data is serialized again (I think):

 "{\"ID\":1,\"Name\":\"Talar Tilt\",\"TagID\":1,\"PracticeID\":1, \"SpecificAreaID\":1,\"TypeID\":1,\"VideoID\":1,\"PicID\":1}" 

Does anyone know how to disable the default serializer so that I can use Json.NET directly? By the way, I donโ€™t use the default serializer, because I canโ€™t figure out how to make it work with complex objects (PTE_Test will eventually contain members of type List).

As an alternative, it will also solve my problem if anyone can explain how to use the default serializer with complex objects. The MSDN explanation did not help me.

+6
source share
3 answers

Rick Strahl has a blog on here with code that works.

+5
source

As others have pointed out, you need to create a formatter and replace the DataContractSerializer with a JSON.NET serializer. Although, if you are not in a hurry specifically for JSON.NET, there are rumors that the next beta / rc will support native JSON.NET.

Conceptually, however, you are missing a piece of WebAPI magic. Using WebAPI, you return your object to its native state (or IQueryable if you want OData support). After your function call completes, Formatter will take over and convert it to the desired form based on the clientโ€™s request.

So, in your source code, you converted PTE_Test to a JSON string and returned it, after which the JSON Formatter launched and serialized the string. I changed your code as follows:

  public class PTE_TestsController : ApiController { PTE_TestsRepository _repository = new PTE_TestsRepository(); public HttpResponseMessage<PTE_Test> Get(int id) { try { PTE_Test test = _repository.GetTest(id); return new HttpResponseMessage(test); } catch { return new HttpResponseMessage<string>(HttpStatusCode.NotFound); } } } 

Notice how your function returns PTE_Test instead of string . Assuming the request came with the Accept = application/json request header, then JSON formatting will be called. If the request has a heading: Accept = text/xml , XML formatting is called.

There is a decent article on the topic here . If you're a visual student, Scott Gu shows some examples using the violinist in this video, starting in about 37 minutes . Pedro Reis will go a little deeper into the discussion of content here .

+2
source

To do this, use formatters.

Check out: https://github.com/WebApiContrib/WebAPIContrib/tree/master/src/WebApiContrib.Formatting.JsonNet .

Json.NET support will be in the RC release of the web API.

0
source

Source: https://habr.com/ru/post/913741/


All Articles