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

StringEnumConverter.AllowIntegerValues confusing documentation #2943

Open
hartmark opened this issue Apr 16, 2024 · 0 comments
Open

StringEnumConverter.AllowIntegerValues confusing documentation #2943

hartmark opened this issue Apr 16, 2024 · 0 comments

Comments

@hartmark
Copy link

I think it's a bit confusing that StringEnumConverter.AllowIntegerValues has the documentation:
"Gets or sets a value indicating whether integer values are allowed when serializing and deserializing."

That gives the impression that that when you serialize you will get the int value of the enum. If I just remove the converter the test passes.

Code reproducing the issue using xunit:


    [Fact]
    public void EnumsAsInt()
    {
        var value = new
        {
            Value = Foo.Bar
        };

        var settings = new JsonSerializerSettings
        {
            Converters =
            [
                new StringEnumConverter { AllowIntegerValues = true }
            ]
        };

        var json = JsonConvert.SerializeObject(value, Formatting.Indented, settings);
        testOutputHelper.WriteLine(json);
        Assert.Contains("1", json);
    }

    private enum Foo
    {
        NotSet = 0,
        Bar = 1
    }

Output from the test:

Xunit.Sdk.ContainsException
Assert.Contains() Failure: Sub-string not found
String:    "{\r\n  "value": "Bar"\r\n}"
Not found: "1"
   at UnitTests.JsonSerializationTest.EnumsAsInt() in C:\KöttKött\JsonSerializationTest.cs:line 99
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)



{
  "value": "Bar"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant