public override bool TryEvaluate(DynamicResources resources, IValue root,
                                         JsonLocationNode lastNode,
                                         IValue current,
                                         ProcessingFlags options,
                                         out IValue result)
        {
            JsonLocationNode?ancestor = lastNode;
            int index = 0;

            while (ancestor != null && index < _ancestorDepth)
            {
                ancestor = ancestor.Parent;
                ++index;
            }

            if (ancestor != null)
            {
                JsonLocation path = new JsonLocation(ancestor);
                IValue       value;
                if (TryGetValue(root, path, out value))
                {
                    return(this.TryEvaluateTail(resources, root, path.Last, value, options, out result));
                }
                else
                {
                    result = JsonConstants.Null;
                    return(true);
                }
            }
            else
            {
                result = JsonConstants.Null;
                return(true);
            }
        }
        public override void Select(DynamicResources resources,
                                    IValue root,
                                    JsonLocationNode lastNode,
                                    IValue current,
                                    INodeReceiver receiver,
                                    ProcessingFlags options,
                                    int depth)
        {
            JsonLocationNode?ancestor = lastNode;
            int index = 0;

            while (ancestor != null && index < _ancestorDepth)
            {
                ancestor = ancestor.Parent;
                ++index;
            }

            if (ancestor != null)
            {
                JsonLocation path = new JsonLocation(ancestor);
                IValue       value;
                if (TryGetValue(root, path, out value))
                {
                    this.TailSelect(resources, root, path.Last, value, receiver, options, depth);
                }
            }
        }
        /// <summary>
        /// Compares this instance with a specified <see cref="JsonLocationNode"/> object and indicates
        /// whether this instance precedes, follows, or appears in the same
        /// position in the sort order as the specified <see cref="JsonLocationNode"/>.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(JsonLocationNode other)
        {
            if (other == null)
            {
                return(1);
            }
            int diff = 0;

            if (ComponentKind != other.ComponentKind)
            {
                diff = ComponentKind - other.ComponentKind;
            }
            else
            {
                switch (ComponentKind)
                {
                case JsonLocationNodeKind.Root:
                    diff = string.Compare(_name, other._name);
                    break;

                case JsonLocationNodeKind.Index:
                    diff = _index - other._index;
                    break;

                case JsonLocationNodeKind.Name:
                    diff = string.Compare(_name, other._name);
                    break;
                }
            }
            return(diff);
        }
 public override void Select(DynamicResources resources,
                             IValue root,
                             JsonLocationNode lastNode,
                             IValue current,
                             INodeReceiver receiver,
                             ProcessingFlags options,
                             int depth)
 {
     if (current.ValueKind == JsonValueKind.Array)
     {
         Int32 index = 0;
         foreach (var item in current.EnumerateArray())
         {
             this.TailSelect(resources, root,
                             PathGenerator.Generate(lastNode, index, options),
                             item, receiver, options, depth);
             ++index;
         }
     }
     else if (current.ValueKind == JsonValueKind.Object)
     {
         foreach (var prop in current.EnumerateObject())
         {
             this.TailSelect(resources, root,
                             PathGenerator.Generate(lastNode, prop.Name, options),
                             prop.Value, receiver, options, depth);
         }
     }
 }
 public abstract void Select(DynamicResources resources,
                             IValue root,
                             JsonLocationNode lastNode,
                             IValue current,
                             INodeReceiver receiver,
                             ProcessingFlags options,
                             int depth);
 public override void Select(DynamicResources resources,
                             IValue root,
                             JsonLocationNode lastNode,
                             IValue current,
                             INodeReceiver receiver,
                             ProcessingFlags options,
                             int depth)
 {
     if (current.ValueKind == JsonValueKind.Array)
     {
         if (_index >= 0 && _index < current.GetArrayLength())
         {
             this.TailSelect(resources, root,
                             PathGenerator.Generate(lastNode, _index, options),
                             current[_index], receiver, options, depth);
         }
         else
         {
             Int32 index = current.GetArrayLength() + _index;
             if (index >= 0 && index < current.GetArrayLength())
             {
                 this.TailSelect(resources, root,
                                 PathGenerator.Generate(lastNode, _index, options),
                                 current[index], receiver, options, depth);
             }
         }
     }
 }
 public override bool TryEvaluate(DynamicResources resources, IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value)
 {
     return(this.TryEvaluateTail(resources, root, lastNode, current, options, out value));
 }
 public override void Select(DynamicResources resources,
                             IValue root,
                             JsonLocationNode lastNode,
                             IValue current,
                             INodeReceiver receiver,
                             ProcessingFlags options,
                             int depth)
 {
     this.TailSelect(resources, root, lastNode, current, receiver, options, depth);
 }
        /// <summary>
        /// Constructs a path node from a parent and an index
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="index">The index.</param>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="parent"/> is <see langword="null"/>.
        /// </exception>

        public JsonLocationNode(JsonLocationNode parent, Int32 index)
        {
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            Parent        = parent;
            ComponentKind = JsonLocationNodeKind.Index;
            _name         = "";
            _index        = index;
        }
        public override void Select(DynamicResources resources,
                                    IValue root,
                                    JsonLocationNode lastNode,
                                    IValue current,
                                    INodeReceiver receiver,
                                    ProcessingFlags options,
                                    int depth)
        {
            if (current.ValueKind == JsonValueKind.Array)
            {
                Int32 start = _slice.GetStart(current.GetArrayLength());
                Int32 end   = _slice.GetStop(current.GetArrayLength());
                Int32 step  = _slice.Step;

                if (step > 0)
                {
                    if (start < 0)
                    {
                        start = 0;
                    }
                    if (end > current.GetArrayLength())
                    {
                        end = current.GetArrayLength();
                    }
                    for (Int32 i = start; i < end; i += step)
                    {
                        this.TailSelect(resources, root,
                                        PathGenerator.Generate(lastNode, i, options),
                                        current[i], receiver, options, depth);
                    }
                }
                else if (step < 0)
                {
                    if (start >= current.GetArrayLength())
                    {
                        start = current.GetArrayLength() - 1;
                    }
                    if (end < -1)
                    {
                        end = -1;
                    }
                    for (Int32 i = start; i > end; i += step)
                    {
                        if (i < current.GetArrayLength())
                        {
                            this.TailSelect(resources, root,
                                            PathGenerator.Generate(lastNode, i, options),
                                            current[i], receiver, options, depth);
                        }
                    }
                }
            }
        }
 static internal JsonLocationNode Generate(JsonLocationNode lastNode,
                                           Int32 index,
                                           ProcessingFlags options)
 {
     if ((options & ProcessingFlags.Path) != 0)
     {
         return(new JsonLocationNode(lastNode, index));
     }
     else
     {
         return(lastNode);
     }
 }
 static internal JsonLocationNode Generate(JsonLocationNode lastNode,
                                           string identifier,
                                           ProcessingFlags options)
 {
     if ((options & ProcessingFlags.Path) != 0)
     {
         return(new JsonLocationNode(lastNode, identifier));
     }
     else
     {
         return(lastNode);
     }
 }
        /// <summary>
        /// Constructs a path node from a parent and name
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="name">The name.</param>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="parent"/> is <see langword="null"/>.
        ///   <paramref name="name"/> is <see langword="null"/>.
        /// </exception>

        public JsonLocationNode(JsonLocationNode parent, string name)
        {
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            Parent        = parent;
            ComponentKind = JsonLocationNodeKind.Name;
            _name         = name;
            _index        = 0;
        }
        /// <summary>
        /// Constructs a normalized path from the last location node.
        ///
        /// </summary>
        public JsonLocation(JsonLocationNode lastNode)
        {
            var nodes             = new List <JsonLocationNode>();
            JsonLocationNode?node = lastNode;

            do
            {
                nodes.Add(node);
                node = node.Parent;
            }while (node != null);

            nodes.Reverse();

            _components = nodes;
        }
 protected void TailSelect(DynamicResources resources,
                           IValue root,
                           JsonLocationNode lastNode,
                           IValue current,
                           INodeReceiver receiver,
                           ProcessingFlags options,
                           int depth)
 {
     if (Tail == null)
     {
         receiver.Add(lastNode, current);
     }
     else
     {
         Tail.Select(resources, root, lastNode, current, receiver, options, depth);
     }
 }
 protected bool TryEvaluateTail(DynamicResources resources,
                                IValue root,
                                JsonLocationNode lastNode,
                                IValue current,
                                ProcessingFlags options,
                                out IValue value)
 {
     if (Tail == null)
     {
         value = current;
         return(true);
     }
     else
     {
         return(Tail.TryEvaluate(resources, root, lastNode, current, options, out value));
     }
 }
 public override void Select(DynamicResources resources,
                             IValue root,
                             JsonLocationNode lastNode,
                             IValue current,
                             INodeReceiver receiver,
                             ProcessingFlags options,
                             int depth)
 {
     if (current.ValueKind == JsonValueKind.Object)
     {
         IValue value;
         if (current.TryGetProperty(_identifier, out value))
         {
             this.TailSelect(resources, root,
                             PathGenerator.Generate(lastNode, _identifier, options),
                             value, receiver, options, depth);
         }
     }
 }
 public override bool TryEvaluate(DynamicResources resources,
                                  IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue result)
 {
     if (resources.TryRetrieveFromCache(_id, out result))
     {
         return(true);
     }
     else
     {
         if (!this.TryEvaluateTail(resources, root, lastNode, root, options, out result))
         {
             result = JsonConstants.Null;
             return(false);
         }
         resources.AddToCache(_id, result);
         return(true);
     }
 }
 public override bool TryEvaluate(DynamicResources resources, IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value)
 {
     if (current.ValueKind == JsonValueKind.Object)
     {
         IValue element;
         if (current.TryGetProperty(_identifier, out element))
         {
             return(this.TryEvaluateTail(resources, root,
                                         PathGenerator.Generate(lastNode, _identifier, options),
                                         element, options, out value));
         }
         else
         {
             value = JsonConstants.Null;
             return(true);
         }
     }
     else if (current.ValueKind == JsonValueKind.Array && _identifier == "length")
     {
         value = new DecimalValue(new Decimal(current.GetArrayLength()));
         return(true);
     }
     else if (current.ValueKind == JsonValueKind.String && _identifier == "length")
     {
         byte[] bytes = Encoding.UTF32.GetBytes(current.GetString().ToCharArray());
         value = new DecimalValue(new Decimal(current.GetString().Length));
         return(true);
     }
     else
     {
         value = JsonConstants.Null;
         return(true);
     }
 }
        public override bool TryEvaluate(DynamicResources resources, IValue root,
                                         JsonLocationNode lastNode,
                                         IValue current,
                                         ProcessingFlags options,
                                         out IValue results)
        {
            var           elements = new List <IValue>();
            INodeReceiver receiver = new ValueReceiver(elements);

            if (resources.Options.ExecutionMode == PathExecutionMode.Parallel)
            {
                receiver = new SynchronizedNodeReceiver(receiver);
            }
            Select(resources,
                   root,
                   lastNode,
                   current,
                   receiver,
                   options,
                   0);
            results = new ArrayValue(elements);
            return(true);
        }
 public void Select(DynamicResources resources,
                    IValue root,
                    JsonLocationNode lastNode,
                    IValue current,
                    INodeReceiver receiver,
                    ProcessingFlags options,
                    int depth)
 {
     if (resources.Options.ExecutionMode == PathExecutionMode.Sequential)
     {
         foreach (var selector in _selectors)
         {
             selector.Select(resources, root, lastNode, current, receiver, options, depth);
         }
     }
     else
     {
         Action <int> action = delegate(int i)
         {
             _selectors[i].Select(resources, root, lastNode, current, receiver, options, depth);
         };
         Parallel.For(0, _selectors.Count, action);
     }
 }
 public override bool TryEvaluate(DynamicResources resources, IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value)
 {
     if (current.ValueKind == JsonValueKind.Array)
     {
         if (_index >= 0 && _index < current.GetArrayLength())
         {
             return(this.TryEvaluateTail(resources, root,
                                         PathGenerator.Generate(lastNode, _index, options),
                                         current[_index], options, out value));
         }
         else
         {
             Int32 index = current.GetArrayLength() + _index;
             if (index >= 0 && index < current.GetArrayLength())
             {
                 return(this.TryEvaluateTail(resources, root,
                                             PathGenerator.Generate(lastNode, _index, options),
                                             current[index], options, out value));
             }
             else
             {
                 value = JsonConstants.Null;
                 return(true);
             }
         }
     }
     else
     {
         value = JsonConstants.Null;
         return(true);
     }
 }
        public override void Select(DynamicResources resources,
                                    IValue root,
                                    JsonLocationNode lastNode,
                                    IValue current,
                                    INodeReceiver receiver,
                                    ProcessingFlags options,
                                    int depth)
        {
            if (depth >= resources.Options.MaxDepth)
            {
                throw new InvalidOperationException($"Maximum depth level exceeded in recursive descent selector.");
            }

            if (current.ValueKind == JsonValueKind.Array)
            {
                this.TailSelect(resources, root, lastNode, current, receiver, options, depth + 1);
                Int32 index = 0;
                foreach (var item in current.EnumerateArray())
                {
                    Select(resources, root,
                           PathGenerator.Generate(lastNode, index, options),
                           item, receiver, options, depth + 1);
                    ++index;
                }
            }
            else if (current.ValueKind == JsonValueKind.Object)
            {
                this.TailSelect(resources, root, lastNode, current, receiver, options, depth + 1);
                foreach (var prop in current.EnumerateObject())
                {
                    Select(resources, root,
                           PathGenerator.Generate(lastNode, prop.Name, options),
                           prop.Value, receiver, options, depth + 1);
                }
            }
        }
 public void Add(JsonLocationNode lastNode, IValue value)
 {
     _accumulator.Add(lastNode, value);
 }
 public void Add(JsonLocationNode lastNode, IValue value)
 {
     _nodes.Add(new PathValuePair(new JsonLocation(lastNode), value.GetJsonElement()));
 }
 public void Add(JsonLocationNode lastNode, IValue value)
 {
     _values.Add(new JsonLocation(lastNode));
 }
 public void Add(JsonLocationNode lastNode, IValue value)
 {
     _values.Add(value);
 }
 public void Add(JsonLocationNode lastNode, IValue value)
 {
     _values.Add(value.GetJsonElement());
 }
 public abstract bool TryEvaluate(DynamicResources resources,
                                  IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value);