Setting IgnoreSerializableAttribute Globally in Json.net

前端 未结 4 2065
萌比男神i
萌比男神i 2020-12-10 04:07

I\'m working on a ASP.NET WebApi (Release Candidate) project where I\'m consuming several DTOs that are marked with the [Serializable] attribute. These DTOs are outside of m

4条回答
  •  情深已故
    2020-12-10 04:25

    Since the library does not expose a static setter for the DefaultContractResolver, I suggest you create a static wrapper over JsonConvert and it's Serialize*/Deserialize* methods (at least the ones you use).

    In your static wrapper you can define a static contract resolver:

    private static readonly DefaultContractResolver Resolver = new DefaultContractResolver
    {
        IgnoreSerializableAttribute = true
    };
    

    This you can pass to each serialization method in the JsonSerializerSettings, inside your wrapper. Then you call your class throughout your project.

    The alternative would be to get the JSON.NET source code and adjust it yourself to use that attribute by default.

提交回复
热议问题