0

When I have a straight forward object I can serialize using an ASP.NET MVC Controller like so:

public JsonResult Program(int id, int userId)
{
    return Json(GetPrograms(userId).FirstOrDefault(f => f.Id == id), JsonRequestBehavior.AllowGet);
}

Client side, my response is a Javascript object: enter image description here

Now I want my response JSON to have CamelCase property names like so: enter image description here

So using JSON.NET and this SO answer I add this code to my ASP.NET MVC Controller:

var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var json = JsonConvert.SerializeObject(program, serializerSettings);

So here is the problem, JsonConvert.SerializeObject returns a JSON string, so when used here as a JsonResult:

public JsonResult Program(int id, int userId)
{
    var serializerSettings = new JsonSerializerSettings();
    serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    var json = JsonConvert.SerializeObject(program, serializerSettings);
    return Json(json, JsonRequestBehavior.AllowGet);
}

This really isn't a JsonResult like my first snippet of code in my question returns. It is a string response and I have to use Javascript JSON.parse with a Web Client calling the Controller and handling the response.

Is there any way to CamelCase my properties and serialize a JSON object JsonResult?

I have tried JObject:

var serializer = new JsonSerializer()
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};
JObject.FromObject(obj, serializer);

But that didn't work at all an my JsonResult is a bunch of empty Arrays: enter image description here

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • There's an answer to this here: [Force lowercase property names from Json() in ASP.NET MVC](https://stackoverflow.com/questions/2789593/force-lowercase-property-names-from-json-in-asp-net-mvc). The important part is the link to creating your own `JsonResult` – Gabriel Luci Oct 31 '19 at 01:30
  • But you will always need to use `JSON.parse` on the client side to turn the JSON string into a JavaScript object. Sometimes that step is hidden from you by libraries like jQuery - but it happens. – Gabriel Luci Oct 31 '19 at 01:34
  • @GabrielLuci hmm maybe, I am not using jQuery, I am using Javascript fetch – Brian Ogden Oct 31 '19 at 01:45
  • @GabrielLuci You have to be mistaken in some way because I use the same client side Javascript to call my Controller endpoints, and it is a JSON string when using JsonConvert.SerializeObject and it is not when just using Json(obj) – Brian Ogden Oct 31 '19 at 01:47
  • @GabrielLuci you are wrong I can see the difference in the responses watching network traffic, using Json(obj) to return JsonResult is a JSON object over the wire and when using the JsonConvertSerizalizeObject it is a JSON string over the wire – Brian Ogden Oct 31 '19 at 01:53
  • 2
    https://stackoverflow.com/questions/17244774/proper-json-serialization-in-mvc-4 contains (directly or as links) pretty much all sensible ways to return JSON with custom serialization. It looks like that exactly what you are looking for and the second part of the question (about `JSON.parse`) is due to double conversion (Object to JSON string to JSON with a string) which should disappear when actually returning JSON of original object. – Alexei Levenkov Oct 31 '19 at 01:55
  • @AlexeiLevenkov Good eye. I didn't pick up on the double serialization. – Gabriel Luci Oct 31 '19 at 02:55

1 Answers1

1

This will work

  public object Program(int id, int userId)
  {
      var serializerSettings = new JsonSerializerSettings();
      serializerSettings.ContractResolver = new 
      CamelCasePropertyNamesContractResolver();
      var json = JsonConvert.SerializeObject(program, serializerSettings);
      return json;
}
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
codein
  • 317
  • 2
  • 12
  • This does work, although I am not sure it is ideal, it would be nice to still return a JsonResult as that conforms the ASP.NET MVC common pattern seen in many ASP.NET MVC applications – Brian Ogden Oct 31 '19 at 01:56
  • I don't think there's a specific diff or performance benefit of using JsonResult class except that it sets the ContentType to application/json if it's not already set. It also serializes the data using javascript serializer (faster than json converter ?) and write the serialized data to the response stream. – codein Oct 31 '19 at 02:13
  • No I don't disagree, it is just that pattern of JsonResult as always the return type that would be ideal to follow – Brian Ogden Oct 31 '19 at 02:15
  • I do appreciate your answer and voted it up – Brian Ogden Oct 31 '19 at 02:19