Skip to content

Commit

Permalink
[FIXED]: Issue JamesNK#401 - Use reference equality to compare object…
Browse files Browse the repository at this point in the history
… instances when checking for circular references in a object graph.
  • Loading branch information
luca-piombino-deltatre committed Oct 23, 2014
1 parent 70d3e93 commit 2875435
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,17 @@ public void DuplicateId()
MetadataPropertyHandling = MetadataPropertyHandling.Default
}), "Error reading object reference '1'. Path 'Data.Prop2.MyProperty', line 9, position 20.");
}

[Test]
public void ShouldCheckForCircularReferencesUsingReferenceEquality()
{
var source = new ValueContainer
{
Value = new object()
};

string json = JsonConvert.SerializeObject(source, Formatting.Indented);
}
}

public class PropertyItemIsReferenceBody
Expand Down Expand Up @@ -1108,4 +1119,27 @@ public class ReferenceObject
public string String { get; set; }
public int Integer { get; set; }
}

public class ValueContainer
{
public object Value;

public override bool Equals(object obj)
{
if (obj == null)
return false;

var otherContainer = obj as ValueContainer;

if (otherContainer != null)
return EqualityComparer<object>.Default.Equals(Value, otherContainer.Value);

return EqualityComparer<object>.Default.Equals(Value, obj);
}

public override int GetHashCode()
{
return EqualityComparer<object>.Default.GetHashCode(Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private bool CheckForCircularReference(JsonWriter writer, object value, JsonProp
if (referenceLoopHandling == null && containerContract != null)
referenceLoopHandling = containerContract.ItemReferenceLoopHandling;

if (_serializeStack.IndexOf(value) != -1)
if (_serializeStack.IndexOf(itemOnStack => object.ReferenceEquals(itemOnStack,value)) != -1)
{
string message = "Self referencing loop detected";
if (property != null)
Expand Down

0 comments on commit 2875435

Please sign in to comment.