How to configure ServiceStack.Text to use EnumMember when deserializing?

前端 未结 2 1245
我在风中等你
我在风中等你 2021-01-25 06:17

I am using ServiceStack.Text to deserialize json received in rest api calls to objects C#. The model classes I use have defined the string representation using EnumMember attrib

2条回答
  •  隐瞒了意图╮
    2021-01-25 06:44

    I found out why my deserialization was not working. ServiceStack.Text was not interpreting the EnumMember attributes because enum declaration does not have DataContract attribute set. This is actually explained in the EnumMember documentation link I also linked in the question:

    One way to use enumeration types in the data contract model is to apply the DataContractAttribute attribute to the type. You must then apply the EnumMemberAttribute attribute to each member that must be included in the data contract.

    Expected results were produced by adding the missing attribute:

    [DataContract] // This was missing
    enum TestEnum
    { 
        // ...
    }
    

提交回复
热议问题