using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
public static void Main()
var list = new List<Test>
new Test { Name = "bytes", Obj = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f } },
new Test { Name = "string", Obj = "This is a test" },
new Test { Name = "reader", Obj = new SqlDataReader() },
new Test { Name = "decimal", Obj = 3.14159 },
new Test { Name = "null", Obj = null }
var settings = new JsonSerializerSettings
ContractResolver = new AutoIgnoreComplexTypesContractResolver(),
Formatting = Formatting.Indented
string json = JsonConvert.SerializeObject(list, settings);
public class AutoIgnoreComplexTypesContractResolver : DefaultContractResolver
private static readonly List<Type> IgnoredTypes = new List<Type>()
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
var property = base.CreateProperty(member, memberSerialization);
if (property.PropertyType == typeof(object))
property.ShouldSerialize = instance =>
PropertyInfo pi = member as PropertyInfo;
object propertyValue = (pi != null) ? pi.GetValue(instance) : null;
return propertyValue != null && !IgnoredTypes.Contains(propertyValue.GetType());
else if (IgnoredTypes.Contains(property.PropertyType))
property.ShouldSerialize = instance => false;
public string Name { get; set; }
public object Obj { get; set; }