0

I have a simple class like so, which I'd like to serialize and send to the client in camel case notation.

I've declared the class in C# honoring the conventional Pascal case notation.

I've also set the JsonProperty attribute on each property with a name override as follows.

using Newtonsoft.Json;

namespace Code.ViewModels
{
    public class ArticleCategoryListItem
    {
        [JsonProperty(PropertyName = "value")]
        public string Value { get; set; }

        [JsonProperty(PropertyName = "label")]
        public string Label { get; set; }
    }
}

However, my client still receives Pascal case property names.

I've tried clearing the ASP.NET cache, cleaning the build and rebuilding the solution and restarting Visual Studio, all to no avail. What's going on?

enter image description here

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • Have you tried this [answer](https://stackoverflow.com/a/22483122/6300600)? – Swift Sep 14 '18 at 07:48
  • Thank you. I know how to set the naming strategy. And if all else fails, I will resort to that. But I have done this thing a million times before. I am interested in knowing why this *simple* thing doesn't work? – Water Cooler v2 Sep 14 '18 at 07:50
  • Well according to their [docs](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_JsonProperty.htm) it says the attribute `Maps a JSON property to a .NET member or constructor parameter.`. I believe it's like what they said, it only maps from Json to .NET and not the other way round when u serialize it. – Swift Sep 14 '18 at 07:54
  • @SwiftingDuster Thank you. That could be it. I do have a foggy recollection of having used this many times over the past but can't be sure as to whether it was one way or the other. If you're sure, could you please put that down as the answer? – Water Cooler v2 Sep 14 '18 at 08:03
  • BTW, I tried what you say and it works. – Water Cooler v2 Sep 14 '18 at 08:03
  • 2
    @SwiftingDuster The mapping is bidirectional. https://dotnetfiddle.net/amUqq4 –  Sep 14 '18 at 08:10
  • @AndyJ You're right. I just tested it in Visual Studio and it indeed is two way. That is strange because I actually encountered this problem before as well and assumed it was one way because of how the docs description worded it. – Swift Sep 14 '18 at 08:19

2 Answers2

3

As far as I can tell from the source code, JSON.Net isn't used by JsonResult.

It instead uses the JavaScriptSerializer.

I'm not 100% sure that the version you're using doesn't use JSON.Net, but if it doesn't then that obviously would explain why the attributes aren't being honored.

I have previously used a JsonDotNetResult in situations like this.

  • You're right, looks like Json.NET still isn't used in MVC 5, see [this answer](https://stackoverflow.com/a/50665725) to [Setting the default JSON serializer in ASP.NET MVC](https://stackoverflow.com/q/14591750), [Using JSON.NET to return ActionResult](https://stackoverflow.com/q/23348262) and also [How to use Json.NET for JSON modelbinding in an MVC5 project?](https://stackoverflow.com/q/23995210). – dbc Sep 14 '18 at 09:27
0

You have to set JsonSerializationSetting for camel case ContractResolver = new CamelCasePropertyNamesContractResolver()

and use like JsonConvert.SerializeObject(object, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

programtreasures
  • 4,250
  • 1
  • 10
  • 29
  • Thank you. I know how to set the naming strategy. And if all else fails, I will resort to that. But I have done this thing a million times before. I am interested in knowing why this *simple* thing doesn't work? – Water Cooler v2 Sep 14 '18 at 07:50