public override void Select(DynamicResources resources,
                             IValue root,
                             JsonLocationNode lastNode,
                             IValue current,
                             INodeReceiver receiver,
                             ProcessingFlags options,
                             int depth)
 {
     if (current.ValueKind == JsonValueKind.Array)
     {
         if (_index >= 0 && _index < current.GetArrayLength())
         {
             this.TailSelect(resources, root,
                             PathGenerator.Generate(lastNode, _index, options),
                             current[_index], receiver, options, depth);
         }
         else
         {
             Int32 index = current.GetArrayLength() + _index;
             if (index >= 0 && index < current.GetArrayLength())
             {
                 this.TailSelect(resources, root,
                                 PathGenerator.Generate(lastNode, _index, options),
                                 current[index], receiver, options, depth);
             }
         }
     }
 }
Beispiel #2
0
        internal static bool IsFalse(IValue val)
        {
            var comparer = ValueEqualityComparer.Instance;

            switch (val.Type)
            {
            case JmesPathType.False:
                return(true);

            case JmesPathType.Null:
                return(true);

            case JmesPathType.Array:
                return(val.GetArrayLength() == 0);

            case JmesPathType.Object:
                return(val.EnumerateObject().MoveNext() == false);

            case JmesPathType.String:
                return(val.GetString().Length == 0);

            case JmesPathType.Number:
                return(false);

            default:
                return(false);
            }
        }
Beispiel #3
0
        public override bool TryEvaluate(DynamicResources resources,
                                         IValue current,
                                         out IValue value)
        {
            if (current.Type != JmesPathType.Array)
            {
                value = JsonConstants.Null;
                return(true);
            }
            Int32 slen = current.GetArrayLength();

            if (_index >= 0 && _index < slen)
            {
                value = current[_index];
            }
            else if ((slen + _index) >= 0 && (slen + _index) < slen)
            {
                Int32 index = slen + _index;
                value = current[index];
            }
            else
            {
                value = JsonConstants.Null;
            }
            return(true);
        }
