In ASP.NET Core Web API, JSON serialization is the process of converting C# objects into JSON strings (for responses) and back again (for request bodies).
By default, ASP.NET Core handles this automatically, but understanding how to configure it is essential for building professional APIs.
1. The Default Serializer: System.Text.Json
Since .NET Core 3.0, Microsoft provides a high-performance, low-memory library called System.Text.Json. It is the built-in choice for modern applications and is optimized for speed and security.
How it works automatically
When you return an object from a controller, ASP.NET Core uses a "Formatter" to serialize it:
C#
[HttpGet]
public IActionResult GetUser()
{
var user = new User { Id = 1, Name = "Alice" };
return Ok(user); // Automatically serialized to {"id": 1, "name": "Alice"}
}
2. Configuration & Global Settings
You can customize serialization behavior globally in your Program.cs. Common tasks include enabling "pretty printing" (indentation) or changing how property names are cased.
C#
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
// 1. Indent the JSON for readability
options.JsonSerializerOptions.WriteIndented = true;
// 2. Ignore properties that are null
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
// 3. Keep property names as they are in C# (PascalCase) instead of camelCase
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
3. Using Attributes for Fine Control
Sometimes you only want to change serialization for a specific class or property. You can use attributes directly on your models:
| Attribute | Purpose |
[JsonPropertyName("user_id")] | Renames the property in the JSON output. |
[JsonIgnore] | Prevents the property from being serialized at all. |
[JsonInclude] | Forces a private property to be included. |
[JsonConstructor] | Tells the serializer which constructor to use for deserialization. |
Join the conversation! Your thoughts help the community grow.