Skip to content

Global JSON options: retarget net10.0, configure Newtonsoft through MVC - #2161

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/116863-global-options
Open

Global JSON options: retarget net10.0, configure Newtonsoft through MVC#2161
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/116863-global-options

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the SEO republish of "JsonSerializerOptions in ASP.NET Core: Set Global Defaults" (aspnetcore-set-global-default-json-serialization-options).

Retarget and package lifts

  • Both projects net8.0 -> net10.0.
  • Newtonsoft.Json 13.0.3 -> 13.0.4, Microsoft.AspNetCore.Mvc.Testing 8.0.4 -> 10.0.11, Microsoft.NET.Test.Sdk 17.6.0 -> 18.9.0, MSTest 3.0.4 -> 4.3.3, coverlet.collector 6.0.0 -> 10.0.1. Zero analyzer warnings under MSTest 4.
  • New: Microsoft.AspNetCore.Mvc.NewtonsoftJson 10.0.11.

Newtonsoft section rewritten
JsonConvert.DefaultSettings never reaches the MVC response pipeline: it only governs JsonConvert calls we write ourselves. Measured on .NET 10 before the change: an action returning the object ignored the distinctive DateFormatString entirely. The sample now uses AddControllers().AddNewtonsoftJson(...), and SaveProduct returns the object instead of hand-serializing it to a string (which also fixed a Content-Type: text/plain response).

AddNewtonsoftJson() replaces both MVC formatters, so it would take the System.Text.Json configuration out of play for every controller action. The sample teaches all three approaches, so the Newtonsoft registration sits behind a UseNewtonsoftJson configuration flag.

Other

  • Configure<JsonOptions>() -> AddControllers().AddJsonOptions(), removing the invisible dependency on which JsonOptions the using directive brought into scope.
  • Swashbuckle wiring removed. The .NET 10 web API template ships AddOpenApi() and no Swashbuckle, and the article never mentions Swagger.
  • MaxDepth = 3 removed. The sample payload is two levels deep, so the setting demonstrates nothing and silently breaks a reader who adds one nesting level.

Verification
SDK 10.0.302, dotnet build -c Release: 0 warnings, 0 errors. dotnet test: 5/5 passing. Three new integration tests cover the System.Text.Json controller path, the Json.NET controller path, and the minimal API endpoint staying on System.Text.Json even with AddNewtonsoftJson() registered.

Retarget both projects to net10.0 and lift the packages: Newtonsoft.Json
13.0.4, Microsoft.AspNetCore.Mvc.Testing 10.0.11, Microsoft.NET.Test.Sdk
18.9.0, MSTest 4.3.3, coverlet.collector 10.0.1.

Configure MVC's JSON options through AddControllers().AddJsonOptions()
instead of Configure<JsonOptions>(), which needed the reader to know which
of the two JsonOptions types the using directive brought into scope.

Replace the JsonConvert.DefaultSettings sample with
AddControllers().AddNewtonsoftJson(). DefaultSettings only governs our own
JsonConvert calls and never reaches the MVC response pipeline, so the old
sample taught per-call serialization under a global-configuration heading.
SaveProduct now returns the object and lets the formatter write it. The
Newtonsoft registration sits behind a UseNewtonsoftJson configuration flag
because it replaces both MVC formatters, and the sample still has to show
the System.Text.Json path.

Drop the Swashbuckle wiring: the .NET 10 web API template no longer ships
it and the article never mentions Swagger.

Drop MaxDepth = 3. The sample payload is two levels deep, so the setting
demonstrates nothing and breaks anyone who adds a nesting level.

Add integration tests covering all three: System.Text.Json options on a
controller action, Json.NET writing the response once AddNewtonsoftJson()
is registered, and the minimal API endpoint staying on System.Text.Json
either way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant