-Performance

This commit is contained in:
James Newton-King
2012-11-01 20:14:08 +13:00
parent a64334b7e2
commit fac8514ff6
3 changed files with 55 additions and 84 deletions
+18 -24
View File
@@ -39,45 +39,39 @@ namespace Newtonsoft.Json
internal struct JsonPosition internal struct JsonPosition
{ {
internal JsonContainerType Type; internal JsonContainerType Type;
internal int? Position; internal int Position;
internal string PropertyName; internal string PropertyName;
internal bool HasIndex;
public JsonPosition(JsonContainerType type)
{
Type = type;
HasIndex = TypeHasIndex(type);
Position = -1;
PropertyName = null;
}
internal void WriteTo(StringBuilder sb) internal void WriteTo(StringBuilder sb)
{ {
switch (Type) switch (Type)
{ {
case JsonContainerType.Object: case JsonContainerType.Object:
if (PropertyName != null) if (sb.Length > 0)
{ sb.Append(".");
if (sb.Length > 0) sb.Append(PropertyName);
sb.Append(".");
sb.Append(PropertyName);
}
break; break;
case JsonContainerType.Array: case JsonContainerType.Array:
case JsonContainerType.Constructor: case JsonContainerType.Constructor:
if (Position != null) sb.Append("[");
{ sb.Append(Position);
sb.Append("["); sb.Append("]");
sb.Append(Position);
sb.Append("]");
}
break; break;
} }
} }
internal bool InsideContainer() internal static bool TypeHasIndex(JsonContainerType type)
{ {
switch (Type) return (type == JsonContainerType.Array || type == JsonContainerType.Constructor);
{
case JsonContainerType.Object:
return (PropertyName != null);
case JsonContainerType.Array:
case JsonContainerType.Constructor:
return (Position != null);
}
return false;
} }
internal static string BuildPath(IEnumerable<JsonPosition> positions) internal static string BuildPath(IEnumerable<JsonPosition> positions)
+11 -7
View File
@@ -225,7 +225,15 @@ namespace Newtonsoft.Json
if (_currentPosition.Type == JsonContainerType.None) if (_currentPosition.Type == JsonContainerType.None)
return string.Empty; return string.Empty;
return JsonPosition.BuildPath(_stack.Concat(new[] { _currentPosition })); bool insideContainer = (_currentState != State.ArrayStart
&& _currentState != State.ConstructorStart
&& _currentState != State.ObjectStart);
IEnumerable<JsonPosition> positions = (!insideContainer)
? _stack
: _stack.Concat(new[] {_currentPosition});
return JsonPosition.BuildPath(positions);
} }
} }
@@ -265,16 +273,12 @@ namespace Newtonsoft.Json
if (_currentPosition.Type == JsonContainerType.None) if (_currentPosition.Type == JsonContainerType.None)
{ {
_currentPosition.Type = value; _currentPosition = new JsonPosition(value);
} }
else else
{ {
_stack.Add(_currentPosition); _stack.Add(_currentPosition);
JsonPosition state = new JsonPosition _currentPosition = new JsonPosition(value);
{
Type = value
};
_currentPosition = state;
// this is a little hacky because Depth increases when first property/value is written but only testing here is faster/simpler // this is a little hacky because Depth increases when first property/value is written but only testing here is faster/simpler
if (_maxDepth != null && Depth + 1 > _maxDepth && !_hasExceededMaxDepth) if (_maxDepth != null && Depth + 1 > _maxDepth && !_hasExceededMaxDepth)
+26 -53
View File
@@ -143,21 +143,6 @@ namespace Newtonsoft.Json
} }
} }
internal string ContainerPath
{
get
{
if (_currentPosition.Type == JsonContainerType.None)
return string.Empty;
IEnumerable<JsonPosition> positions = (_currentPosition.InsideContainer())
? _stack
: _stack.Concat(new[] { _currentPosition });
return JsonPosition.BuildPath(positions);
}
}
/// <summary> /// <summary>
/// Gets the state of the writer. /// Gets the state of the writer.
/// </summary> /// </summary>
@@ -190,6 +175,17 @@ namespace Newtonsoft.Json
} }
} }
internal string ContainerPath
{
get
{
if (_currentPosition.Type == JsonContainerType.None)
return string.Empty;
return JsonPosition.BuildPath(_stack);
}
}
/// <summary> /// <summary>
/// Gets the path of the writer. /// Gets the path of the writer.
/// </summary> /// </summary>
@@ -200,7 +196,15 @@ namespace Newtonsoft.Json
if (_currentPosition.Type == JsonContainerType.None) if (_currentPosition.Type == JsonContainerType.None)
return string.Empty; return string.Empty;
return JsonPosition.BuildPath(_stack.Concat(new[] { _currentPosition })); bool insideContainer = (_currentState != State.ArrayStart
&& _currentState != State.ConstructorStart
&& _currentState != State.ObjectStart);
IEnumerable<JsonPosition> positions = (!insideContainer)
? _stack
: _stack.Concat(new[] { _currentPosition });
return JsonPosition.BuildPath(positions);
} }
} }
@@ -249,45 +253,29 @@ namespace Newtonsoft.Json
internal void UpdateScopeWithFinishedValue() internal void UpdateScopeWithFinishedValue()
{ {
if (_currentPosition.Type == JsonContainerType.Array if (_currentPosition.HasIndex)
|| _currentPosition.Type == JsonContainerType.Constructor)
{
if (_currentPosition.Position == null)
_currentPosition.Position = 0;
else
_currentPosition.Position++; _currentPosition.Position++;
}
} }
private void Push(JsonContainerType value) private void Push(JsonContainerType value)
{ {
if (_currentPosition.Type == JsonContainerType.None) if (_currentPosition.Type != JsonContainerType.None)
{
_currentPosition.Type = value;
}
else
{
_stack.Add(_currentPosition); _stack.Add(_currentPosition);
var state = new JsonPosition
{ _currentPosition = new JsonPosition(value);
Type = value
};
_currentPosition = state;
}
} }
private JsonContainerType Pop() private JsonContainerType Pop()
{ {
JsonPosition oldPosition; JsonPosition oldPosition = _currentPosition;
if (_stack.Count > 0) if (_stack.Count > 0)
{ {
oldPosition = _currentPosition;
_currentPosition = _stack[_stack.Count - 1]; _currentPosition = _stack[_stack.Count - 1];
_stack.RemoveAt(_stack.Count - 1); _stack.RemoveAt(_stack.Count - 1);
} }
else else
{ {
oldPosition = _currentPosition;
_currentPosition = new JsonPosition(); _currentPosition = new JsonPosition();
} }
@@ -564,21 +552,6 @@ namespace Newtonsoft.Json
} }
} }
private JsonContainerType GetTypeForCloseToken(JsonToken token)
{
switch (token)
{
case JsonToken.EndObject:
return JsonContainerType.Object;
case JsonToken.EndArray:
return JsonContainerType.Array;
case JsonToken.EndConstructor:
return JsonContainerType.Constructor;
default:
throw JsonWriterException.Create(this, "No type for token: " + token, null);
}
}
private JsonToken GetCloseTokenForType(JsonContainerType type) private JsonToken GetCloseTokenForType(JsonContainerType type)
{ {
switch (type) switch (type)