Skip to main content
added 44 characters in body
Source Link
Tim S.
  • 56.7k
  • 9
  • 100
  • 124

You don't need to write your own converter. Json.NET's StringEnumConverter will read the EnumMember attribute. If you change your enum to this, it will serialize from and to the values you want.

[JsonConverter(typeof(StringEnumConverter))] public enum Animals { [EnumMember(Value = "dog")] Dog, [EnumMember(Value = "cat")] Cat, [EnumMember(Value = "black_bear")] BlackBear } 

(As a minor note, since Animals isn't a flags enum, it should be singular: Animal. You should consider changing it to this.)

Json.NET's StringEnumConverter will read the EnumMember attribute. If you change your enum to this, it will serialize from and to the values you want.

[JsonConverter(typeof(StringEnumConverter))] public enum Animals { [EnumMember(Value = "dog")] Dog, [EnumMember(Value = "cat")] Cat, [EnumMember(Value = "black_bear")] BlackBear } 

(As a minor note, since Animals isn't a flags enum, it should be singular: Animal. You should consider changing it to this.)

You don't need to write your own converter. Json.NET's StringEnumConverter will read the EnumMember attribute. If you change your enum to this, it will serialize from and to the values you want.

[JsonConverter(typeof(StringEnumConverter))] public enum Animals { [EnumMember(Value = "dog")] Dog, [EnumMember(Value = "cat")] Cat, [EnumMember(Value = "black_bear")] BlackBear } 

(As a minor note, since Animals isn't a flags enum, it should be singular: Animal. You should consider changing it to this.)

Source Link
Tim S.
  • 56.7k
  • 9
  • 100
  • 124

Json.NET's StringEnumConverter will read the EnumMember attribute. If you change your enum to this, it will serialize from and to the values you want.

[JsonConverter(typeof(StringEnumConverter))] public enum Animals { [EnumMember(Value = "dog")] Dog, [EnumMember(Value = "cat")] Cat, [EnumMember(Value = "black_bear")] BlackBear } 

(As a minor note, since Animals isn't a flags enum, it should be singular: Animal. You should consider changing it to this.)