Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper JSON serialization in MVC 4

I'd like to have JSON 'properly' serialized (camelCase), and the ability to change date formats if necessary.

For Web API it is very easy - in the Global.asax I execute the following code

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

This code, at the pipeline level, handles serialization the way I'd like.

I would like to accomplish the same thing in MVC 4 - have any JSON returned from controller action methods to be serialized properly. With a little searching I found the following code to throw in the Global.asax application startup:

HttpConfiguration config = GlobalConfiguration.Configuration; Int32 index = config.Formatters.IndexOf(config.Formatters.JsonFormatter); config.Formatters[index] = new JsonMediaTypeFormatter {      SerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } }; 

It seems to execute fine but when I return JSON from a controller it is all PascalCased. A simple example of my action method:

private JsonResult GetJsonTest() {     var returnData = dataLayer.GetSomeObject();     return Json(returnData, JsonRequestBehavior.AllowGet); } 

Am I going about this wrong? Any idea how to accomplish this at the pipeline level?

like image 256
Mario Avatar asked Jun 21 '13 21:06

Mario


People also ask

What is serialization in MVC?

Serialization in C# is the process of bringing an object into a form that it can be written on stream. It's the process of converting the object into a form so that it can be stored on a file, database, or memory; or, it can be transferred across the network.

Does JSON need serialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

What is serialization of JSON in C#?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects. In this article and code examples, first we will learn how to serialize JSON in C# and then we will learn how to deserialize JSON in C#.

What is JSON in MVC with example?

"JSON" (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. When working together with "jQuery" and "ASP.NET MVC" in building web applications, it provides an efficient mechanism to exchange data between the web browser and the web server.


1 Answers

I would recommend using something like ServiceStack or Json.NET for handling Json output in your MVC application. However, you can easily write a class and override the Json method using a base class. See my example below.

NOTE: With this, you do not need anything in your Global.ascx.cs file.

Custom JsonDotNetResult class:

public class JsonDotNetResult : JsonResult {     private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings     {         ContractResolver = new CamelCasePropertyNamesContractResolver(),         Converters = new List<JsonConverter> { new StringEnumConverter() }     };      public override void ExecuteResult(ControllerContext context)     {         if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet &&             string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))         {             throw new InvalidOperationException("GET request not allowed");         }          var response = context.HttpContext.Response;          response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json";          if (this.ContentEncoding != null)         {             response.ContentEncoding = this.ContentEncoding;         }          if (this.Data == null)         {             return;         }          response.Write(JsonConvert.SerializeObject(this.Data, Settings));     } } 

Base Controller class:

public abstract class Controller : System.Web.Mvc.Controller {     protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)     {         return new JsonDotNetResult             {                 Data = data,                 ContentType = contentType,                 ContentEncoding = contentEncoding,                 JsonRequestBehavior = behavior             };     } } 

Now, on your controller action you can simply return something like so.

return Json(myObject, JsonRequestBehavior.AllowGet); 

BAM. You now have camelcase Objects returned with Json :)

NOTE: There are ways to do this with Serializer settings on each object that you make with Json. But who would want to type that out every time you want to return Json?

like image 58
technicallyjosh Avatar answered Oct 01 '22 08:10

technicallyjosh