Search code examples
jsonasp.net-corejson.netcamelcasing

ASP.NET Core - JsonConvert.DeserializeObject does not respect CamelCasePropertyNamesContractResolver


I need to output some JSON file to client app. So, I have following code

var content = System.IO.File.ReadAllText(this.path);
var model = JsonConvert.DeserializeObject(content, new JsonSerializerSettings() { 
    ContractResolver = new CamelCasePropertyNamesContractResolver() 
});

return Ok(model);

it should be returned as Object with camelCase property names, but I get it the same as it is in JSON file (PascalCase)

enter image description here

to be mentioned, in the Startup file, IMvcBuilder.AddJsonOptions also sets

services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
});

and it works perfectly, other controllers returns objects as expected. The problem is only with object converted from JSON file.

How can I fix that?


Solution

  • Ok, I've found a way to fix it with

    var model = JsonConvert.DeserializeObject<JObject>(content);
    

    and now it works as expected :| enter image description here