Exemplo n.º 1
0
        public override bool TryEvaluate(DynamicResources resources, IList <IValue> args,
                                         out IValue result)
        {
            Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value);

            var arg0 = args[0];

            switch (arg0.Type)
            {
            case JmesPathType.Number:
                result = arg0;
                return(true);

            case JmesPathType.String:
            {
                var     s = arg0.GetString();
                Decimal dec;
                double  dbl;
                if (Decimal.TryParse(s, out dec))
                {
                    result = new DecimalValue(dec);
                    return(true);
                }
                else if (Double.TryParse(s, out dbl))
                {
                    result = new DoubleValue(dbl);
                    return(true);
                }
                else
                {
                    result = JsonConstants.Null;
                    return(false);
                }
            }

            default:
                result = JsonConstants.Null;
                return(false);
            }
        }
Exemplo n.º 2
0
        public override bool TryEvaluate(DynamicResources resources, IList <IValue> args, out IValue result)
        {
            Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value);

            var arg0 = args[0];

            if (arg0.Type != JmesPathType.Array || arg0.GetArrayLength() == 0)
            {
                result = JsonConstants.Null;
                return(false);
            }

            IValue sum;

            if (!SumFunction.Instance.TryEvaluate(resources, args, out sum))
            {
                result = JsonConstants.Null;
                return(false);
            }

            Decimal decVal;
            double  dblVal;

            if (sum.TryGetDecimal(out decVal))
            {
                result = new DecimalValue(decVal / arg0.GetArrayLength());
                return(true);
            }
            else if (sum.TryGetDouble(out dblVal))
            {
                result = new DoubleValue(dblVal / arg0.GetArrayLength());
                return(true);
            }
            else
            {
                result = JsonConstants.Null;
                return(false);
            }
        }
Exemplo n.º 3
0
        public override bool TryEvaluate(DynamicResources resources, IList <IValue> args,
                                         out IValue result)
        {
            Debug.Assert(this.Arity.HasValue && args.Count == this.Arity !.Value);

            var arg0 = args[0];

            switch (arg0.Type)
            {
            case JmesPathType.Object:
            {
                int count = 0;
                foreach (var item in arg0.EnumerateObject())
                {
                    ++count;
                }
                result = new DecimalValue(new Decimal(count));
                return(true);
            }

            case JmesPathType.Array:
                result = new DecimalValue(new Decimal(arg0.GetArrayLength()));
                return(true);

            case JmesPathType.String:
            {
                byte[] bytes = Encoding.UTF32.GetBytes(arg0.GetString().ToCharArray());
                result = new DecimalValue(new Decimal(bytes.Length / 4));
                return(true);
            }

            default:
            {
                result = JsonConstants.Null;
                return(false);
            }
            }
        }