I use this trick in my Web API application. I work with Dto (Data Transfer Objects). A Dto is the JSON representation of my entity.
I don't know how to deal with enum. I like enum and in my database I save all the data in enum value. But when receiving a JSON object I like to receive it with string value.
[StringLength(2)]
public string DocumentType { get; set; }
[JsonIgnore]
public DocumentType DocumentTypeEnum
{
get
{
return (DocumentType)Enum.Parse(typeof(DocumentType), DocumentType);
}
set
{
DocumentType = value.ToString();
}
}
My enum is
public enum DocumentType
{
PI,
CI,
DN,
CN
};
Is there an attribute or a better way to force the system to deserialize the JSON string as enum value?