Skip to content

EnumStringConverter fails on Enum serialization when multiple members have same value #856

@nazar-kuzo

Description

@nazar-kuzo

Nuget Package

Json.More.Net

Package Version

2.1.0

Operating System

Windows

.Net Target (if relevant, please specify the version in the description)

.Net (5 or after)

A clear and concise description of the bug.

EnumStringConverter fails on Enum serialization when multiple members have same value.

This could be reproduced by Serializing System.Net.HttpStatusCode which has both members (Ambiguous, MultipleChoices) assigned to same value (300).

What did you expect?

While this is really edge case scenario, I would say that since C# supports such enum declaration, EnumStringConverter should conform to it.

When serializing, I would expect that it should rely on member name or member DescriptionAttribute without value.
When deserializing, I assume library doesnt support parsing number values, therefore no changes required.

Please add test code or steps to reproduce the behavior.

JsonSerializer.Serialize(HttpStatusCode.BadRequest, new JsonSerializerOptions { Converters = { new EnumStringConverter<HttpStatusCode>() } });

Is there any other information you'd like to share regarding this bug?

Converter fails in EnsureMap() method since it tries to create write cache dictionary based on enum values, which does not support duplicates.

Code of Conduct & Contributor Guidelines

  • I agree to follow this project's Code of Conduct and Contribution Guidelines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpkg:more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions