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
While Web API uses JSON.NET, MVC4 uses by default the JavaScriptSerializer, and I dont think it supports changing to Camel Case serialization. Check this: Setting the default JSON serializer in ASP.NET MVC
My suggestion is that you create a custom JsonNetResult as described here Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible? and change the last line to:
var serializedObject = JsonConvert.SerializeObject(
Data,
Formatting.Indented,
new JsonSerializerSettings { MappingResolver = new CamelCaseMappingResolver() });
Note that below information is for Asp .Net core
.Net team has recently announced that MVC now serializes JSON with camel case names by default.
With couple of lines below you would be able to enable this functionality:
services
.AddMvc()
.AddJsonOptions(options =>
options.SerializerSettings.ContractResolver = new DefaultContractResolver());
I wrote little blog on the same which is here.
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?
You can create a static method to return a ContentResult
that utilizes the NewtonSoft.Json
library to do the serialization similar to this:
public static ContentResult CamelJson<TData>(TData response)
{
DefaultContractResolver resolver = new CamelCasePropertyNamesContractResolver();
JsonSerializerSettings settings = new JsonSerializerSettings
{
ContractResolver = resolver,
DateFormatHandling = DateFormatHandling.IsoDateFormat
};
return new ContentResult
{
Content = JsonConvert.SerializeObject(response, settings),
ContentType = "application/json"
};
}
Example usage:
[HttpGet]
public ContentResult GetCamelCaseJsonData()
{
return ContentUtils.CamelJson(result);
}
The output will be camel-case.