Skip to content

Allow serializing combined [Flags] enum values - #66

Open
armadavalor wants to merge 1 commit into
SamboyCoding:masterfrom
armadavalor:fix/flags-enum-serialization
Open

armadavalor wants to merge 1 commit into
SamboyCoding:masterfrom
armadavalor:fix/flags-enum-serialization

Conversation

@armadavalor

Copy link
Copy Markdown

Serializing a [Flags] enum field holding more than one flag (e.g. Flag1 | Flag3) threw "does not declare a name for that value", because the enum serializer uses Enum.GetName, which only returns names of single declared values.

The serializer now falls back to ToString() for [Flags] enums, which writes "Flag1, Flag3". The deserializer already reads that back through Enum.Parse. If the value has a bit with no declared name, ToString() returns a number, and that case still throws as before. Non-flags enums are unchanged.

Tests: added CanSerializeAndDeserializeCombinedFlagsEnum (round trip) and SerializingAFlagsEnumWithAnUndeclaredBitThrows. dotnet test Tomlet.Tests passes 166/166 on net9; the round-trip test fails without the change. Only the net9 test target was run, not the other library targets.

Closes #64

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

Successfully merging this pull request may close these issues.

Enum with [Flags] attribute cannot be serialized.

1 participant