JsonSerializerOptions created per call (CA1869)
Key dotnet.performance.jsonserializeroptions-per-call · Severity warning · Category performance
Microsoft analyzer CA1869. Flags a new JsonSerializerOptions built for a single serialize or deserialize call. The options object caches type metadata; a fresh one per call rebuilds it every time.
Incorrect
Section titled “Incorrect”return JsonSerializer.Serialize(order, new JsonSerializerOptions { WriteIndented = true });Correct
Section titled “Correct”private static readonly JsonSerializerOptions Indented = new() { WriteIndented = true };
return JsonSerializer.Serialize(order, Indented);