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);
        }
예제 #2
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);
        }