Comprehensive Guide to Camel Case Serialization with System.Text.Json in ASP.NET Core 3.0

Dec 06, 2025 · Programming · 11 views · 7.8

Keywords: System.Text.Json | Camel Case | Serialization | ASP.NET Core 3.0 | JsonSerializerOptions

Abstract: This article provides an in-depth exploration of how to configure System.Text.Json in ASP.NET Core 3.0 Web API projects for automatic serialization and deserialization between Pascal Case properties and Camel Case JSON. Based on the best-practice answer, it details setting PropertyNamingPolicy to JsonNamingPolicy.CamelCase via JsonSerializerOptions, contrasting MVC configuration with standalone JsonSerializer usage. Through code examples and step-by-step explanations, it helps developers avoid common pitfalls, such as relying incorrectly on AddJsonOptions without passing options directly. Additionally, it supplements with other configurations, like setting PropertyNamingPolicy to null for Pascal Case retention, and highlights the performance and cross-platform advantages of System.Text.Json. Keywords include System.Text.Json, Camel Case, Serialization, ASP.NET Core 3.0, and JsonSerializerOptions.

Introduction

In ASP.NET Core 3.0, System.Text.Json serves as the default JSON serialization library, replacing Newtonsoft.Json to offer higher performance and better cross-platform support. However, many developers encounter configuration issues with property naming policies when migrating or starting new projects, particularly in achieving automatic conversion between Pascal Case (e.g., Firstname) and Camel Case (e.g., firstname). Drawing from community Q&A data, this article delves into best practices to help you correctly configure System.Text.Json for your serialization needs.

Core Problem Analysis

In ASP.NET Core 3.0 Web API projects, when using System.Text.Json for serialization or deserialization, it does not automatically convert Pascal Case property names to Camel Case JSON keys by default, and vice versa. For example, given a Person class:

public class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

Attempting to deserialize a JSON string "{\"firstname\":\"John\",\"lastname\":\"Smith\"}" using JsonSerializer.Deserialize<Person>(json) will fail due to property name mismatches. This persists unless attributes like [JsonPropertyName("firstname")] are added to each property, which increases code redundancy. The root cause lies in improper configuration of naming policies.

Best Practice Solution

According to the top answer (score 10.0), the key is to use JsonSerializerOptions and set PropertyNamingPolicy to JsonNamingPolicy.CamelCase. This can be achieved in two ways: within MVC configuration or by passing options directly when using JsonSerializer.

First, for scenarios involving standalone use of JsonSerializer in code, create a JsonSerializerOptions instance and specify the naming policy:

var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Deserialize<Person>(json, options);

This enables System.Text.Json to automatically map Camel Case keys in JSON to Pascal Case properties in the Person class, eliminating the need for extra attribute annotations. Similarly, during serialization, JsonSerializer.Serialize(person, options) outputs property names in Camel Case.

MVC Configuration and Limitations

Many developers attempt to configure naming policies in Startup.cs via AddJsonOptions, for example:

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

This configuration applies only to serialization within the MVC framework, such as JSON responses from controllers. However, if you directly call JsonSerializer.Deserialize or JsonSerializer.Serialize in your code, these global settings are not automatically applied; you must explicitly pass JsonSerializerOptions. This common misunderstanding leads to ineffective configurations. Therefore, distinguishing between MVC configuration and standalone usage is crucial.

Supplementary Configuration Options

Other answers (scores 4.4 and 3.7) provide useful supplements. For instance, setting PropertyNamingPolicy = null maintains Pascal Case serialization, preventing automatic conversion to Camel Case:

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

This is beneficial when compatibility with existing systems or adherence to specific naming conventions is required. Note that System.Text.Json currently supports only JsonNamingPolicy.CamelCase and null (default Pascal Case), unlike Newtonsoft.Json which offers more options like Snake Case. Additionally, DictionaryKeyPolicy can be used to configure naming policies for dictionary keys, but it is typically handled separately from property naming.

Performance and Cross-Platform Advantages

As the default choice in ASP.NET Core 3.0, System.Text.Json offers approximately 2-3 times faster serialization speeds and reduced memory allocation compared to Newtonsoft.Json. It natively supports cross-platform environments without additional dependencies. By correctly configuring naming policies, developers can leverage these advantages while avoiding compatibility issues. For example, in microservices architectures, uniform Camel Case JSON formats simplify API communication.

Practical Application Recommendations

In real-world projects, it is advisable to combine MVC configuration with standalone option passing. Set global Camel Case policies in Startup.cs to ensure consistent API responses; for custom serialization in code, create shared JsonSerializerOptions instances to enhance performance. For example, define a static class:

public static class JsonOptions
{
    public static JsonSerializerOptions CamelCaseOptions = new JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    };
}

Then reuse it throughout the application. This avoids redundant option object creation and ensures uniform behavior.

Conclusion

In summary, the core of implementing Camel Case serialization with System.Text.Json in ASP.NET Core 3.0 lies in correctly configuring PropertyNamingPolicy. Passing options directly via JsonSerializerOptions is the most reliable method, while MVC configuration applies only within the framework. Developers should understand these distinctions and choose appropriate strategies based on project requirements. As the .NET ecosystem evolves, System.Text.Json will continue to optimize, providing a solid foundation for high-performance JSON processing.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.