System.Text.Json – Camel Case Serialization and Deserialization

In this article, we will see how to perform Camel Case Serialization and Deserialization in .NET using System.Text.Json

We shall additionally learn to use the same for any non-host applications like .NET Core Console or Winform or WPF application.

We shall see the below aspects in today’s article,

Getting started

Let’s create any ASP.NET Core application. I am using ASP.NET Core 3.1 or ASP.NET 5.0 project template.

Here is the sample API implementation and the response is as below,

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

Camel Case Serialization and Deserialization in NET

The above Controller method returns below WeatherForecast object as JSON,

public class WeatherForecast
    {
        public DateTime Date { get; set; }
        public int TemperatureCelcius { get; set; }
        public int TemperatureFerenhit => 32 + (int)(TemperatureCelcius / 0.5556);
        public string Summary { get; set; }
    }

Camel Case Serialization

To force to apply Came casing for all JSON responses or output, use the AddJsonOption as below,

AddJsonOptions(option =>
option.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);

This produces below JSON response which is Camel Case by default.

So as we see above Camel Case behavior doesn’t apply if the object casing changes like TemperatureCelcius used in the lower cases etc.

JsonNamingPolicy as CamelCase

Please use JsonNamingPolicy as camel case using AddJsonOptions as below,

ASP.NET Core 3.1 or DOT.NET 6.0 (template)

SystemTextJson Camel Case Serialization and Deserialization

ASP.NET Core 2.2 (template)

SystemTextJson Camel Case Serialization and Deserialization

SystemTextJson Camel Case Serialization and Deserialization

JsonNamingPolicy as PascalCase

To force to apply no casing for all JSON responses or output, use the AddJsonOption as below,

AddJsonOptions(option =>
option.JsonSerializerOptions.PropertyNamingPolicy = null);

In this case, the output matches the response object as specified.

Usually, public properties are declared as Pascal Casing in C# Code, and hence when PropertyNamingPolicy is specified as null, it generates the JSON response as Pascal i.e Object’s used property casing.

Due to the same reason you always get an opportunity to switch between Pascal or Camel casing as required.

SystemTextJson Camel Case Serialization and Deserialization

Enable Global Serialization settings

The above-discussed techniques apply the JSON serialization settings globally for all the controller in the ASP.NET Core API or MVC based application.

Using services.AddMvc or service.AddController along with AddJsonOptions helps us enabling global settings for all controllers in a service.

Casing in Custom Components or Console or Desktop application

If you need to handle casing in other custom modules like classes in non-host apps or other components, then please use the serializer as below,

Camel Casing

var serializeOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            };
            var jsonString = JsonSerializer.Serialize(empDetails, serializeOptions);

The above code example shall produce JSON output as below,

SystemTextJson Camel Case Serialization and Deserialization

Pascal Casing

var serializeOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy = null
            };
            var jsonString = JsonSerializer.Serialize(empDetails, serializeOptions);

The above code example shall produce JSON output as properties on target object below,

SystemTextJson Camel Case Serialization and Deserialization

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Reference:

Summary

Today in this article, we learned how to perform Camelcase case Serialization and Deserialization in applications like ASP.NET Core. We looked at the Global setting configuration for ASP.NET based application and also looked at the local settings useful in non-host apps like Console, Winform, etc..



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *