Serialize enum as string: net10.0, ConfigureHttpJsonOptions, DataContractJsonSerializer and source-generation samples - #2160
Merged
vladimir-pecanac-main merged 2 commits intoAug 31, 2026
Conversation
… DataContractJsonSerializer and source-generation samples Retarget all seven projects from net6.0 to net10.0 and refresh packages against NuGet: Newtonsoft.Json 13.0.4, Microsoft.AspNetCore.Mvc.NewtonsoftJson 10.0.11, Microsoft.AspNetCore.Mvc.Testing 10.0.11, Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 3.1.5 (the v2-compatible line), coverlet.collector 10.0.1. The System.Text.Json package references are dropped because the library is in-box on net10.0. Minimal API sample now uses ConfigureHttpJsonOptions instead of Configure<JsonOptions>; both are the same Microsoft.AspNetCore.Http.Json.JsonOptions object, and the extension is the first-class way to reach it. Dead app.UseAuthorization() removed from both Web API samples: neither registers authentication or authorization. New coverage: - JsonStringEnumMemberName, both with a registered JsonStringEnumConverter (custom string) and without one (still a number). - DataContractJsonSerializer: enums are always written as numbers, EnumMember is ignored, reading the string form back throws, an undefined number is accepted, and the XML DataContractSerializer does honour EnumMember on the same type. - Source-generated serialization through a JsonSerializerContext with UseStringEnumConverter set. Build clean with 0 warnings, 27 tests passing on SDK 10.0.302.
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.
Refresh of
json-csharp/JsonSerializationOfEnumAsString, the sample behind How to Serialize Enum to a String in C#, which has not been touched since 2022.Target framework and packages
All seven projects move from
net6.0(out of support) tonet10.0. Package versions were read from NuGet today, not from memory:Newtonsoft.Json13.0.4,Microsoft.AspNetCore.Mvc.NewtonsoftJson10.0.11,Microsoft.AspNetCore.Mvc.Testing10.0.11,Microsoft.NET.Test.Sdk18.9.0,xunit2.9.3,xunit.runner.visualstudio3.1.5,coverlet.collector10.0.1. The runner stays on the 3.x line because 4.0.0 is the xUnit v3 line and this folder is on xUnit v2. TheSystem.Text.Jsonpackage references are gone: the library is in-box onnet10.0.Sample code
builder.Services.Configure<JsonOptions>(...)becomesbuilder.Services.ConfigureHttpJsonOptions(...). Both configure the sameMicrosoft.AspNetCore.Http.Json.JsonOptionsobject; the extension is the current way to reach it, and it drops theusing Microsoft.AspNetCore.Http.Json;directive.app.UseAuthorization()with no authentication or authorization registered anywhere in the solution. Removed.New tests
[JsonStringEnumMemberName](.NET 9+): with a registeredJsonStringEnumConverterthe custom string is emitted, and without one the same model still writes a number. The attribute does nothing on its own, which is the trap worth covering.DataContractJsonSerializer: enums are always written as numbers,[EnumMember]is ignored, reading the string form back throws aSerializationException, a number no member defines is accepted, and the XMLDataContractSerializerdoes honour[EnumMember]on the very same type. The class is in-box onnet10.0and needs no package reference.JsonSerializerContextwithUseStringEnumConverter = true, the AOT-safe form theIL3050analyzer points at.The existing test asserting that
System.Text.Jsonignores[EnumMember]is kept unchanged: that behaviour is still current on .NET 10 and the article states it.dotnet build -c Release: 0 errors, 0 warnings.dotnet test -c Release: 27 passed, 0 failed (SDK 10.0.302, runtime 10.0.10).