Skip to content

JsonStringEnumConverter always returns a default value #100852

@karatakos

Description

@karatakos

Description

When deserialising JSON containing a string that needs to be converted to an enum via JsonStringEnumConverter, the converter always deserialises with the first value of the enum.

Reproduction Steps

    [Test]
    public void JsonEnumStringConverterTest() {
        const string json = 
        """
        { 
            "foo": 0,
            "bar": "secondvalue"
        }
        """;

        var opts = new JsonSerializerOptions();

        opts.Converters.Add(new JsonStringEnumConverter());

        var value = JsonSerializer.Deserialize<SomeType>(json, opts);

        value.Bar.Should().Be(Enum.Parse<SomeTypeEnum>("secondvalue", ignoreCase: true));
    }

    public record SomeType(int Foo, SomeTypeEnum Bar);

    public enum SomeTypeEnum {
        FirstValue,
        SecondValue
    }

Expected behavior

Bar should be SecondValue

Actual behavior

Bar is FirstValue

Regression?

No response

Known Workarounds

No response

Configuration

8.0.201
MacOS 14.2.1 (23C71)

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions