internal static void Handle(EvaluationContext context)
        {
            if (!context.Options.ExperimentalFeatures.ProcessDataReferences)
            {
                return;
            }

            foreach (var match in context.Current.ToList())
            {
                if (!IsReference(match.Value, out var reference))
                {
                    continue;
                }

                var newData = ResolveReference(reference, context.Options).GetAwaiter().GetResult();
                if (newData == null)
                {
                    continue;
                }

                var newMatch = new PathMatch(newData.Value, match.Location);
                var index    = context.Current.IndexOf(match);
                context.Current.RemoveAt(index);
                context.Current.Insert(index, newMatch);
            }
        }
        protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
        {
            switch (match.Value.ValueKind)
            {
            case JsonValueKind.Object:
                yield return(new PathMatch(match.Value.EnumerateObject().Count().AsJsonElement(), match.Location.Combine(PointerSegment.Create("$length"))));

                break;

            case JsonValueKind.Array:
                yield return(new PathMatch(match.Value.GetArrayLength().AsJsonElement(), match.Location.Combine(PointerSegment.Create("$length"))));

                break;
            }
        }
示例#3
0
        protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
        {
            switch (match.Value.ValueKind)
            {
            case JsonValueKind.Array:
                var array = match.Value.EnumerateArray().ToArray();
                IEnumerable <int> indices;
                indices = _ranges?.OfType <IArrayIndexExpression>()
                          .SelectMany(r => r.GetIndices(match.Value))
                          .OrderBy(i => i)
                          .Where(i => 0 <= i && i < array.Length)
                          .Distinct() ??
                          Enumerable.Range(0, array.Length);
                foreach (var index in indices)
                {
                    yield return(new PathMatch(array[index], match.Location.Combine(PointerSegment.Create(index.ToString()))));
                }
                break;

            case JsonValueKind.Object:
                if (_ranges != null)
                {
                    var props = _ranges.OfType <IObjectIndexExpression>()
                                .SelectMany(r => r.GetProperties(match.Value))
                                .Distinct();
                    foreach (var prop in props)
                    {
                        if (!match.Value.TryGetProperty(prop, out var value))
                        {
                            continue;
                        }
                        yield return(new PathMatch(value, match.Location.Combine(PointerSegment.Create(prop))));
                    }
                }
                else
                {
                    foreach (var prop in match.Value.EnumerateObject())
                    {
                        yield return(new PathMatch(prop.Value, match.Location.Combine(PointerSegment.Create(prop.Name))));
                    }
                }
                break;
            }
        }
示例#4
0
        private static IEnumerable <PathMatch> GetChildren(PathMatch match)
        {
            switch (match.Value.ValueKind)
            {
            case JsonValueKind.Object:
                yield return(match);

                foreach (var prop in match.Value.EnumerateObject())
                {
                    var newMatch = new PathMatch(prop.Value, match.Location.Combine(PointerSegment.Create(prop.Name)));
                    foreach (var child in GetChildren(newMatch))
                    {
                        yield return(child);
                    }
                }
                break;

            case JsonValueKind.Array:
                yield return(match);

                foreach (var(item, index) in match.Value.EnumerateArray().Select((item, i) => (item, i)))
                {
                    var newMatch = new PathMatch(item, match.Location.Combine(PointerSegment.Create($"{index}")));
                    foreach (var child in GetChildren(newMatch))
                    {
                        yield return(child);
                    }
                }
                break;

            case JsonValueKind.String:
            case JsonValueKind.Number:
            case JsonValueKind.True:
            case JsonValueKind.False:
            case JsonValueKind.Null:
                yield return(match);

                break;

            case JsonValueKind.Undefined:
            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#5
0
        protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
        {
            if (_name == null)
            {
                switch (match.Value.ValueKind)
                {
                case JsonValueKind.Object:
                    foreach (var propPair in match.Value.EnumerateObject())
                    {
                        yield return(new PathMatch(propPair.Value, match.Location.Combine(PointerSegment.Create(propPair.Name))));
                    }
                    break;

                case JsonValueKind.Array:
                    foreach (var(value, index) in match.Value.EnumerateArray().Select((v, i) => (v, i)))
                    {
                        yield return(new PathMatch(value, match.Location.Combine(PointerSegment.Create(index.ToString()))));
                    }
                    break;
                }

                yield break;
            }

            if (match.Value.ValueKind != JsonValueKind.Object)
            {
                yield break;
            }

            if (!match.Value.TryGetProperty(_name, out var prop))
            {
                yield break;
            }

            yield return(new PathMatch(prop, match.Location.Combine(PointerSegment.Create(_name))));
        }
 protected abstract IEnumerable <PathMatch> ProcessMatch(PathMatch match);
示例#7
0
 protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
 {
     return(new[] { match });
 }
示例#8
0
 protected override IEnumerable <PathMatch> ProcessMatch(PathMatch match)
 {
     return(GetChildren(match));
 }