コード例 #1
0
        private static ExpressionTreeNode <TIn> _Visit <TIn>(JsonPathExpression expr)
        {
            if (expr is PathValueExpression <TIn> path)
            {
                return(path.Path);
            }
            if (expr is ValueExpression value)
            {
                return(new ValueExpression <TIn>(value.Value));
            }

            if (!(expr is OperatorExpression op))
            {
                throw new NotSupportedException($"Expressions of type {expr.GetType()} are not supported.");
            }

            var left = _Visit <TIn>(op.Children[0]);

            if (op.Operator == JsonPathOperator.Negate)
            {
                return(_VisitNegate(left));
            }
            if (op.Operator == JsonPathOperator.Not)
            {
                return(new NotExpression <TIn>(_MakeHasPropertyIfNameExpression(left)));
            }

            var right = _Visit <TIn>(op.Children.Count > 1
                                                        ? op.Children[1]
                                                        : throw new InvalidOperationException($"Operator type {op.Operator} requires two operands."));

            _CheckAndReplaceIfHasPropertyNeeded(op.Operator, ref left, ref right);
            return(op.Operator switch
            {
                JsonPathOperator.Add => (ExpressionTreeNode <TIn>) new AddExpression <TIn>(left, right),
                JsonPathOperator.And => new AndExpression <TIn>(left, right),
                JsonPathOperator.Divide => new DivideExpression <TIn>(left, right),
                JsonPathOperator.Exponent => new ExponentExpression <TIn>(left, right),
                JsonPathOperator.Equal => new IsEqualExpression <TIn>(left, right),
                JsonPathOperator.GreaterThan => new IsGreaterThanExpression <TIn>(left, right),
                JsonPathOperator.GreaterThanOrEqual => new IsGreaterThanEqualExpression <TIn>(left, right),
                JsonPathOperator.LessThan => new IsLessThanExpression <TIn>(left, right),
                JsonPathOperator.LessThanOrEqual => new IsLessThanEqualExpression <TIn>(left, right),
                JsonPathOperator.NotEqual => new IsNotEqualExpression <TIn>(left, right),
                JsonPathOperator.Modulo => new ModuloExpression <TIn>(left, right),
                JsonPathOperator.Multiply => new MultiplyExpression <TIn>(left, right),
                JsonPathOperator.Subtract => new SubtractExpression <TIn>(left, right),
                JsonPathOperator.Or => new OrExpression <TIn>(left, right),
                _ => throw new NotSupportedException($"Expressions of type {expr.GetType()} are not supported")
            });
コード例 #2
0
        private ExpressionTreeNode <TIn> _Visit <TIn>(JsonPathExpression expr)
        {
            if (expr is null)
            {
                return(null);
            }

            if (expr is PathValueExpression <TIn> path)
            {
                return(path.Path);
            }
            if (expr is ValueExpression value)
            {
                return new ValueExpression <TIn> {
                           Value = value.Value
                }
            }
            ;

            if (!(expr is OperatorExpression op))
            {
                throw new NotSupportedException($"Expressions of type {expr.GetType()} are not supported");
            }

            var left  = _Visit <TIn>(op.Children[0]);
            var right = _Visit <TIn>(op.Children.Count > 1 ? op.Children[1] : null);

            _CheckAndReplaceIfHasPropertyNeeded(op.Operator, ref left, ref right);
            switch (op.Operator)
            {
            case JsonPathOperator.Add:
                return(new AddExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.And:
                return(new AndExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Divide:
                return(new DivideExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Exponent:
                return(new ExponentExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Equal:
                return(new IsEqualExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.GreaterThan:
                return(new IsGreaterThanExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.GreaterThanOrEqual:
                return(new IsGreaterThanEqualExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.LessThan:
                return(new IsLessThanExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.LessThanOrEqual:
                return(new IsLessThanEqualExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.NotEqual:
                return(new IsNotEqualExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Modulo:
                return(new ModuloExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Multiply:
                return(new MultiplyExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Subtract:
                return(new SubtractExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Or:
                return(new OrExpression <TIn> {
                    Left = left, Right = right,
                });

            case JsonPathOperator.Negate:
                return(_VisitNegate(left));

            case JsonPathOperator.Not:
                return(new NotExpression <TIn> {
                    Root = _MakeHasPropertyIfNameExpression(left)
                });
            }

            throw new NotSupportedException($"Expressions of type {expr.GetType()} are not supported");
        }
コード例 #3
0
ファイル: Interpreter.cs プロジェクト: stanac/JsonPathway
        internal static IEnumerable <JsonElement> ExecuteInternal(JsonPathExpression expression, JsonElement element)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            switch (expression)
            {
            case PropertyAccessExpression pae:
                return(ExecuteInternal(pae, element));

            case ArrayElementsExpression aee:
                return(ExecuteInternal(aee, element));

            case FilterExpression fe:
                return(ExecuteInternal(fe, element));
            }

            throw new ArgumentOutOfRangeException($"No interpreter implementation found for {expression.GetType()}");
        }