title: JSONIgnore data annotations on class
body: <p>I have a public property which is an object that contains numerous properties itself.  Using ASP.net MVC, when I serialize the JSON data I simply add the <code>[JsonIgnore]</code> attribute wherever I use the object so it doesn't display the contents.</p>
  
  <p>Is there a way to add the <code>[JsonIgnore]</code> attribute to the class so it never is serialized?  </p>
  
  <pre><code>//[JsonIgnore]  ??
  public class DataObj
  {
      public string ConnectionName { get; set; }
      public string Query { get; set; }
      ...
  }
  
  public class Customer
  {
      public string First { get; set; }
      public string Last { get; set; }
      [JsonIgnore]
      public DataObj Foo { get; set; }
  }
  
  public class ShipAddress
  {
      public string Street { get; set; }
      public string City { get; set; }
      public string Country { get; set; }
      [JsonIgnore]
      public DataObj Foo { get; set; }
  }
  </code></pre>
  
  <p><strong>My solution</strong> after receiving the code provided by <a href="https://stackoverflow.com/users/205988/jvanrhyn">jvanrhyn</a>.<br>
  Also, here is a <a href="http://www.newtonsoft.com/json/help/html/ContractResolver.htm" rel="nofollow noreferrer">link</a> that explains more.</p>
  
  <pre><code>public class DataObjFilterContractResolver : DefaultContractResolver
  {
      public static readonly DataObjFilterContractResolver Instance = new DataObjFilterContractResolver();
  
      protected override JsonProperty CreateProperty(MemberInfo member,MemberSerialization memberSerialization)
      {
          var property = base.CreateProperty(member, memberSerialization);
          if (property.DeclaringType.Name.StartsWith("DataObj") || property.PropertyName == "DataObj")
          {
              property.ShouldSerialize = instance =&gt; false;
          }
          return property;
      }
  }
  
  
  public class UtcJsonResult : JsonResult
  {
      public UtcJsonResult(object data)
      {
          Data = data;
          JsonRequestBehavior = JsonRequestBehavior.AllowGet;
      }
  
      private const string DateFormat = @"yyyy-MM-dd HH:mm:ssZ";
  
      public override void ExecuteResult(ControllerContext context)
      {
          if (context == null) throw new ArgumentNullException("context");
          if (Data == null) return;
  
          var response = context.HttpContext.Response;
          response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
          if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;
  
          var isoConvert = new IsoDateTimeConverter {DateTimeFormat = DateFormat};
          JsonConvert.DefaultSettings = 
              () =&gt; new JsonSerializerSettings 
                  { ContractResolver = new DataObjFilterContractResolver()};  //&lt;--- Used here
          var json = JsonConvert.SerializeObject(Data, isoConvert);
          response.Write(json);
      }
  }
  </code></pre>