C# Newtonsoft:重写反序列化的默认值
我有一个生成的类,如下所示:C# Newtonsoft:重写反序列化的默认值,c#,serialization,json.net,C#,Serialization,Json.net,我有一个生成的类,如下所示: public partial class ResponseOfImportLeadResponse { /// <summary>Array of LeadsErrors that occurred if the request was unsuccessful</summary> [Newtonsoft.Json.JsonProperty("LeadsErrors", Required = Newtons
public partial class ResponseOfImportLeadResponse
{
/// <summary>Array of LeadsErrors that occurred if the request was unsuccessful</summary>
[Newtonsoft.Json.JsonProperty("LeadsErrors", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public System.Collections.Generic.List<LeadsError> LeadsErrors { get; set; } = new System.Collections.Generic.List<LeadsError>();
/// <summary>Boolean indicating if there are more results in subsequent pages</summary>
[Newtonsoft.Json.JsonProperty("moreResult", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public bool? MoreResult { get; set; }
/// <summary>Paging token given if the result set exceeded the allowed batch size</summary>
[Newtonsoft.Json.JsonProperty("nextPageToken", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string NextPageToken { get; set; }
/// <summary>Id of the request made</summary>
[Newtonsoft.Json.JsonProperty("requestId", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string RequestId { get; set; }
/// <summary>Array of results for individual records in the operation, may be empty</summary>
[Newtonsoft.Json.JsonProperty("result", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public System.Collections.Generic.List<ImportLeadResponse> Result { get; set; } = new System.Collections.Generic.List<ImportLeadResponse>();
/// <summary>Whether the request succeeded</summary>
[Newtonsoft.Json.JsonProperty("success", Required = Newtonsoft.Json.Required.Always)]
public bool Success { get; set; }
/// <summary>Array of warnings given for the operation</summary>
[Newtonsoft.Json.JsonProperty("warnings", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public System.Collections.Generic.List<Warning> Warnings { get; set; } = new System.Collections.Generic.List<Warning>();
}
public class OverrideRequiredResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty jsonProperty = base.CreateProperty(member, memberSerialization);
if (jsonProperty.Required == Required.Always)
{
jsonProperty.Required = Required.Default;
}
return jsonProperty;
}
}