question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flags enums are not serialized when multiple flags are set

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jhranaccommented, Apr 21, 2020

Yup, this works! Thanks for the quick fix.

1reaction
Mike-E-angelocommented, Apr 21, 2020

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-617018114

Let’s see if that treats you better. ✌

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found