Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 870
Alexander Staroselsky Avatar asked Oct 20 '19 19:10

Alexander Staroselsky


People also ask

Which is better Newtonsoft JSON or System text JSON?

Json does case-insensitive property name matching by default. The System. Text. Json default is case-sensitive, which gives better performance since it's doing an exact match.

How do I deserialize a JSON string to an object in C#?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is JsonProperty annotation C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.


2 Answers

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); 
like image 56
Kahbazi Avatar answered Sep 16 '22 18:09

Kahbazi


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.

like image 42
sutherlandahoy Avatar answered Sep 17 '22 18:09

sutherlandahoy