Skip to content
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

Conversation

lateapexearlyspeed
Copy link
Contributor

Fix #58247

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Dec 9, 2022
@ghost
Copy link

ghost commented Dec 9, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Fix #58247

Author: lateapexearlyspeed
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@lateapexearlyspeed lateapexearlyspeed marked this pull request as ready for review December 14, 2022 07:23
@layomia layomia added this to the 8.0.0 milestone Dec 19, 2022
@eiriktsarpalis
Copy link
Member

@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
@lateapexearlyspeed
Copy link
Contributor Author

there appear to be merge conflicts

Hi @layomia @eiriktsarpalis , conflict resolved.

@jeffhandley
Copy link
Member

@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!

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a 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.

@ghost ghost added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Feb 14, 2023
[GeneratedRegex(NumericPattern)]
private static partial Regex NumericRegex();
#else
private static readonly Regex s_numericRegex = new(NumericPattern);
Copy link
Member

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.

Copy link
Contributor Author

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))
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ericstj ericstj assigned eiriktsarpalis and unassigned layomia Oct 5, 2023
@eiriktsarpalis eiriktsarpalis merged commit 7d429e4 into dotnet:main Oct 6, 2023
106 of 109 checks passed
@ghost ghost locked as resolved and limited conversation to collaborators Nov 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values
7 participants