ASP.NET Core 3.0 System.Text.Json Camel Case Serialization ASP.NET Core 3.0 System.Text.Json Camel Case Serialization json json

ASP.NET Core 3.0 System.Text.Json Camel Case Serialization


AddJsonOptions() would config System.Text.Json only for MVC. If you want to use JsonSerializer in your own code you should pass the config to it.

var options = new JsonSerializerOptions{    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,};var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";var person = JsonSerializer.Parse<Person>(json, options);


In startup.cs:

// keeps the casing to that of the model when serializing to json// (default is converting to camelCase)services.AddMvc()    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); 

This means you don't need to import newtonsoft.json.

The only other option for options.JsonSerializerOptions.PropertyNamingPolicy is JsonNamingPolicy.CamelCase. There do not seem to be any other JsonNamingPolicy naming policy options, such as snake_case or PascalCase.


If you want camelCase serialization use this code in Startup.cs: (for example firstName)

services.AddControllers()        .AddJsonOptions(options =>        {            options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;        });

If you want PascalCase serialization use this code in Startup.cs: (for example FirstName)

services.AddControllers()        .AddJsonOptions(options =>        {            options.JsonSerializerOptions.PropertyNamingPolicy= null;        );