private void UpdateContext()
{
if (!currentToken.HasValue) return;
if (currentToken.Value == JsonToken.ObjectStart || currentToken.Value == JsonToken.ArrayStart)
{
// Push '/' for object start and array start.
stack.Push(DELIMITER);
}
else if (currentToken.Value == JsonToken.ObjectEnd || currentToken.Value == JsonToken.ArrayEnd)
{
if (object.ReferenceEquals(stack.Peek(),DELIMITER))
{
// Pop '/' associated with corresponding object start and array start.
stack.Pop();
if (stack.Count > 0 && ! object.ReferenceEquals(stack.Peek(),DELIMITER))
{
// Pop the property name associated with the
// object or array if present.
// e.g. {"a":["1","2","3"]}
stack.Pop();
}
}
currentField = null;
}
else if (currentToken.Value == JsonToken.PropertyName)
{
string t = ReadText();
currentField = t;
// Push property name, it's appended to the stack's CurrentPath,
// it this does not affect the depth.
stack.Push(currentField);
}
else if (currentToken.Value != JsonToken.None && !stack.CurrentPath.EndsWith(DELIMITER, StringComparison.OrdinalIgnoreCase))
{
// Pop if you encounter a simple data type or null
// This will pop the property name associated with it in cases like {"a":"b"}.
// Exclude the case where it's a value in an array so we dont end poping the start of array and
// property name e.g. {"a":["1","2","3"]}
stack.Pop();
}
}