When I serialize a enum value using DataContractJsonSerializer, it serializes the numerical value of the enum, not the string name.
IE:
enum foo
{
bar,
baz
}
Serializing a value of foo.bar returns “0”, not “bar”.
I’d prefer it the other way around, is there a way to override this?
Edit:
Because I didn’t want to change the serializer, I used a simple workaround hack.
I exposed a property in the class to serialize that calls ToString on the value, ie:
// Old
[DataMember]
public EnumType Foo
{
get { return _foo; }
set { _foo = value; }
}
// New, I still kept the EnumType but I only serialize the string version
public EnumType Foo
{
get { return _foo; }
set { _foo = value; }
}
[DataMember]
public string FooType
{
get { return _foo.ToString(); }
private set {}
}
It looks like this is by design and this behavior cannot be changed:
Here’s an example using an alternative (and IMO better and more extensible) serializer which achieves what you are looking for: