Ejemplo n.º 1
0
 public override JmesPathArgument Transform(JmesPathArgument argument)
 {
     return(argument.IsProjection
         ? Project(argument)
         : base.Transform(argument)
            );
 }
        public override JmesPathArgument Project(JmesPathArgument argument)
        {
            if (argument.IsProjection)
            {
                argument = argument.AsJToken();
            }

            var array = argument.Token as JArray;

            if (array == null)
            {
                return(null);
            }

            var items = new List <JmesPathArgument>();

            foreach (var item in array)
            {
                var result = expression_.Transform(item);
                if (!JmesPathArgument.IsFalse(result))
                {
                    items.Add(item);
                }
            }

            return(new JmesPathArgument(items));
        }
        public override JmesPathArgument Project(JmesPathArgument argument)
        {
            if (argument.IsProjection)
            {
                argument = argument.AsJToken();
            }

            var items = new List <JmesPathArgument>();

            var array = argument.Token as JArray;

            if (array == null)
            {
                return(null);
            }

            foreach (var item in array)
            {
                var nested = item as JArray;
                if (nested == null)
                {
                    items.Add(item);
                }

                else
                {
                    items.AddRange(nested.Select(i => (JmesPathArgument)i));
                }
            }

            return(new JmesPathArgument(items));
        }
        protected override JmesPathArgument Transform(JToken json)
        {
            var token = base.Transform(json);

            return(JmesPathArgument.IsFalse(token)
                ? JmesPathArgument.True
                : JmesPathArgument.False
                   );
        }
        public override JmesPathArgument Project(JmesPathArgument argument)
        {
            if (argument.Projection != null)
            {
                return(argument);
            }

            var array = argument.Token as JArray;

            if (array == null)
            {
                return(JmesPathArgument.Null);
            }

            var items = array
                        .Select(i => (JmesPathArgument)i)
            ;

            return(new JmesPathArgument(items));
        }
        public override JmesPathArgument Project(JmesPathArgument argument)
        {
            if (argument.Projection != null)
            {
                return(argument);
            }

            var item = argument.Token as JObject;

            if (item == null)
            {
                return(JmesPathArgument.Null);
            }

            var hashes =
                item
                .Properties()
                .Select(p => (JmesPathArgument)p.Value)
            ;

            return(new JmesPathArgument(hashes));
        }
        /// <summary>
        /// Evaluates the expression against the specified JSON object.
        /// The result cannot be null and is:
        /// either a valid JSON found in the resulting <see cref="JmesPathArgument"/>'s Token property.
        /// or a projection found in the resulting <see cref="JmesPathArgument"/>'s Projection property.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        public virtual JmesPathArgument Transform(JmesPathArgument argument)
        {
            if (argument.IsProjection)
            {
                var items = new List <JmesPathArgument>();
                foreach (var projected in argument.Projection)
                {
                    var item = Transform(projected);
                    if (item.IsProjection)
                    {
                        items.Add(item);
                    }
                    else if (item.Token != JTokens.Null)
                    {
                        items.Add(item);
                    }
                }

                return(new JmesPathArgument(items));
            }

            return(Transform(argument.Token));
        }
Ejemplo n.º 8
0
        public override JmesPathArgument Project(JmesPathArgument argument)
        {
            if (argument.IsProjection)
            {
                argument = argument.AsJToken();
            }

            var json = argument.Token;

            if (json.Type != JTokenType.Array)
            {
                return(null);
            }

            // slice expression adhere to the following rule:
            // if the element being sliced is not an array, the result is null.

            var array = json as JArray;

            if (array == null)
            {
                return(null);
            }

            // slice expressions adhere to the following rules:
            // if the given step is omitted, it it assumed to be 1.

            var step = step_ ?? 1;

            // if the given step is 0, an error MUST be raised.
            // no runtime check here - the parser will ensure that 0 is not a valid value

            System.Diagnostics.Debug.Assert(step != 0);

            var length = array.Count;

            // if no start position is given, it is assumed to be 0 if the given step is greater than 0 or the end of the array if the given step is less than 0.

            var start = start_ ?? (step > 0 ? 0 : length - 1);

            // if a negative start position is given, it is calculated as the total length of the array plus the given start position.

            if (start_.HasValue && start_.Value < 0)
            {
                start = length + start_.Value;
            }

            // if no stop position is given, it is assumed to be the length of the array if the given step is greater than 0 or 0 if the given step is less than 0.

            var stop = stop_ ?? (step > 0 ? length : -1);

            // if a negative stop position is given, it is calculated as the total length of the array plus the given stop position.

            if (stop_.HasValue && stop_.Value < 0)
            {
                stop = length + stop_.Value;
            }

            // if the element being sliced is an array and yields no results, the result MUST be an empty array.

            var items = new List <JToken>();

            for (var index = start; (step > 0 ? index <stop : index> stop); index += step)
            {
                if (index >= 0 && index < length)
                {
                    items.Add(array[index]);
                }
            }

            var arguments = items.Select(i => (JmesPathArgument)i);

            return(new JmesPathArgument(arguments));
        }
Ejemplo n.º 9
0
 public abstract JmesPathArgument Project(JmesPathArgument argument);
        protected override JmesPathArgument Transform(JToken json)
        {
            var token = Left.Transform(json);

            return(!JmesPathArgument.IsFalse(token) ? token : Right.Transform(json));
        }