Esempio n. 1
0
 private static JNode ExpandValue(JNode node, string name)
 {
     if (node is JContentNode jcn)
     {
         return(jcn[name] as JNode);
     }
     JUtils.ValidateOutOfBounds("Cannot expand a node. Path does not exist.");
     throw new NotImplementedException("This exception should never be hit");
 }
Esempio n. 2
0
        public static JNode Expand(IEnumerable <JSegment> segments, JNode json)
        {
            JNode value = json;

            foreach (var jSegment in segments)
            {
                value = jSegment._expression.Invoke(value);
            }

            return(value);
        }
Esempio n. 3
0
        private static JNode ExpandIndexer(JNode node, string indexer)
        {
            if (node is JEnumerationNode jen && jen.Value is IEnumerable <JNode> itms)
            {
                if (int.TryParse(indexer, out var idx))
                {
                    return(jen[idx] as JNode);
                }

                if (indexer.Contains(":"))
                {
                    int?start = null, end = null;
                    var parts = indexer.Split(':');
                    if (parts.Length != 2)
                    {
                        JUtils.ValidateOutOfBounds("Range indexer is incorrectly formed.");
                    }
                    if (int.TryParse(parts[0], out var s))
                    {
                        start = s;
                    }
                    if (int.TryParse(parts[1], out var e))
                    {
                        end = e;
                    }
                    if (!start.HasValue)
                    {
                        start = 0;
                    }
                    if (!end.HasValue || end.Value == -1)
                    {
                        end = itms.Count() - 1;
                    }
                    List <JNode> subrange = new List <JNode>();
                    for (int i = start.Value; i < end; i++)
                    {
                        subrange.Add(itms.ElementAt(i));
                    }
                    return(new JEnumerationNode(subrange));
                }
                else if (indexer.Contains(","))
                {
                    var          values   = indexer.Split(',').Select(int.Parse);
                    List <JNode> subrange = new List <JNode>();
                    foreach (var value in values)
                    {
                        subrange.Add(itms.ElementAt(value));
                    }
                    return(new JEnumerationNode(subrange));
                }
            }
            JUtils.ValidateOutOfBounds("Cannot expand an indexer. Path cannot be indexed.");
            throw new NotImplementedException("This exception should never be hit");
        }
Esempio n. 4
0
        private static JNode ExpandValueRecursive(JNode node, string name)
        {
            if (node.Type == JNode.NodeType.Leaf)
            {
                JUtils.ValidateOutOfBounds("Cannot expand a node. Path does not exist.");
            }
            if (node.Type == JNode.NodeType.Enumeration)
            {
                JUtils.ValidateOutOfBounds("Cannot expand a node. Path does not exist.");
            }
            var combined = ExpandNodesRecursive(node as JContentNode, name);

            return(new JEnumerationNode(combined));
        }
Esempio n. 5
0
        public static T Parse <T>(JNode value)
        {
            if (typeof(T) != typeof(string) && typeof(IEnumerable).IsAssignableFrom(typeof(T)) &&
                value is JEnumerationNode enu)
            {
                //return ParseEnumerableContract<T>(value);
                var arg0    = JUtils.GetGenericTypeFromEnumerable <T>();
                var method  = JUtils.GetCastMethodForInputType <JEnumerationNode>();
                var generic = method.MakeGenericMethod(arg0);
                var result  = generic.Invoke(null, new[] { enu });
                return((T)result);
            }
            else if (value is JEnumerationNode jen)
            {
                var data = jen.Cast <T>();
                if (data == null)
                {
                    data = Enumerable.Empty <T>();
                }
                var enumerable = data as T[] ?? data.ToArray();
                if (enumerable.Length > 1)
                {
                    JUtils.ValidateOutOfBounds();
                }

                return(enumerable.FirstOrDefault());
            }
            else if (value is JValueNode jvn)
            {
                //return ParseObjectContract<T>(value);
                return(jvn.Cast <T>());
            }
            else if (value is JContentNode jcn)
            {
                return(jcn.Cast <T>());
            }
            else
            {
                JUtils.ValidateOutOfBounds();
            }
            throw new NotImplementedException("This should not be hit. I'm responsible if it is.");
        }
Esempio n. 6
0
        private static JNode ExpandWildcard(JNode node)
        {
            throw new NotImplementedException("This feature is not implemented yet.");
            //TODO
            //all of the child nodes should be expanded into a single content node recusively
            //all of the immediate child nodes shoul be expanded into an enumeration

#pragma warning disable 162
            if (node is JEnumerationNode jen)
            {
                return(jen);
            }
            if (node is JValueNode jvn)
            {
                return(jvn);
            }
            if (node is JContentNode jcn)
            {
                return(new JEnumerationNode((jcn.Value as IDictionary <string, JNode>)?.Select(x => x.Value)));
            }
            JUtils.ValidateOutOfBounds("Cannot expand a wildcard node. Path does not conform.");
            throw new NotImplementedException("This exception should never be hit");
#pragma warning restore 162
        }
Esempio n. 7
0
        public static JNode Expand(string expression, JNode json)
        {
            var segments = Parse(expression);

            return(Expand(segments, json));
        }
Esempio n. 8
0
 private static JNode ExpandRoot(JNode node)
 {
     return(node ?? null);
 }
Esempio n. 9
0
        public static T Find <T>(string expression, JNode json)
        {
            var result = Find(expression, json);

            return(Parse <T>(result));
        }
Esempio n. 10
0
 public static JNode Find(IEnumerable <JSegment> expression, JNode json)
 {
     return(JSegment.Expand(expression, json));
 }
Esempio n. 11
0
 public static JNode Find(string expression, JNode json)
 {
     return(JSegment.Expand(expression, json));
 }
Esempio n. 12
0
        public static JNode Parse(string value)
        {
            var buffer = new StringBuffer(value);

            return(JNode.ParseNode(buffer));
        }
Esempio n. 13
0
        public static T Find <T>(IEnumerable <JSegment> expression, JNode json)
        {
            var result = Find(expression, json);

            return(Parse <T>(result));
        }