Beispiel #4
0
        internal static bool IsFalse(IValue val)
        {
            var comparer = ValueEqualityComparer.Instance;

            switch (val.ValueKind)
            {
            case JsonValueKind.False:
            case JsonValueKind.Null:
            case JsonValueKind.Undefined:
                return(true);

            case JsonValueKind.Array:
                return(val.GetArrayLength() == 0);

            case JsonValueKind.Object:
                return(val.EnumerateObject().MoveNext() == false);

            case JsonValueKind.String:
                return(val.GetString().Length == 0);

            case JsonValueKind.Number:
                return(false);

            default:
                return(false);
            }
        }
        public override void Select(DynamicResources resources,
                                    IValue root,
                                    JsonLocationNode lastNode,
                                    IValue current,
                                    INodeReceiver receiver,
                                    ProcessingFlags options,
                                    int depth)
        {
            if (current.ValueKind == JsonValueKind.Array)
            {
                Int32 start = _slice.GetStart(current.GetArrayLength());
                Int32 end   = _slice.GetStop(current.GetArrayLength());
                Int32 step  = _slice.Step;

                if (step > 0)
                {
                    if (start < 0)
                    {
                        start = 0;
                    }
                    if (end > current.GetArrayLength())
                    {
                        end = current.GetArrayLength();
                    }
                    for (Int32 i = start; i < end; i += step)
                    {
                        this.TailSelect(resources, root,
                                        PathGenerator.Generate(lastNode, i, options),
                                        current[i], receiver, options, depth);
                    }
                }
                else if (step < 0)
                {
                    if (start >= current.GetArrayLength())
                    {
                        start = current.GetArrayLength() - 1;
                    }
                    if (end < -1)
                    {
                        end = -1;
                    }
                    for (Int32 i = start; i > end; i += step)
                    {
                        if (i < current.GetArrayLength())
                        {
                            this.TailSelect(resources, root,
                                            PathGenerator.Generate(lastNode, i, options),
                                            current[i], receiver, options, depth);
                        }
                    }
                }
            }
        }
 public override bool TryEvaluate(DynamicResources resources, IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value)
 {
     if (current.ValueKind == JsonValueKind.Array)
     {
         if (_index >= 0 && _index < current.GetArrayLength())
         {
             return(this.TryEvaluateTail(resources, root,
                                         PathGenerator.Generate(lastNode, _index, options),
                                         current[_index], options, out value));
         }
         else
         {
             Int32 index = current.GetArrayLength() + _index;
             if (index >= 0 && index < current.GetArrayLength())
             {
                 return(this.TryEvaluateTail(resources, root,
                                             PathGenerator.Generate(lastNode, _index, options),
                                             current[index], options, out value));
             }
             else
             {
                 value = JsonConstants.Null;
                 return(true);
             }
         }
     }
     else
     {
         value = JsonConstants.Null;
         return(true);
     }
 }
 public override bool TryEvaluate(DynamicResources resources, IValue root,
                                  JsonLocationNode lastNode,
                                  IValue current,
                                  ProcessingFlags options,
                                  out IValue value)
 {
     if (current.ValueKind == JsonValueKind.Object)
     {
         IValue element;
         if (current.TryGetProperty(_identifier, out element))
         {
             return(this.TryEvaluateTail(resources, root,
                                         PathGenerator.Generate(lastNode, _identifier, options),
                                         element, options, out value));
         }
         else
         {
             value = JsonConstants.Null;
             return(true);
         }
     }
     else if (current.ValueKind == JsonValueKind.Array && _identifier == "length")
     {
         value = new DecimalValue(new Decimal(current.GetArrayLength()));
         return(true);
     }
     else if (current.ValueKind == JsonValueKind.String && _identifier == "length")
     {
         byte[] bytes = Encoding.UTF32.GetBytes(current.GetString().ToCharArray());
         value = new DecimalValue(new Decimal(current.GetString().Length));
         return(true);
     }
     else
     {
         value = JsonConstants.Null;
         return(true);
     }
 }
 bool TryGetValue(IValue root, JsonLocation path, out IValue element)
 {
     element = root;
     foreach (var pathComponent in path)
     {
         if (pathComponent.ComponentKind == JsonLocationNodeKind.Index)
         {
             if (element.ValueKind != JsonValueKind.Array || pathComponent.GetIndex() >= element.GetArrayLength())
             {
                 return(false);
             }
             element = element[pathComponent.GetIndex()];
         }
         else if (pathComponent.ComponentKind == JsonLocationNodeKind.Name)
         {
             if (element.ValueKind != JsonValueKind.Object || !element.TryGetProperty(pathComponent.GetName(), out element))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Beispiel #9
0
        public override bool TryEvaluate(DynamicResources resources,
                                         IValue current,
                                         out IValue value)
        {
            if (current.Type != JmesPathType.Array)
            {
                value = JsonConstants.Null;
                return(true);
            }

            var start = _slice.GetStart(current.GetArrayLength());
            var end   = _slice.GetStop(current.GetArrayLength());
            var step  = _slice.Step;

            if (step == 0)
            {
                value = JsonConstants.Null;
                return(false);;
            }

            var result = new List <IValue>();

            if (step > 0)
            {
                if (start < 0)
                {
                    start = 0;
                }
                if (end > current.GetArrayLength())
                {
                    end = current.GetArrayLength();
                }
                for (Int32 i = start; i < end; i += step)
                {
                    IValue val;
                    if (!this.TryApplyExpressions(resources, current[i], out val))
                    {
                        value = JsonConstants.Null;
                        return(false);
                    }
                    if (val.Type != JmesPathType.Null)
                    {
                        result.Add(val);
                    }
                }
            }
            else
            {
                if (start >= current.GetArrayLength())
                {
                    start = current.GetArrayLength() - 1;
                }
                if (end < -1)
                {
                    end = -1;
                }
                for (Int32 i = start; i > end; i += step)
                {
                    IValue val;
                    if (!this.TryApplyExpressions(resources, current[i], out val))
                    {
                        value = JsonConstants.Null;
                        return(false);
                    }
                    if (val.Type != JmesPathType.Null)
                    {
                        result.Add(val);
                    }
                }
            }

            value = new ArrayValue(result);
            return(true);
        }