using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace Question44515823
public class DisableReferencePreservationContractResolver : DefaultContractResolver
readonly HashSet<Type> types;
public DisableReferencePreservationContractResolver(IEnumerable<Type> types)
this.types = new HashSet<Type>(types);
bool ContainsType(Type type)
return types.Contains(type);
bool? GetIsReferenceOverride(Type type)
return ContainsType(type) ? false : (bool?)null;
protected override JsonContract CreateContract(Type objectType)
var contract = base.CreateContract(objectType);
contract.IsReference = contract.IsReference ?? GetIsReferenceOverride(objectType);
public int Value { get; set; }
public class Container<T>
this.MultipleItems = new List<T>();
this.DictionaryItems = new Dictionary<string, T>();
public T SingleItem { get; set; }
public List<T> MultipleItems { get; set; }
public Dictionary<string, T> DictionaryItems { get; set; }
public class MidLevel : Container<Leaf>
public class RootLevel : Container<MidLevel>
public static void Test()
var leaf1 = new Leaf { Value = 1 };
var leaf2 = new Leaf { Value = 2 };
var midClass1 = new MidLevel
MultipleItems = { leaf1, leaf2 },
DictionaryItems = { { "leaf1", leaf1 } },
var midClass2 = new MidLevel
MultipleItems = { leaf1, leaf2 },
DictionaryItems = { { "leaf2", leaf2 } },
var rootClass = new RootLevel
MultipleItems = { midClass2, },
DictionaryItems = { { "midClass2", midClass2 } },
Test(rootClass, typeof(MidLevel));
Test(rootClass, typeof(Leaf));
static void Test<T>(T root, Type excludeType)
var settings = new JsonSerializerSettings
TypeNameHandling = TypeNameHandling.Objects,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ContractResolver = new DisableReferencePreservationContractResolver(new[] { excludeType }),
var json = JsonConvert.SerializeObject(root, typeof(object), Formatting.Indented, settings);
Console.WriteLine("\nJSON excluding $ref and $id information for instances of {0}:", excludeType);
var rootToken = (JContainer)JToken.Parse(json);
var query = from obj in rootToken.DescendantsAndSelf().OfType<JObject>()
let typeString = (string)obj["$type"]
where typeString != null && typeString.StartsWith(excludeType.FullName)
where obj["$ref"] != null || obj["$id"] != null
Assert.IsTrue(!query.Any());
Console.WriteLine("$ref and $id information successfully excluded.");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
throw new AssertionFailedException("failed");
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);