Global JSON options: retarget net10.0, configure Newtonsoft through MVC - #2161
Open
vladimir-pecanac-main wants to merge 1 commit into
Open
Global JSON options: retarget net10.0, configure Newtonsoft through MVC#2161vladimir-pecanac-main wants to merge 1 commit into
vladimir-pecanac-main wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
net8.0->net10.0.Newtonsoft.Json13.0.3 -> 13.0.4,Microsoft.AspNetCore.Mvc.Testing8.0.4 -> 10.0.11,Microsoft.NET.Test.Sdk17.6.0 -> 18.9.0, MSTest 3.0.4 -> 4.3.3,coverlet.collector6.0.0 -> 10.0.1. Zero analyzer warnings under MSTest 4.Microsoft.AspNetCore.Mvc.NewtonsoftJson10.0.11.Newtonsoft section rewritten
JsonConvert.DefaultSettingsnever reaches the MVC response pipeline: it only governsJsonConvertcalls we write ourselves. Measured on .NET 10 before the change: an action returning the object ignored the distinctiveDateFormatStringentirely. The sample now usesAddControllers().AddNewtonsoftJson(...), andSaveProductreturns the object instead of hand-serializing it to a string (which also fixed aContent-Type: text/plainresponse).AddNewtonsoftJson()replaces both MVC formatters, so it would take theSystem.Text.Jsonconfiguration out of play for every controller action. The sample teaches all three approaches, so the Newtonsoft registration sits behind aUseNewtonsoftJsonconfiguration flag.Other
Configure<JsonOptions>()->AddControllers().AddJsonOptions(), removing the invisible dependency on whichJsonOptionstheusingdirective brought into scope.AddOpenApi()and no Swashbuckle, and the article never mentions Swagger.MaxDepth = 3removed. 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 withAddNewtonsoftJson()registered.