ASP.NET Core JSON serialization after removing Newtonsoft.Json
0 reputation · 02 Aug 2022, 01:02 UTC
0 reputation · 02 Aug 2022, 01:02 UTC
An ASP.NET Core MVC or Web API application targets .NET 6 or .NET 8 and relies on System.Text.Json as the default serializer introduced in .NET Core 3.0. The unresolved decision is whether removing Microsoft.AspNetCore.Mvc.NewtonsoftJson, when present, changes serialization for existing controllers without explicit opt-in.
System.Text.Json ignores Newtonsoft.Json attributes such as [JsonProperty], uses case-sensitive property matching by default, and throws on object reference loops unless configured otherwise. ASP.NET Core applies its own web defaults, so console-library behavior is not a reliable predictor.
The goal is to identify which settings govern request deserialization and response output before changing package references.
37275 reputation · 02 Aug 2022, 06:10 UTC
Yes. On .NET 6 and .NET 8, removing the Microsoft.AspNetCore.Mvc.NewtonsoftJson package and its AddNewtonsoftJson() call leaves System.Text.Json as the only built-in JSON formatter for MVC and Web API controllers, assuming no custom formatters were registered. Controllers without explicit serializer opt-in switch to System.Text.Json behavior immediately after the change.
Microsoft.AspNetCore.Mvc.Json.JsonOptions.JsonSerializerOptions through the MVC pipeline.PropertyNamingPolicy controls output property naming; DictionaryKeyPolicy controls dictionary key naming.PropertyNameCaseInsensitive controls case matching during request deserialization.ReferenceHandler to ReferenceHandler.Preserve to emit $id/$ref, or ReferenceHandler.IgnoreCycles to drop repeated references (confirm availability for the target runtime).[JsonProperty] and [JsonIgnore] are ignored. Use [JsonPropertyName] and [JsonIgnore] from System.Text.Json.Serialization.ASP.NET Core web defaults are not identical to bare System.Text.Json console defaults. Case-insensitive deserialization is commonly enabled in web scenarios, but the exact value should be read from the registered options rather than assumed. Default details can also vary between .NET 6 and .NET 8, so version-specific verification is needed before treating either release as interchangeable.
Output formatting and model binding are separate pipeline concerns, but both commonly read from the same JsonSerializerOptions instance registered through JsonOptions. That means both can be audited by inspecting registered formatters and options without executing either serializer.
Microsoft.AspNetCore.Mvc.NewtonsoftJson PackageReference from the project file.AddNewtonsoftJson() call from Program.cs or Startup.cs. Leaving the call in place causes a compile error after the package is removed.Program.cs:builder.Services.Configure<Microsoft.AspNetCore.Mvc.Json.JsonOptions>(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy =
System.Text.Json.Serialization.JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.DictionaryKeyPolicy =
System.Text.Json.Serialization.JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.ReferenceHandler =
System.Text.Json.Serialization.ReferenceHandler.Preserve;
});
[JsonPropertyName] and [JsonIgnore] from System.Text.Json.Serialization.JsonOutputFormatter appears in output formatters, SystemTextJsonInputFormatter appears in input formatters, and NewtonsoftJsonOutputFormatter is absent.dotnet build and test a controller that returns an object graph plus a request using differently cased properties.Do existing controllers or models use Newtonsoft attributes such as [JsonProperty], [JsonIgnore], or custom JsonConverter types? If yes, simple package removal silently changes naming and ignoring behavior, so attribute migration must precede removal. If no, the steps above are sufficient.
dotnet list package | grep Newtonsoft should return no matches after removal.Program.cs or Startup.cs for AddNewtonsoftJson(); the build will fail if any reference remains.PropertyNamingPolicy, PropertyNameCaseInsensitive, and ReferenceHandler values.Note: This draft is prepared for human review because the research brief flags current verification requirements. Version-specific defaults between .NET 6 and .NET 8 should be confirmed against the installed runtime before production rollout.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 02 Aug 2022, 04:11 UTC
Removing Microsoft.AspNetCore.Mvc.NewtonsoftJson does not by itself guarantee System.Text.Json handles every JSON request if AddNewtonsoftJson() remains in Program.cs; that call can break startup or leave an unexpected formatter registered. In .NET 6 and .NET 8, controllers and minimal APIs can configure separate JsonSerializerOptions instances, so auditing MvcOptions.OutputFormatters and InputFormatters at startup is more reliable than assuming a single global switch.
One version-sensitive detail: ReferenceHandler.IgnoreCycles is available in .NET 8, while .NET 6 offers ReferenceHandler.Preserve or throws on cycles by default. Before removing the package, add a startup diagnostic that prints each formatter type and its SerializerOptions.PropertyNamingPolicy, ReferenceHandler, and PropertyNameCaseInsensitive values.