How do you configure JSON serialization in ASP.NET Core (System.Text.Json)?
Quick Answer
ASP.NET Core uses System.Text.Json by default. Configure it with AddControllers().AddJsonOptions(o => { ... }) — property naming (camelCase is the default), enum-as-string (JsonStringEnumConverter), null handling, and reference handling for cycles. Per-property control uses [JsonPropertyName], [JsonIgnore], [JsonConverter].
The default serializer is System.Text.Json. I configure it in AddJsonOptions — camelCase is already the default, I usually add the string enum converter so enums serialize as names not numbers, and set nulls to be ignored on write. Per-property I use [JsonPropertyName] and [JsonIgnore]. The cycle problem with EF navigation properties I solve by serializing DTOs instead of entities, rather than turning on reference handling. If I need JSON Patch or some Newtonsoft-specific behaviour I add AddNewtonsoftJson.
System.Text.Jsonis fast and allocation-light but stricter than Newtonsoft (case-sensitive by default for deserialization unless configured, no[JsonProperty]).- Common options:
DefaultIgnoreCondition = WhenWritingNull, addJsonStringEnumConverter,PropertyNameCaseInsensitive. - Cycles (EF navigation properties):
ReferenceHandler.IgnoreCyclesor, better, serialize DTOs without navigation properties. - To switch back to Newtonsoft:
AddNewtonsoftJson()(needed for JSON Patch, some legacy behaviour). - Source-generated serializers (
JsonSerializerContext) for AOT / hot paths.