You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
}
The text was updated successfully, but these errors were encountered:
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:
Output from the test:
The text was updated successfully, but these errors were encountered: