Quantcast
Channel: Ugo Lattanzi
Viewing all articles
Browse latest Browse all 8

Customize Json result in Web API

$
0
0

In my previous post I wrote about Web API and the content negotiation. In this post I’d like to describe how it’s easy to change the Json serialization.

The most important improvement from the Beta to the RC in the Web API is about the serialization, in fact now the framework is using Json.Net for the serialization. For all people who don’t know what Json.Net is I can say, to make the definition easily, Json.Net is the best serializer in the .NET world.

It's cool for many reasons, especially because it's flexible and easy to setup. In this post I’d like to show the principals and useful settings that you probably want to change in your application, or can be helpful for you in the future.

Before to start, we have to make our Web API project easy to debug so, I’m going to remove the XML formatter. I’m doing that because I’m in a test project and I’d like to see the response in the browser. The easily way to force the response to Json for all Web API requests is to remove the XML. Obviously you shouldn't do it in production.

NOTE: All code in this post,except the last one, must be located into the global.asax.cs

varformatters=GlobalConfiguration.Configuration.Formatters;formatters.Remove(formatters.XmlFormatter);

Now we can start change the setting for all Json responses accessing to GlobalConfiguration.Configuration.Formatters.JsonFormatter.

In the following examples I’ll use always the class below:

publicclassUser{publicstringFirstname{get;set;}publicstringLastname{get;set;}publicstringUsername{get;set;}publicDateTimeBirthdate{get;set;}publicUriWebsite{get;set;}publicintAge{get;set;}publicdoubleSalary{get;set;}  [JsonIgnore]publicstringIgnoreProperty{get;set;}}

How can we indent the json response?

varjson=GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.Formatting=Newtonsoft.Json.Formatting.Indented;

How can we change the case in the response?

varjson=GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.ContractResolver=newCamelCasePropertyNamesContractResolver();

How can we manage the null in the response?

varjson=GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.NullValueHandling=Newtonsoft.Json.NullValueHandling.Ignore;

How can we change the DateTime format?

varjson=GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.DateFormatHandling=Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;

How can we change the TimeZone format?

varjson=GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.DateTimeZoneHandling=Newtonsoft.Json.DateTimeZoneHandling.Utc;

How can we change the Culture of the serializer?

varjson=GlobalConfiguration.Configuration.Formatters.JsonFormatter;json.SerializerSettings.Culture=newCultureInfo("it-IT");

Another cool feature of Web API is to opportunity to override the configuration for a single response. You can use all of the previous setting directly in the single action like explained in the code below:

publicHttpResponseMessageGet(){IList<User>result=newList<User>();result.Add(newUser{Age=34,Birthdate=DateTime.Now,Firstname="Ugo",Lastname="Lattanzi",IgnoreProperty="This text should not appear in the reponse",Salary=1000,Username="imperugo",Website=newUri("http://www.tostring.it")});varformatter=newJsonMediaTypeFormatter();varjson=formatter.SerializerSettings;json.DateFormatHandling=Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;json.DateTimeZoneHandling=Newtonsoft.Json.DateTimeZoneHandling.Utc;json.NullValueHandling=Newtonsoft.Json.NullValueHandling.Ignore;json.Formatting=Newtonsoft.Json.Formatting.Indented;json.ContractResolver=newCamelCasePropertyNamesContractResolver();json.Culture=newCultureInfo("it-IT");returnRequest.CreateResponse(HttpStatusCode.OK,result,formatter);}

Such as ASP.NET MVC, Web API are really flexible ... and ROCKS!


Viewing all articles
Browse latest Browse all 8

Trending Articles