-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom converters for enum dictionary keys not working as expected #94452
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionI'm encountering an issue when trying to deserialize a JSON object to a The deserialization fails with the following error message:
This is surprising because, as of .NET 6, However, it seems that this feature might not be working as expected for enum dictionary keys. I would appreciate it if you could look into this issue. Reproduction Steps
Expected behaviorThe JSON object is successfully deserialized to a Actual behaviorDeserialization fails with a Regression?None available. Known WorkaroundsFirst deserialize the JSON to a await using var statesPerProgramStream = await FileSystem.OpenAppPackageFileAsync("StatesPerProgram.json").ConfigureAwait(false);
// Pass the options to the DeserializeAsync method
var statesPerProgramStr = await JsonSerializer.DeserializeAsync<Dictionary<string, Dictionary<int, UowState>>>(statesPerProgramStream, options,token).ConfigureAwait(false);
Debug.Assert(statesPerProgramStr != null);
// Convert string keys to CsdbProgram enum keys
statesPerProgram = statesPerProgramStr.ToDictionary(
item => Enum.Parse<CsdbProgram>(item.Key),
item => item.Value
); ConfigurationMicrosoft Windows 10.0.19045, OSDImageName is Windows 10 x64 21H2 v8.0. Other informationNo response
|
Cool! This solved my issue. Thank you. |
Description
I'm encountering an issue when trying to deserialize a JSON object to a
Dictionary<CsdbProgram, Dictionary<int, UowState>>
usingSystem.Text.Json
. Here,CsdbProgram
is an enum andUowState
is a class. I'm using custom converters for bothCsdbProgram
andUowState
.The deserialization fails with the following error message:
This is surprising because, as of .NET 6,
System.Text.Json
is supposed to support custom converters for dictionary keys. This feature was added in response to issue #46520 and implemented in pull request #57302.However, it seems that this feature might not be working as expected for enum dictionary keys. I would appreciate it if you could look into this issue.
Reproduction Steps
CsdbProgram
and a classUowState
.CsdbProgram
andUowState
.Dictionary<CsdbProgram, Dictionary<int, UowState>>
usingJsonSerializer.DeserializeAsync
and the custom converters.Expected behavior
The JSON object is successfully deserialized to a
Dictionary<CsdbProgram, Dictionary<int, UowState>>
.Actual behavior
Deserialization fails with a
NotSupportedException
.Regression?
None available.
Known Workarounds
First deserialize the JSON to a
Dictionary<string, Dictionary<int, UowState>>
and then change the dictionary to aDictionary<CsdbProgram, Dictionary<int, UowState>>
usingToDictionary
:Configuration
Microsoft Windows 10.0.19045, OSDImageName is Windows 10 x64 21H2 v8.0.
The version of .NET I am using is .NET 8.0 (8.0.100-rc.2.23502.2).
The target frameworks I am using are net8.0-android, net8.0-ios, net8.0-maccatalyst, and net8.0-windows10.0.19041.0.
The supported OS platform versions are 14.2 for iOS, 14.0 for MacCatalyst, 24.0 for Android, 10.0.17763.0 for Windows, and 6.5 for Tizen.
The application title is Bombardier XML Splitter and the application ID is com.bombardier.mauixmlsplitter.
The project URL is https://github.com/Technical-Publications-Bombardier/BaXmlSplitter and the repository type is git.
Other information
No response
The text was updated successfully, but these errors were encountered: