CamelCase serialization: retarget net10.0, cache the options instance, add the web preset - #2151
Open
vladimir-pecanac-main wants to merge 1 commit into
Open
Conversation
…, add the web preset - Both projects net7.0 -> net10.0. - Test packages to current stable: Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1. - JsonSerializerExtensions now holds one static readonly JsonSerializerOptions instead of building a fresh instance per call, and DeserializeFromCamelCase<T> returns T? (Deserialize<T> can return null; the old signature raised CS8603). - Program.cs demonstrates new JsonSerializerOptions(JsonSerializerDefaults.Web), the preset ASP.NET Core applies, with a covering test. - ExtensionTests: xUnit assertion arguments were reversed (Assert.Equal(actual, expected)); swapped to (expected, actual) and used Assert.True for the boolean.
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 Using System.Text.Json for Camel Case Serialization, which is being rewritten and republished.
The article's title is also being repaired in the same pass: an automated retitle on 2026-07-20 took the page from 687 impressions a day to 230 (down 67%) while the whole site fell 25% over the same windows, and position slipped 7.4 to 9.0, so the new title is a repair of measured damage rather than an experiment.
What changes in
json-csharp/CamelCaseSerializationnet7.0tonet10.0. net7.0 is out of support and the framework version was the article's only version string.Microsoft.NET.Test.Sdk18.9.0,xunit2.9.3,xunit.runner.visualstudio4.0.0,coverlet.collector10.0.1. They were three years old (17.3.2 / 2.4.2 / 2.4.5 / 3.1.2).JsonSerializerExtensionskeeps onestatic readonly JsonSerializerOptionsinstead of constructing a fresh instance inside each method.JsonSerializerOptionscaches the serialization metadata it builds per instance; measured on net10.0 with a countingJsonNamingPolicy, five serializations of the same four-property type cost 4ConvertNamecalls through one shared instance and 20 through a fresh instance per call.DeserializeFromCamelCase<T>now returnsT?.JsonSerializer.Deserialize<T>returnsT?and both projects have<Nullable>enable</Nullable>, so the old signature raisedwarning CS8603: Possible null reference return.Program.csdemonstratesnew JsonSerializerOptions(JsonSerializerDefaults.Web), the preset ASP.NET Core already applies, with a covering test. The rewritten article gains a section answering "does ASP.NET Core camelCase by default?", and that claim needed a runnable counterpart here.ExtensionTestshad its xUnit assertion arguments reversed. All five calls readAssert.Equal(actual, expected); the signature isAssert.Equal(expected, actual), so a failure would have named the wrong value as expected. Swapped, andAssert.Equal(result.IsActive, true)becameAssert.True(result.IsActive).Person.csandPersonWithAttributes.csare untouched: the nullable properties are deliberate and the article explains why they are there.Build and tests
dotnet build -c Release: 0 errors, 1 warning.dotnet test -c Release: 3 passed, 0 failed (was 2, plus the new web-preset test). SDK 10.0.302, runtime 10.0.10.The one remaining warning is pre-existing and deliberately left alone:
Program.cs(27)CS8602onpersonFromString.FirstName, whereJsonSerializer.Deserialize<PersonWithAttributes>returns a nullable. Those exact lines are quoted verbatim in the article'sJsonPropertyNamesection, which this rewrite does not change, so touching them here would put the sample and the published snippet out of step. It is a separate, article-visible edit.