ASP.NET Core 3.0 System.Text.Json Camel Case Serialization
C#Jsonasp.net Coresystem.text.jsonC# Problem Overview
In ASP.NET Core 3.0 Web API project, how do you specify System.Text.Json serialization options to serialize/deserialize Pascal Case properties to Camel Case and vice versa automatically?
Given a model with Pascal Case properties such as:
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
}
And code to use System.Text.Json to deserialize a JSON string to type of Person
class:
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Deserialize<Person>(json);
Does not successfully deserialize unless JsonPropertyName is used with each property like:
public class Person
{
[JsonPropertyName("firstname")]
public string Firstname { get; set; }
[JsonPropertyName("lastname")]
public string Lastname { get; set; }
}
I tried the following in startup.cs
, but it did not help in terms of still needing JsonPropertyName
:
services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
// also the following given it's a Web API project
services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
How can you set Camel Case serialize/deserialize in ASP.NET Core 3.0 using the new System.Text.Json namespace?
Thanks!
C# Solutions
Solution 1 - C#
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);
Solution 2 - C#
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.
Solution 3 - C#
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;
);
Solution 4 - C#
You can use PropertyNameCaseInsensitive
. You need to pass it as a parameter to the deserializer.
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var person = JsonSerializer.Deserialize<Person>(json, options);
which (from the docs):
> Gets or sets a value that determines whether a property's name uses a > case-insensitive comparison during deserialization. The default value > is false
So, it doesn't specify camelCase or PascalCase but it will use case-insensitive comparison.
The below will configure System.Text.Json for Json passed through a controller endpoint:
services.AddControllers()
.AddJsonOptions(options => {
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
Solution 5 - C#
You can still set it application wide by installing Microsoft.AspNetCore.Mvc.NewtonsoftJson
Nuget Package, which allows you to use the previous Json serializer implementation :
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
Credits to Poke, answer found here : https://stackoverflow.com/questions/55666826/where-did-imvcbuilder-addjsonoptions-go-in-net-core-3-0
Solution 6 - C#
Try this!
In StartUp.cs
inside the ConfigureServices
method write:
services.AddMvc()
.AddJsonOptions(options =>
options.JsonSerializerOptions.PropertyNamingPolicy
= JsonNamingPolicy.CamelCase);
You need namespaces such as Newtonsoft.Json.Serialization
& System.Text.Json