Returning camelCase JSON from ASP.NET Web API
Loving ASP.NET Web API but not loving the .NET-centric PascalCase JSON it produces?
// a .NET class like this... public class Book { public int NumberOfPages { get; set; } public string Author { get; set; } }
// ... should be serialized into JSON like this { "numberOfPages": 42, "author": "JK Rowling" }
Luckily this is quite easy to fix with a custom formatter. I started with Henrik Nielsen’s custom Json.NET formatter and changed it slightly to use a camelCase resolver and also indent the JSON output (just for developers; we would turn this off once the app was deployed). You can grab the code here: https://gist.github.com/2012642
Then just swap out the default JSON formatter in your global.asax.cs:
var config = GlobalConfiguration.Configuration; // Replace the default JsonFormatter with our custom one var index = config.Formatters.IndexOf(config.Formatters.JsonFormatter); config.Formatters[index] = new JsonCamelCaseFormatter();March 10, 2012
- «Previous Post
Leave Your Comment (Cancel Reply)
Your email will not be published or shared. Required fields are marked *
*
*
You may use these HTML tags and attributes: <a class="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>