liveBook · Manning

System.Text.Json

As explained in ASP.NET Core in Action, 3e

System.Text.Json is the .NET library for reading and writing JSON data.

System.Text.Json is the .NET library for reading and writing JSON. ASP.NET Core uses it by default because it provides high-performance, low-allocation serialization and deserialization, while offering less flexibility than Newtonsoft.Json. section 7.3 section 20.7

In minimal APIs, System.Text.Json handles JSON request bodies and responses automatically:

  • Deserialize one JSON request body into a single complex handler parameter. section 5.2.3 section 7.3
  • Serialize endpoint results as JSON when returning API responses. section 7.3
  • Customize options such as trailing commas, camelCase property names, and case-insensitive property matching. section 7.3

Binding a JSON request

A minimal API can accept a Product parameter directly. ASP.NET Core deserializes the JSON request body into a Product instance before invoking the handler. The request must contain valid JSON, and only one handler parameter can bind this way. section 7.3

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureRouteHandlerJsonOptions(o => {
   o.SerializerOptions.AllowTrailingCommas = true;
   o.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
   o.SerializerOptions.PropertyNameCaseInsensitive = true;
});

Use route-handler JSON options to permit trailing commas, apply camelCase naming, and accept property names without matching case. section 7.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains automatic binding and selected serializer options, but defers broader model binding and advanced customization.
Examples
Uses Fruit and Product request models, Postman requests, serializer-option configuration, and an NSwag-generated client.
What it assumes
Assumes basic C# records and minimal API handlers while demonstrating JSON binding through small endpoint examples.
What you can do after
Apply it via ASP.NET Core to bind JSON requests, serialize responses, and configure common naming and syntax options.
How it connects
Connects JSON serialization with minimal APIs, ASP.NET Core, model binding, Newtonsoft.Json, web API controllers, and NSwag.

Also explained in

Compare how 3 books explain System.Text.Json →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.