Make JSON.NET and Serializable attribute work together

前端 未结 3 822
刺人心
刺人心 2020-12-28 16:25

I\'m using JSON.NET and had some troubles in the past during WEBAPI objects deserialization. After doing some research I\'ve found that the class was marked with [Serializab

3条回答
  •  臣服心动
    2020-12-28 17:22

    An alternative to specifying JsonObject on each class is to tell web.api to ignore Serialize attributes globally. This can be done by resetting the DefaultContractResolver on the web api JsonFormatter:

    config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver();
    

    (using NewtonSoft.Json.Serialization where config is the System.Web.Http.HttpConfiguration)

    As of NewtonSoft v4.5 the IgnoreSerializableAttribute property on the DefaultContractResolver is set to true but the web api wrapper, around DefaultContractResolver, has this set to false by default.

提交回复
热议问题