-Reduced allocations of JToken.Path++

This commit is contained in:
James Newton-King
2016-03-28 19:44:05 +13:00
parent 795ef28e0a
commit 5aa75ae595
+18 -31
View File
@@ -190,45 +190,32 @@ namespace Newtonsoft.Json.Linq
return string.Empty; return string.Empty;
} }
List<JToken> ancestors = new List<JToken>(); List<JsonPosition> positions = new List<JsonPosition>();
JToken previous = null;
for (JToken current = this; current != null; current = current.Parent) for (JToken current = this; current != null; current = current.Parent)
{ {
ancestors.Add(current); switch (current.Type)
}
ancestors.Reverse();
List<JsonPosition> positions = new List<JsonPosition>(ancestors.Count);
for (int i = 0; i < ancestors.Count; i++)
{
JToken current = ancestors[i];
JToken next = null;
if (i + 1 < ancestors.Count)
{ {
next = ancestors[i + 1]; case JTokenType.Property:
} JProperty property = (JProperty)current;
else if (ancestors[i].Type == JTokenType.Property) positions.Add(new JsonPosition(JsonContainerType.Object) { PropertyName = property.Name });
{ break;
next = ancestors[i]; case JTokenType.Array:
} case JTokenType.Constructor:
if (previous != null)
if (next != null) {
{ int index = ((IList<JToken>)current).IndexOf(previous);
switch (current.Type)
{
case JTokenType.Property:
JProperty property = (JProperty)current;
positions.Add(new JsonPosition(JsonContainerType.Object) { PropertyName = property.Name });
break;
case JTokenType.Array:
case JTokenType.Constructor:
int index = ((IList<JToken>)current).IndexOf(next);
positions.Add(new JsonPosition(JsonContainerType.Array) { Position = index }); positions.Add(new JsonPosition(JsonContainerType.Array) { Position = index });
break; }
} break;
} }
previous = current;
} }
positions.Reverse();
return JsonPosition.BuildPath(positions, null); return JsonPosition.BuildPath(positions, null);
} }
} }