Flags enums are not serialized when multiple flags are set
See original GitHub issueHi,
I’ve encountered an issue with Flags enums. When multiple flags are set the enum value is serialized as null. Works fine with only one flag.
Code to repro:
[Test]
public void FlagsEnumSerializationWorks()
{
var config = new ConfigurationContainer();
var serializer = config.Create();
var input = new FlagsEnumContainer(LogMessageSeverity.Debug | LogMessageSeverity.Informational);
var clone = Clone(input, serializer);
Assert.AreEqual(input.Value, clone.Value);
}
public class FlagsEnumContainer
{
public LogMessageSeverity Value { get; set; }
public FlagsEnumContainer()
{
}
public FlagsEnumContainer(LogMessageSeverity value)
{
Value = value;
}
}
public enum LogMessageSeverity
{
Debug = 0b1,
Informational = 0b10,
Critical = 0b100
}
public T Clone<T>(T objectToClone, IExtendedXmlSerializer serializer)
{
using var stream = new MemoryStream();
serializer.Serialize(stream, objectToClone);
stream.Seek(0, SeekOrigin.Begin);
return serializer.Deserialize<T>(stream);
}
Given the above example, the code:
var input = new FlagsEnumContainer(LogMessageSeverity.Debug | LogMessageSeverity.Informational);
string serialized = serializer.Serialize(new XmlWriterSettings {Indent = true}, input);
produces incorrectly
<?xml version="1.0" encoding="utf-8"?>
<SerializationTests-FlagsEnumContainer xmlns="clr-namespace:AutoSliceAndView.Core.Tests;assembly=AutoSliceAndView.Core.Tests">
<Value xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</SerializationTests-FlagsEnumContainer>
while this snippet works
var input = new FlagsEnumContainer(LogMessageSeverity.Debug);
string serialized = serializer.Serialize(new XmlWriterSettings {Indent = true}, input);
<?xml version="1.0" encoding="utf-8"?>
<SerializationTests-FlagsEnumContainer xmlns="clr-namespace:AutoSliceAndView.Core.Tests;assembly=AutoSliceAndView.Core.Tests">
<Value>Debug</Value>
</SerializationTests-FlagsEnumContainer>
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How can you make a flags enum serialize its combination ...
The problem is that with a flags enum, you're expecting several numerical values to be used that aren't specifically declared.
Read more >Flag-Enums are not serialized correctly #1666
Since the serializer hasn't changed, there's no way to serialize two different flags that represent the same value, hence the reason for ....
Read more >Enum Flags In Unity and C#. Enum Flags are ... - Vincent Taylor
Enum Flags allow for multiple enum values to be applied to a single enum variable. ... that makes a serialized field non-editable in...
Read more >How to Serialize Enum to a String in C# - Code Maze
In this article, we will demonstrate different ways of how to serialize enum to string in C# with detailed explanation and examples.
Read more >FlagsAttribute Class (System)
A convenient way to test whether a flag is set in a numeric value is to perform a bitwise AND operation between the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yup, this works! Thanks for the quick fix.
Weird @jhranac it would seem there’s something wrong with the versioning here. The callstack points to a converter class that was in the first release but removed in the 2nd release yesterday.
I have created a new release with a passing test on an
uint
enumeration type. You can find that here: https://github.com/ExtendedXmlSerializer/home/pull/387#issuecomment-617018114Let’s see if that treats you better. ✌