6

I've migrated a web api application from net core 5 to 6 and changed NewtonSoft serializer by System.Text.Json. In my Startup.cs, I've configured Json serialization with the following code:

services.AddControllers(config =>
{
    config.RespectBrowserAcceptHeader = true;
    config.ReturnHttpNotAcceptable = true;
})
.AddJsonOptions(options =>
{
    options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.WriteIndented = true;
})
.AddXmlDataContractSerializerFormatters();

This code is working only partially. WriteIndented is working fine (see screen capture below), but I can't get camelcase to work.

enter image description here

Any suggestions? Regards

Jose L. Garcia
  • 316
  • 5
  • 14
  • Hello. The JsonNamingPolicy.CamelCase is used for PascalCase, for camelCase with lover first char, this might be helpfull: https://stackoverflow.com/questions/59559560/change-json-serialization-from-camelcase-to-pascalcase-duplicate-no-solution – Klemikaze Jan 04 '22 at 10:35
  • Hi @Klemikaze, you can check this docs: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties?pivots=dotnet-6-0#use-camel-case-for-all-json-property-names – Jose L. Garcia Jan 04 '22 at 10:41
  • 1
    Can you share a [mcve]? What does the type with `{"Id" : 5, "Nombre": ""}` look like? Is there any chance a custom converter is applied directly, or that the web api method is serializing manually? – dbc Jan 04 '22 at 14:07

4 Answers4

4

After many tries I solved this problem. In latest version there is no default issue with "BaseController" but same issue with "ODataController".

The solution is;

services
.AddControllers(opt => opt.Filters.Add(typeof(ValidateModelAttribute)))
.AddJsonOptions(o => {
   o.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
   o.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
   o.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
   o.JsonSerializerOptions.WriteIndented = true;
})
.AddOData(options => options.Select().Expand().Filter().OrderBy().SetMaxTop(32).Count());

Read more detail on this documents.

Necip Sunmaz
  • 1,546
  • 17
  • 22
0

Check your config. Maybe you are still using Newtonsoft somehow?

Being safe: configure both libraries...

services
.AddMvc()
.AddJsonOptions(options => {
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
})
.AddNewtonsoftJson(options => {
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    // options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
-2

Simply add this in the program.cs file

using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
JsonConvert.DefaultSettings = () =>
 {
   var settings = new JsonSerializerSettings();
   settings.Converters.Add(new StringEnumConverter());
   settings.ContractResolver = new 
   CamelCasePropertyNamesContractResolver();
   return settings;
 };
-3

please try this way: Add option UseCamelCasing in NewtonsoftJson.

services.AddControllers()
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                    options.UseCamelCasing(true); // additional line here
                });