Advertisement
Intermediate ASP.NET Core, Content Negotiation & Output Formatters

How does System.Text.Json's configuration (via AddJsonOptions) let you customize the default JSON serialization behavior globally?

Short answer

AddJsonOptions lets you configure JsonSerializerOptions applied to every JSON response and request across the API -- common customizations include property naming policy (camelCase vs PascalCase), handling of null values, enum serialization as strings instead of numbers, and…

Save ASP.NET Core mock interview
Advertisement
AModel answer
AddJsonOptions lets you configure JsonSerializerOptions applied to every JSON response and request across the API -- common customizations include property naming policy (camelCase vs PascalCase), handling of null values, enum serialization as strings instead of numbers, and reference handling for circular object graphs.
builder.Services.AddControllers()
    .AddJsonOptions(options => {
        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
        options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
    });
Real-world example
An API configures global JSON options to serialize enums as readable strings ("Active" instead of 1) and omit null fields entirely, producing cleaner, more JavaScript-idiomatic responses for its frontend consumers.
Likely follow-ups
  • How do these global options interact with per-property [JsonPropertyName] attributes?
  • What's the performance cost of adding custom converters to every serialization?
Related topics