This project has moved and is read-only. For the latest updates, please go here.


Make reference loop checking extensible


Use case/problem - You have a class that looks like this:
public class Foo
public int Id { get; set; }
public Foo OtherFoo
  //Fetch an instance of Foo from a database, XML serialization or whatever
Now, as Foo doesn't override Equals or implements IEquatable<T> two instances of Foo that represent the same "entity" would not be considered equal when using List<T>.IndexOf. In this case, if we have an instance of Foo that returns the same "entity" created from the database, we'll not get a serialization exception but instead a stack overflow exception.
The root cause is of course not in JSON.NET but rather in the design of Foo, but in some cases we may not be able to modify Foo. It would therefor be useful to be able to modify how reference loop checks are made. One very simple but crude solution would be to let the JsonSerializer class have a property of type Func<IList<Object>> and then in the ctor for JsonSerializerInternalReader check if this property has a value. If if does, invoke the delegate and use that as the seralization stack. I'm guessing there are many more elegant solutions though :)
Closed Jul 25, 2012 at 3:08 AM by JamesNK
This is very edge case and I don't want to add it.