1

I need to serialize an enum's values to a string other than the name.

I tried doing this (see code below) but the attribute [EnumMember(Value = "name")] didn't change anything. When I do .ToJson() I can see that the value is still "Monday" and not "MONDAY"

[DataContract]
public enum Day
{
    [EnumMember(Value = "MONDAY")]
    Monday,
    [EnumMember(Value = "TUESDAY")]
    Tuesday,
    [EnumMember(Value = "WEDNESDAY")]
    Wednesday,
    [EnumMember(Value = "THURSDAY")]
    Thursday,
    [EnumMember(Value = "FRIDAY")]
    Friday,
    [EnumMember(Value = "SATURDAY")]
    Saturday,
    [EnumMember(Value = "SUNDAY")]
    Sunday,
}
LorneCash
  • 1,446
  • 2
  • 16
  • 30
  • So you are trying JSON serialization? Please check out this [so post](https://stackoverflow.com/questions/2441290/json-serialization-of-enum-as-string) which does seem to match your problem – dsdel Apr 08 '18 at 07:07
  • 1
    I saw that post but that doesn't apply to me because ServiceStack is doing the serialization. It's not a question of how to serialize it but rather how to get ServiceStack to do it the way I want. – LorneCash Apr 08 '18 at 07:21

1 Answers1

3

I've just added support for [EnumMember] customization's in this commit where your example will work as expected.

This change is available from v5.0.3 that's now available on MyGet.

mythz
  • 141,670
  • 29
  • 246
  • 390