title: Ignore deserialize when no such property exist
body: <p>I'm consuming a private web service , which is not in my control.</p>
  
  <p>i'm consuming a login service, This is class about to deseralized the result</p>
  
  <pre><code>[DataContract]
  public class BaseDTO
  {
      [DataMember]
      public string status { get; set; }
  
      [DataMember]
      public string msg { get; set; }
  
      [DataMember]
      public string code { get; set; }
  
      [DataMember(Name = "data", IsRequired = false)]
      public LoginDTO LoginInfo{ get; set; }
  }
  
  [DataContract]
  public class LoginDTO 
  {
      [DataMember]
      public string FirstName{ get; set; }
  
      [DataMember]
      public string LastName{ get; set; }
  
  }
  </code></pre>
  
  <p>Here what i tried</p>
  
  <pre><code>public async Task&lt;BaseDTO&gt; InsertString(MyObject insertParameters)
      {
          try
          {
              using (var _client = new HttpClient())
              {
                  _client.BaseAddress = new Uri(baseServiceUri);
                  _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                  HttpContent insertHttpcontent = new StringContent(JsonConvert.SerializeObject(insertObject));
  
                  var response = await _client.PostAsync(new Uri(baseAccessPoint, UriKind.Relative), insertHttpcontent);
  
                  //Throws exception when its invalid
                  //response.EnsureSuccessStatusCode();
  
                  var responseResult = await response.Content.ReadAsStringAsync();
  
  
                  return JsonConvert.DeserializeObject&lt;BaseDTO&gt;(responseResult, new JsonSerializerSettings()
                  {
                      NullValueHandling = NullValueHandling.Ignore,
                      Error = JsonDeserializeErrorHandler,
                  });
              }
  
  
          }
          catch (Exception ex)
          {
  
              throw;
          }
      }
  </code></pre>
  
  <p><strong>If the login credentials are true service is returning the LoginInfo details ,else it will not provide any information just the status,msg and code.</strong></p>
  
  <p>The problem is the Json Deseralizer always try to Deseralize everything even if tried to set ignore null value , but doesn't seems working. As my service is not returning null, It will not provide the object itself. Currently i handle using JsonDeserializeErrorHandler setting errorhandled property to true. 
  I have referenced <a href="https://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-it-is-null-using-json-net">this also</a></p>
  
  <p>JsonResult When success</p>
  
  <pre><code>{"status":"success","msg":"Welcome!!","code":"","data":{"session_token":"bdadee2cb7597fbb5ceed550ca389440","firsname":"test1","lastname":"test2"}}}}
  </code></pre>
  
  <p>JsonResult When Error </p>
  
  <pre><code>{"status":"warning","msg":"Incorrect Username or MPIN","code":"","data":[]}
  </code></pre>
  
  <p><strong>How do i overcome this scenario more gracefully</strong></p>
  
  <pre><code>    #region DeserializeErrorHandling
      private void JsonDeserializeErrorHandler(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs e)
      {
          //var currentError = e.ErrorContext.Error.Message;
          e.ErrorContext.Handled = true;
      }
      #endregion
  </code></pre>