Newtonsoft: Override deserialized default value

Viewed 195

I have a generated class as follows:

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>();
}

I am currently receiving JSON that contains values for Warnings, but upon deserializtion of the JSON into this class I get an empty List. I actually get this for all of the properties of type List. I have a hunch that it's due the fact that there is an explicit declaration of a new System.Collections.Generic.List on each of these properties with an empty List.

I also see that through the debugger, the new instantiation happens followed by the set called on the other non List properties to confirm my hunch.

Is there a way to stop this? I cannot edit the generated files since I am worried of re-generation later on.

I currently have this resolver in place for my JsonSerializerSettings.ContractResolver because the API response I get back does not actually contain the Required.Always properties..

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;
    }
}
0 Answers
Related