Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC JsonResult camelCase serialization [duplicate]

I am trying to make my action return a JsonResult where all its properties are in camelCase.

I have a simply model:

public class MyModel {     public int SomeInteger { get; set; }      public string SomeString { get; set; } } 

And a simple controller action:

public JsonResult Index()     {         MyModel model = new MyModel();         model.SomeInteger = 1;         model.SomeString = "SomeString";          return Json(model, JsonRequestBehavior.AllowGet);     } 

Calling this action method now returns a JsonResult containing the following data:

{"SomeInteger":1,"SomeString":"SomeString"} 

For my uses i need the action return the data in camelCase, somehow like this:

{"someInteger":1,"someString":"SomeString"} 

Is there any elegant way to do this?

I was looking into possible solutions around here and found MVC3 JSON Serialization: How to control the property names? where they set DataMember definitions to every property of the model, but I do not really want to do this.

Also I found a link where they say that it is possible to solve exactly this kind of issue: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#json_camelcasing. It says:

To write JSON property names with camel casing, without changing your data model, set the CamelCasePropertyNamesContractResolver on the serializer:

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

One entry on this blog http://frankapi.wordpress.com/2012/09/09/going-camelcase-in-asp-net-mvc-web-api/ also mentiones this solution and states you can simply add it to the RouteConfig.RegisterRoutes to fix this issue. I tried it, but I couldn't make it work.

Do you guys have any idea where I was doing something wrong?

like image 735
Thomas Mondel Avatar asked Feb 23 '13 13:02

Thomas Mondel


1 Answers

If you want to return a json string from your action which adheres to camelcase notation what you have to do is to create a JsonSerializerSettings instance and pass it as the second parameter of JsonConvert.SerializeObject(a,b) method.

public string GetSerializedCourseVms()     {         var courses = new[]         {             new CourseVm{Number = "CREA101", Name = "Care of Magical Creatures", Instructor ="Rubeus Hagrid"},             new CourseVm{Number = "DARK502", Name = "Defence against dark arts", Instructor ="Severus Snape"},             new CourseVm{Number = "TRAN201", Name = "Transfiguration", Instructor ="Minerva McGonal"}         };         var camelCaseFormatter = new JsonSerializerSettings();         camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();         return JsonConvert.SerializeObject(courses, camelCaseFormatter);     } 
like image 91
DanKodi Avatar answered Oct 17 '22 21:10

DanKodi