I have an enum which contains attributes on each value, for example:
public enum MyEnum
{
[MyEnumAttribute("val01")]
Value01,
[MyEnumAttribute("val02")]
Value02
}
This enum is used as a property in a class, for example:
public class MyDataObject
{
public string Name { get; set; }
public MyEnum EnumOption { get; set; }
}
How can I serialize this object to JSON (preferably using Newtonsoft.Json), using the value from MyEnumAttribute
instead of the enum value?
Edit:
Ideally, I'd also like to be able to deserialise the JSON into an instance of MyDataObject
, mapping the values val01
to MyEnum.Value01
, val02
to MyEnum.Value02
etc