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

前端 未结 2 1246
我在风中等你
我在风中等你 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:25

    Support for [EnumMember] was only added during this release so you'll need to upgrade to v5.1.1 pre-release NuGet packages on MyGet.

    0 讨论(0)
  • 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
    { 
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题