Add a custom attribute to json.net

后端 未结 3 1842
温柔的废话
温柔的废话 2021-01-02 10:38

JSON.NET comes with property attributes like [JsonIgnore] and [JsonProperty].

I want to create some custom ones that get run when the seria

3条回答
  •  执笔经年
    2021-01-02 11:11

    Not sure if this is new, but I would suggest using the method GetSerializableMembers which handles the member infos directly. This way, one can avoid having to deal with the JsonProperty.

    public class MyJsonContractResolver : DefaultContractResolver
    {
      protected override List GetSerializableMembers(Type objectType)
      {
        return base.GetSerializableMembers(objectType)
          .Where(mi => mi.GetCustomAttribute() != null)
          .ToList();
      }
    }
    

提交回复
热议问题