8

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

3

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I must be missing something... I don't see an answer here.
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;
});

Comments

0

In my case, I had the problem when POSTing to the server, e.g.:

{
   "myPropertyCamelCase": "..."
}

And got an error saying "MyPropertyCamelCase" was not set in the request, during model binding. The problem was a missing setter on the request property, e.g.

public MyPropertyCamelCase { get; }

should be

public MyPropertyCamelCase { get; set; }

Not sure if this is directly related.

Comments

-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;
 };

1 Comment

The OP stated they are not using Newtonsoft.Json
-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
                });

1 Comment

The question clearly states he replaced NewtonSoft with System.Text.Json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.