-
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
KeyValuePair is not properly serialized with JsonNamingPolicy.CamelCase - System.Text.Json #1197
Comments
I believe the camel case option for @steveharter - can you clarify what your expectations are for that feature? Is there anything we can do to support this scenario? I'd imagine this can be resolved by a custom converter. cc @layomia |
This would require a new naming policy. I don't think we can expand e.g. we'd need to add a |
As a work-around it is possible to create a new converter that does this by coping the converter code from |
Is KeyValuePair the only type that isn't covered by current options? Where else can the concept of "key" show up in .NET types? I don't know if adding a new policy just for a specific type makes sense (unless it happens to be a commonly used type/pattern). Alternatively, we have "KeyPolicy" which works for all "keys" (dictionary, keyvaluepair, etc.). |
Note that Dictionary<,> uses KeyValuePair<,> internally so a new policy |
Key and Value are just properties on the struct. |
That's a good point. It looks like because we have a built-in converter for KeyValuePair, For user-defined structs, that isn't the case, and we (correctly) go down the code-path which calls A trivial fix would be to update if (options.PropertyNamingPolicy != null)
{
// Not great, but would work
string modifiedName = options.PropertyNamingPolicy.ConvertName(name.ToString());
writer.WritePropertyName(modifiedName);
}
else
{
writer.WritePropertyName(name);
} |
@ahsonkhan I'm interested in seeing this fixed and can contribute. What is the purpose of If the converter is required for some reason, then its design is simply flawed, since it is not capable of respecting |
@steveharter, @layomia - can you please help here. |
From @vladinvancouver in https://github.com/dotnet/corefx/issues/41813
|
The serializer only supports public properties with public getters and setters for deserialization. KeyValuePair's
A solution here might be to obtain and cache the correct |
@layomia If done at that point, we would only be able to include support for |
@ArrowCase converters.Add(new JsonKeyValuePairConverter(this)); With this constructor, the |
@layomia can you please file a breaking change issue: https://github.com/dotnet/docs/issues/new?template=dotnet-breaking-change.md |
Removing breaking change as it is tracked with #36424 |
When serializing a collection of KeyValuePair with CamelCase enabled the properties are serialized in PascalCase:
MVC Configuration is:
The result is:
Expected result:
The text was updated successfully, but these errors were encountered: