How to serialize enums to different property name using json.net

前端 未结 1 1636
一向
一向 2020-12-15 18:06

I have the following enum

public enum PermissionType
{
  [JsonProperty(PropertyName = \"can_fly\")]
  PermissionToFly,
  [JsonProperty(PropertyName = \"can_s         


        
相关标签:
1条回答
  • 2020-12-15 18:21

    Looks like you can make this work using the EnumMember attribute (found in System.Runtime.Serialization).

    public enum PermissionType
    {
        [EnumMember(Value = "can_fly")]
        PermissionToFly,
    
        [EnumMember(Value = "can_swim")]
        PermissionToSwim
    }
    

    If you use those attributes you should also not need to set the ItemConverterType in the JsonProperty attribute on the list.

    0 讨论(0)
提交回复
热议问题