-
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
Consider numeric string json for EnumConverter. #79432
Consider numeric string json for EnumConverter. #79432
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsFix #58247
|
...raries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/EnumConverterTests.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/EnumConverterTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/EnumConverterTests.cs
Outdated
Show resolved
Hide resolved
@lateapexearlyspeed there appear to be merge conflicts, could you try rebasing your branch over the latest main? |
…nsiderNumericStringEnum # Conflicts: # src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Hi @layomia @eiriktsarpalis , conflict resolved. |
@layomia / @dotnet/area-system-text-json Could you give this a re-review please? The conflicts were resolved and it looks like it could be ready to merge. It'd be nice to get this in for Preview 2. Thanks! |
...raries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CheckIfValidNamedEnumLiteral
is not a correct check for valid IL identifiers.
…ass enum parsing by numeric value.
src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.Tests/EnumTests.fs
Outdated
Show resolved
Hide resolved
...raries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.Tests/EnumTests.fs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.Tests/EnumTests.fs
Outdated
Show resolved
Hide resolved
[GeneratedRegex(NumericPattern)] | ||
private static partial Regex NumericRegex(); | ||
#else | ||
private static readonly Regex s_numericRegex = new(NumericPattern); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-source generated regexes should make use of RegexOptions.Compiled
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
bool success = Enum.TryParse(source, out T result) || Enum.TryParse(source, ignoreCase: true, out result); | ||
bool success; | ||
T result; | ||
if ((_converterOptions & EnumConverterOptions.AllowNumbers) != 0 || !NumericRegex().IsMatch(source)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the NumericRegex()
method be storing the result in s_numericRegex
for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
...raries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Outdated
Show resolved
Hide resolved
...raries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Outdated
Show resolved
Hide resolved
…ion/Converters/Value/EnumConverter.cs
...raries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs
Outdated
Show resolved
Hide resolved
…ion/Converters/Value/EnumConverter.cs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Fix #58247