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);
 }
        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);
                        }
                    }
                }
            }
        }
 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 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())
         {
             IValue val;
             if (_expr.TryEvaluate(resources, root, item, options, out val) &&
                 Expression.IsTrue(val))
             {
                 this.TailSelect(resources, root,
                                 PathGenerator.Generate(lastNode, index, options),
                                 item, receiver, options, depth);
             }
             ++index;
         }
     }
     else if (current.ValueKind == JsonValueKind.Object)
     {
         foreach (var property in current.EnumerateObject())
         {
             IValue val;
             if (_expr.TryEvaluate(resources, root, property.Value, options, out val) &&
                 Expression.IsTrue(val))
             {
                 this.TailSelect(resources, root,
                                 PathGenerator.Generate(lastNode, property.Name, options),
                                 property.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);
        }
Exemplo n.º 10
0
 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);
     }
 }
Exemplo n.º 11
0
        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);
                }
            }
        }
Exemplo n.º 12
0
 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);
     }
 }
Exemplo n.º 13
0
        public IList <PathValuePair> SelectNodes(JsonElement root,
                                                 JsonSelectorOptions?options = null)
        {
            DynamicResources resources;
            ProcessingFlags  flags = _requiredFlags;

            if (options != null)
            {
                if (options.NoDuplicates)
                {
                    flags |= ProcessingFlags.NoDups;
                }
                if (options.Sort)
                {
                    flags |= ProcessingFlags.SortByPath;
                }
                resources = new DynamicResources(options);
            }
            else
            {
                resources = new DynamicResources(JsonSelectorOptions.Default);
            }

            var           nodes    = new List <PathValuePair>();
            INodeReceiver receiver = new NodeReceiver(nodes);

            if (resources.Options.ExecutionMode == PathExecutionMode.Parallel)
            {
                receiver = new SynchronizedNodeReceiver(receiver);
            }
            _selector.Select(resources,
                             new JsonElementValue(root),
                             JsonLocationNode.Root,
                             new JsonElementValue(root),
                             receiver,
                             flags | ProcessingFlags.Path,
                             0);

            if ((flags & ProcessingFlags.SortByPath | flags & ProcessingFlags.NoDups) != 0)
            {
                if (nodes.Count > 1)
                {
                    if ((flags & ProcessingFlags.SortByPath) != 0)
                    {
                        nodes.Sort();
                    }
                    if ((flags & ProcessingFlags.NoDups) != 0)
                    {
                        var temp  = new List <PathValuePair>();
                        var index = new HashSet <PathValuePair>(nodes);
                        foreach (var path in nodes)
                        {
                            if (index.Contains(path))
                            {
                                temp.Add(path);
                                index.Remove(path);
                            }
                        }
                        nodes = temp;
                    }
                }
            }

            return(nodes);
        }
Exemplo n.º 14
0
 public abstract bool TryEvaluate(DynamicResources resources,
                                  IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value);
Exemplo n.º 15
0
        public bool TryEvaluate(DynamicResources resources,
                                IValue root,
                                IValue current,
                                ProcessingFlags options,
                                out IValue result)
        {
            Stack <IValue> stack    = new Stack <IValue>();
            IList <IValue> argStack = new List <IValue>();

            for (int i = _tokens.Count - 1; i >= 0; --i)
            {
                var token = _tokens[i];
                switch (token.Type)
                {
                case TokenType.Value:
                {
                    stack.Push(token.GetValue());
                    break;
                }

                case TokenType.RootNode:
                {
                    stack.Push(root);
                    break;
                }

                case TokenType.CurrentNode:
                {
                    stack.Push(current);
                    break;
                }

                case TokenType.UnaryOperator:
                {
                    Debug.Assert(stack.Count >= 1);
                    var    item = stack.Pop();
                    IValue value;
                    if (!token.GetUnaryOperator().TryEvaluate(item, out value))
                    {
                        result = JsonConstants.Null;
                        return(false);
                    }
                    stack.Push(value);
                    break;
                }

                case TokenType.BinaryOperator:
                {
                    Debug.Assert(stack.Count >= 2);
                    var rhs = stack.Pop();
                    var lhs = stack.Pop();

                    IValue value;
                    if (!token.GetBinaryOperator().TryEvaluate(lhs, rhs, out value))
                    {
                        result = JsonConstants.Null;
                        return(false);
                    }
                    stack.Push(value);
                    break;
                }

                case TokenType.Selector:
                {
                    Debug.Assert(stack.Count >= 1);
                    IValue val = stack.Peek();
                    stack.Pop();
                    IValue value;
                    if (token.GetSelector().TryEvaluate(resources, root, JsonLocationNode.Current, val, options, out value))
                    {
                        stack.Push(value);
                    }
                    else
                    {
                        result = JsonConstants.Null;
                        return(false);
                    }
                    break;
                }

                case TokenType.Argument:
                    Debug.Assert(stack.Count != 0);
                    argStack.Add(stack.Peek());
                    stack.Pop();
                    break;

                case TokenType.Function:
                {
                    if (token.GetFunction().Arity.HasValue&& token.GetFunction().Arity !.Value != argStack.Count)
                    {
                        result = JsonConstants.Null;
                        return(false);
                    }

                    IValue value;
                    if (!token.GetFunction().TryEvaluate(argStack, out value))
                    {
                        result = JsonConstants.Null;
                        return(false);
                    }
                    argStack.Clear();
                    stack.Push(value);
                    break;
                }

                case TokenType.Expression:
                {
                    IValue value;
                    if (!token.GetExpression().TryEvaluate(resources, root, current, options, out value))
                    {
                        result = JsonConstants.Null;
                        return(false);
                    }
                    else
                    {
                        stack.Push(value);
                    }
                    break;
                }

                default:
                    break;
                }
            }

            if (stack.Count == 0)
            {
                result = JsonConstants.Null;
                return(false);
            }
            else
            {
                result = stack.Pop();
                return(true);
            }
        }