-Improved performance issue fix for deeply nested LINQ to JSON objects

This commit is contained in:
James Newton-King
2012-03-07 22:59:10 +13:00
parent c7f5208769
commit d32061cd16
3 changed files with 34 additions and 21 deletions
@@ -20,6 +20,15 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(0, a.Count);
}
[Test]
public void AddToSelf()
{
JArray a = new JArray();
a.Add(a);
Assert.IsFalse(ReferenceEquals(a[0], a));
}
[Test]
public void Contains()
{
+16 -1
View File
@@ -703,6 +703,21 @@ namespace Newtonsoft.Json.Tests
}, "JObject.ToString");
}
[Test]
public void RecursiveLoop()
{
JArray a1 = new JArray();
JArray a2 = new JArray();
JArray a3 = new JArray();
JArray a4 = new JArray();
a1.Add(a2);
a2.Add(a3);
a3.Add(a4);
}
[Test]
public void NestedJToken()
{
@@ -716,7 +731,7 @@ namespace Newtonsoft.Json.Tests
for (int j = 0; j < i; j++)
{
JArray temp = new JArray();
ija.AddAndSkipParentCheck(temp);
ija.Add(temp);
ija = temp;
}
ija.Add(1);
+9 -20
View File
@@ -266,27 +266,16 @@ namespace Newtonsoft.Json.Linq
if (item == null)
return new JValue((object) null);
if (item.Parent != null)
{
if (skipParentCheck)
return item;
// to avoid a token having multiple parents or creating a recursive loop, create a copy if...
// the item already has a parent
// the item is being added to itself
// the item is being added to the root parent of itself
if (item.Parent != null || item == this || (item.HasValues && Root == item))
item = item.CloneToken();
}
else
{
// check whether attempting to add a token to itself
// not possible with a value, only a container
if (item is JContainer && !skipParentCheck)
{
JContainer parent = this;
while (parent.Parent != null)
{
parent = parent.Parent;
}
if (item == parent)
{
item = item.CloneToken();
}
}
}
return item;
}