You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exception thrown unless ReferenceLoopHandling.Serialize is passed to force
Steps to reproduce
varcontainer=new ChildContainerClass(){Name="My",Number=1};varentry=new ChildEntryClass {Name="My",Number=1};
container.Entries.Add(entry);
Console.WriteLine("Instance Equals : "+ container.Equals(entry));// returns True, because they equal by implementation
Console.WriteLine("Reference Equals : "+ ReferenceEquals(container, entry));// returns False, because they are different refsvarstr= JsonConvert.SerializeObject(container);// will fail with reference loop exception
The text was updated successfully, but these errors were encountered:
This is not a bug. Using equality comparison for detecting circular references is intended default behavior. For example, see this comment from the author of the library: #401 (comment). (Mostly because this behavior has already been established over a span of numerious library versions across many years before people started asking for a change of this behavior. But at that point breaking compatibility by breaking established default behavior was deemed not acceptable anymore. Unfortunate, but understandable and reasonable...)
Fortunately, you can override this behavior by configuring Newtonsoft.Json's EqualityComparer setting to use object reference equality comparison only:
Source/destination types
Source/destination JSON
Not applicable
Expected behavior
Object serializes properly
Actual behavior
Exception thrown unless
ReferenceLoopHandling.Serialize
is passed to forceSteps to reproduce
The text was updated successfully, but these errors were encountered: