-
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
JsonSerializer.Serialize an anonymous type, with duplicate nested names throws an unexpected exception #81550
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionUpgrading my project from .netcore 3.1 to .net6 I came across one of my tests failing due to a change in how JsonSerializer.Serialize will serialize an anonymous type. Reproduction Stepsvar obj = new
{
input = new {
foo = "foo"
},
INPUT = new {
foo = "foo"
}
};
var json = JsonSerializer.Serialize(obj); // throws an exception Expected behaviorIn .netcore 3.1 the above would have returned: {"input":{"foo":"bar"},"INPUT":{"foo":"bar"}} Actual behaviorIn .net5+ this exception is thrown instead:
Unhandled exception. System.InvalidOperationException: Members 'input' and 'INPUT' on type '<>f__AnonymousType2 Regression?Worked as I expected in .netcore 3.1 but not in .net5+ Known WorkaroundsIf I were to create a class to represent my anonymous type instead, then I do receive the expected JSON string. public sealed class Obj
{
public Input input {get; set;}
public Input INPUT {get; set;}
}
public sealed class Input
{
public string foo {get;set;}
} And instantiate similar to my anonymous type: var obj = new Obj()
{
input = new Input()
{
foo="bar"
},
INPUT = new Input()
{
foo="bar"
}
};
var json = JsonSerializer.Serialize(obj); I do get my expected JSON string: {"input":{"foo":"bar"},"INPUT":{"foo":"bar"}} Configuration
Other informationI posted this same question to StackOverflow where it was suggested that I open an issue here.
|
I can reproduce. This appears to have been caused by the introduction of constructor deserialization support. Constructor deserialization will attempt to bind constructor parameters to properties using case insensitive comparison, so if the type uses two properties with different casing it will fail at that binding phase. We should consider relaxing this restriction in line with work in #71944. In the meantime, it should be possible to work around your issue using classes instead of anonymous types when you serialize. |
Description
Upgrading my project from .netcore 3.1 to .net6 I came across one of my tests failing due to a change in how JsonSerializer.Serialize will serialize an anonymous type.
Reproduction Steps
Expected behavior
In .netcore 3.1 the above would have returned:
Actual behavior
In .net5+ this exception is thrown instead:
Unhandled exception. System.InvalidOperationException: Members 'input' and 'INPUT' on type '<>f__AnonymousType2
2[<>f__AnonymousType1
1[System.String],<>f__AnonymousType1`1[System.String]]' cannot both bind with parameter 'input' in the deserialization constructor.Regression?
Worked as I expected in .netcore 3.1 but not in .net5+
Known Workarounds
If I were to create a class to represent my anonymous type instead, then I do receive the expected JSON string.
And instantiate similar to my anonymous type:
I do get my expected JSON string:
Configuration
Other information
I posted this same question to StackOverflow where it was suggested that I open an issue here.
https://stackoverflow.com/questions/75317065/exception-serializing-anonymous-type-to-json-when-nested-property-names-are-the
The text was updated successfully, but these errors were encountered: