0

Are Enums binary serialized as numbers or as text - I want to rename them.

Example:

[Flags]
public enum VerticalAlignment : byte
{
    None = 0,
    Centered = 1,
    Top = 2,
    Bottom = 4,
    Stretch = 8,
    All = Centered | Top | Bottom | Stretch
}
Marek
  • 2,419
  • 6
  • 34
  • 38
  • I believe that `byte` is used to serialize enum. – pwas Oct 21 '15 at 09:37
  • @PatrickHofman enum are whichever numeric type they are based on, defaulting to `int` if none is specified. That in the question is `byte`. – Jon Hanna Oct 21 '15 at 09:52

3 Answers3

3

It depends on the serializer you are using.

Here is code that uses the DataContractSerializer

var serializer = new DataContractSerializer(typeof (VerticalAlignment));

var alignment = VerticalAlignment.Centered;

using (var stream = File.Create("serialize.txt"))
{
    serializer.WriteObject(stream, alignment);
    stream.Close();
}

This will serialize the data as XML, which uses the enum's name and not the value. The contents of the "serialize.txt" file are as follows:

<Program.VerticalAlignment xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication2">Centered</Program.VerticalAlignment>

If you want to do binary serialization, you can use the BinaryFormatter class:

var serializer = new BinaryFormatter();

var alignment = VerticalAlignment.Centered;

using (var stream = File.Create("serialize.txt"))
{
    serializer.Serialize(stream, alignment);
    stream.Close();
}

This will write the following to our serialization file:

Binary serialization

To test if name or value is used, we can rename the field and see if what gets written to file is different. First, we rename the enum field:

[Flags]
public enum VerticalAlignment : byte
{
    None = 0,
    CenteredDummy = 1,
    Top = 2,
    Bottom = 4,
    Stretch = 8,
    All = CenteredDummy | Top | Bottom | Stretch
}

Now, if we examine the data that was written to file, we can see that the contents are the same:

Binary serialization

We thus now know for sure that the enum value is used and not the key.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
2

I believe that byte is used to serialize enum. A quick sample:

public static void Main()
{
    VerticalAlignment some  = VerticalAlignment.All;

    BinaryFormatter bf = new BinaryFormatter();
    using (var writer = File.Create("SomeText.bin"))
    {
        bf.Serialize(writer, some);
    }
}

A dump from file:

dump ........

I am sure that the 0f (the one before last pair) means the value of enum:

1 | 2 | 4 | 8 = 15 (dec) = f (hex)
pwas
  • 3,225
  • 18
  • 40
0

Borrowing nopeflows sample, we can confirm that it is in fact a serialization of just the bytes, not the names.

First serialize it to a string as demostrated by nopeflow's sample code. Next rename the "All" enum member "AllNew" and then deserialize it and you'll see that the value of the deserialized Enum is "AllNew", proof that the byte value is serialized.

VerticalAlignment some = VerticalAlignment.Bottom;

BinaryFormatter bf = new BinaryFormatter();
using (var reader = File.Open("SomeText.bin", FileMode.Open))
{
    some = (VerticalAlignment)bf.Deserialize(reader);
}
Don
  • 6,632
  • 3
  • 26
  • 34