-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Apply DictionaryKeyPolicy to enum keys (and other primitive key types?) #47765
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsDescriptionExample for reproducing issue: public enum ETestEnum {
TestValue1 = 1,
TestValue2 = 2
};
public class Info
{
public Dictionary<ETestEnum, int> Queue { get; set; }
}
public void Main(string[] args) {
string jsonString = JsonSerializer.Serialize(new Info
{
Queue = new Dictionary<ETestEnum, int> { [ETestEnum.TestValue1] = 1 },
}, new JsonSerializerOptions {
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
Converters = {
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
}
} );
} jsonString in result: {"queue":{"TestValue1":1}} Key name in dictionary not in camel case. I investigated little bit and find out that DictionaryKeyPolicy and Converters (in my case JsonStringEnumConverter(JsonNamingPolicy.CamelCase)) don't executing while serialization process. Configuration
Regression?I don't know because used Newtonsoft.Json.
|
That seems like a bug to me. For example using the same serializer options I get the following behaviour: var dict = new Dictionary<ETestEnum, ETestEnum> { [ETestEnum.TestValue1] = ETestEnum.TestValue1 };
JsonSerializer.Serialize(dict, options); // "{\"TestValue1\":\"testValue1\"}" @layomia thoughts? |
cc @jozkee When we designed the feature to support non-string primitives as dictionary keys #32676, we elected to keep applying |
@layomia I made a quick research. In my opinion |
E.g. we use |
Even if you decide that |
I think this issue is not about what format we write data types with, but about whether to apply the dictionary naming policy to instances of those types when they are dictionary keys. It doesn't seem meaningful to apply something like camelCase policy (as a concrete example) to a guid.
From the example from @eiriktsarpalis above, the policy is being applied to the dictionary value as expected. I do think it makes sense to apply the policy to enum keys. It would be a breaking change to do this however, but it might be worth it since the workaround of having a custom converter for the parent dictionary could be cumbersome. [I'd normally push something like this to future to wait for more customer feedback, but since it would be a breaking change we should address 6.0 before more folks depend on behavior that we want to change.] |
Marking this as up-for-grabs. The task is to apply DictionaryKeyPolicy to enum keys on serialization. I can help with any questions. |
@layomia Hey, I would like to work on this one. |
We have an open PR to fix this #54429, but it won't block the release if we don't get this functionality in. I'll mark as .NET 7. |
Fixed by #54429. |
* origin/main: (64 commits) [wasm][debugger] Create test Inherited Properties (dotnet#56754) Mark new test as incompatible with GC Mark4781_1GcStressIncompatible (dotnet#56739) Ensure MetadataEnumResult is sufficiently updated by MetaDataImport::Enum (dotnet#56756) [mono] Remove gdb xdebug and binary writer support, it hasn't worked in a while. (dotnet#56759) Update windows-requirements.md (dotnet#56476) Update doc and generic parameter name for JsonValue.GetValue (dotnet#56639) [wasm][debugger] Inspect static class (dotnet#56740) Fix stack overflow handling issue in GC stress (dotnet#56733) Use ReflectionOnly as serialization mode in case dynamic code runtime feature is not supported (dotnet#56604) Move Windows Compat pack to NuGet pack task (dotnet#56686) Fix build error when building some packages (dotnet#56767) Simplify JIT shutdown logic in crossgen2 (dotnet#56687) Fix race in crossdac publishing with PGO (dotnet#56762) Add DictionaryKeyPolicy support for EnumConverter [dotnet#47765] (dotnet#54429) Use ComWrappers in some Marshal unit-tests and update platform metadata (dotnet#56595) Set `DisableImplicitNamespaceImports_Dotnet=true` to workaround sdk issue (dotnet#56744) Make sure ServerGCHeapDetails is up to date (dotnet#56056) [libraries] Reenable System.Diagnostics.DiagnosticSorce.Switches.Tests on mobile (dotnet#56737) Disable failing arm64 win10 Graphics.FromHdc tests (dotnet#56732) Match xplat event source conditions (dotnet#56435) ...
…ger_proxy_attribute * origin/main: (340 commits) add RID for Debian 11 (dotnet#56789) [wasm] [debugger] Skip thread static field (dotnet#56749) Fix timeouts in coreroot_determinism test in GC stress mode (dotnet#56770) Use File.OpenHandle in Socket.SendFile directly (dotnet#56777) accept empty realm for digest auth (dotnet#56369) (dotnet#56455) [wasm][debugger] Create test Inherited Properties (dotnet#56754) Mark new test as incompatible with GC Mark4781_1GcStressIncompatible (dotnet#56739) Ensure MetadataEnumResult is sufficiently updated by MetaDataImport::Enum (dotnet#56756) [mono] Remove gdb xdebug and binary writer support, it hasn't worked in a while. (dotnet#56759) Update windows-requirements.md (dotnet#56476) Update doc and generic parameter name for JsonValue.GetValue (dotnet#56639) [wasm][debugger] Inspect static class (dotnet#56740) Fix stack overflow handling issue in GC stress (dotnet#56733) Use ReflectionOnly as serialization mode in case dynamic code runtime feature is not supported (dotnet#56604) Move Windows Compat pack to NuGet pack task (dotnet#56686) Fix build error when building some packages (dotnet#56767) Simplify JIT shutdown logic in crossgen2 (dotnet#56687) Fix race in crossdac publishing with PGO (dotnet#56762) Add DictionaryKeyPolicy support for EnumConverter [dotnet#47765] (dotnet#54429) Use ComWrappers in some Marshal unit-tests and update platform metadata (dotnet#56595) ...
Description
Example for reproducing issue:
jsonString in result: {"queue":{"TestValue1":1}}
jsonString in expected result: {"queue":{"testValue1":1}}
Key name in dictionary not in camel case.
I investigated little bit and find out that DictionaryKeyPolicy and Converters (in my case JsonStringEnumConverter(JsonNamingPolicy.CamelCase)) don't executing while serialization process.
Configuration
Regression?
I don't know because used Newtonsoft.Json.
The text was updated successfully, but these errors were encountered: