title: Ignoring class members that throw exceptions when serializing to JSON
body: <p>I'm using the Newtonsoft JSON serializer and it works for most objects. </p>
  
  <p>Unfortunately I get a <code>JsonSerializationException</code> when I try to serialize a large object, one of whose members throws a <code>NullReferenceException</code>.</p>
  
  <p>Is there anyway to ignore the offending member and serialize the rest of the object?</p>
  
  <p>I'm thinking perhaps in the <code>JsonSerializerSettings</code>? </p>
  
  <p>Here's a simplified version of what I want to do:</p>
  
  <pre><code>private class TestExceptionThrowingClass
  {
      public string Name { get { return "The Name"; } }
      public string Address { get { throw new NullReferenceException(); } }
      public int Age { get { return 30; } }
  }
  
  [Test]
  public void CanSerializeAClassWithAnExceptionThrowingMember()
  { 
      // Arrange
      var toSerialize = new TestExceptionThrowingClass();
  
      // Act
  
      var serializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
      serializerSettings.MaxDepth = 5;
      serializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
      serializerSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
      serializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
      serializerSettings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Reuse;
      serializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
  
      var result = Newtonsoft.Json.JsonConvert.SerializeObject(toSerialize);
  
      // Assert
      Assert.That(result, Is.EqualTo(@"{""Name"":""The Name"",""Age"":30}"));
  }
  </code></pre>
  
  <p>And here's the stack trace:</p>
  
  <pre><code>at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target) 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract&amp; memberContract, Object&amp; memberValue) 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) 
  at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) 
  at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings) 
  at Newtonsoft.Json.JsonConvert.SerializeObject(Object value) 
  at AspectsProject.Aspects.CachingPolicy.CachingPolicyCacheKeyCreatorTests.CanSerializeAClassWithAnExceptionThrowingMember() in D:\Dev\test.cs:line 169
      --NullReferenceException 
  at AspectsProject.Aspects.CachingPolicy.CachingPolicyCacheKeyCreatorTests.TestExceptionThrowingClass.get_Address() in D:\Dev\test.cs:line 149 
  at GetAddress(Object ) 
  at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
  </code></pre>
  
  <p>I'm happy to use a different JSON serializer if someone knows one that will do this.</p>