public static IEnumerable <ListTreeNode <T> > Traverse <T>(this ListTreeNode <T> self)
            where T : IListTreeItem, IValue <T>
        {
            yield return(self);

            if (self.IsArray())
            {
                foreach (var x in self.ArrayItems())
                {
                    foreach (var y in x.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
            else if (self.IsMap())
            {
                foreach (var kv in self.ObjectItems())
                {
                    foreach (var y in kv.Value.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
        }
 public static IEnumerable <ListTreeNode <T> > ArrayItems <T>(this ListTreeNode <T> self)
     where T : IListTreeItem, IValue <T>
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children);
 }
 public static int GetArrayCount <T>(this ListTreeNode <T> self)
     where T : IListTreeItem, IValue <T>
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children.Count());
 }
Esempio n. 4
0
        private static V[] ArrayCreator <V>(ListTreeNode <T> src)
        {
            if (!src.IsArray())
            {
                throw new ArgumentException("value is not array");
            }
            var count = src.GetArrayCount();

            return(new V[count]);
        }
Esempio n. 5
0
        public static V[] GenericArrayDeserializer <V>(ListTreeNode <T> s)
        {
            if (!s.IsArray())
            {
                throw new ArgumentException("not array: " + s.Value.ValueType);
            }
            var u = new V[s.GetArrayCount()];
            var i = 0;

            foreach (var x in s.ArrayItems())
            {
                x.Deserialize(ref u[i++]);
            }
            return(u);
        }
Esempio n. 6
0
        public static List <V> GenericListDeserializer <V>(ListTreeNode <T> s)
        {
            if (!s.IsArray())
            {
                throw new ArgumentException("not array: " + s.Value.ValueType);
            }
            var u = new List <V>(s.GetArrayCount());

            foreach (var x in s.ArrayItems())
            {
                var e = default(V);
                x.Deserialize(ref e);
                u.Add(e);
            }

            return(u);
        }
Esempio n. 7
0
        public bool FromJsonSchema(IFileSystemAccessor fs, string key, ListTreeNode <JsonValue> value)
        {
            switch (key)
            {
            case "items":
                if (value.IsArray())
                {
                    throw new NotImplementedException();
                }
                else
                {
                    var sub = new JsonSchema();
                    sub.Parse(fs, value, "items");
                    Items = sub;
                }

                return(true);

            case "additionalItems":
                return(true);

            case "maxItems":
                MaxItems = value.GetInt32();
                return(true);

            case "minItems":
                MinItems = value.GetInt32();
                return(true);

            case "uniqueItems":
                return(true);

            case "contains":
                return(true);
            }

            return(false);
        }
Esempio n. 8
0
        public static IEnumerable <ListTreeNode <T> > GetNodes <T>(this ListTreeNode <T> self,
                                                                   JsonPointer jsonPointer)
            where T : IListTreeItem, IValue <T>
        {
            if (jsonPointer.Path.Count == 0)
            {
                yield return(self);

                yield break;
            }

            if (self.IsArray())
            {
                // array
                if (jsonPointer[0][0] == '*')
                {
                    // wildcard
                    foreach (var child in self.ArrayItems())
                    {
                        foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                        {
                            yield return(childChild);
                        }
                    }
                }
                else
                {
                    var index = jsonPointer[0].ToInt32();
                    var child = self.ArrayItems().Skip(index).First();
                    foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                    {
                        yield return(childChild);
                    }
                }
            }
            else if (self.IsMap())
            {
                // object
                if (jsonPointer[0][0] == '*')
                {
                    // wildcard
                    foreach (var kv in self.ObjectItems())
                    {
                        foreach (var childChild in kv.Value.GetNodes(jsonPointer.Unshift()))
                        {
                            yield return(childChild);
                        }
                    }
                }
                else
                {
                    ListTreeNode <T> child;
                    try
                    {
                        child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value;
                    }
                    catch (Exception)
                    {
                        // key
                        self.AddKey(jsonPointer[0]);
                        // value
                        self.AddValue(default(ArraySegment <byte>), ValueNodeType.Object);

                        child = self.ObjectItems().First(x => x.Key.GetUtf8String() == jsonPointer[0]).Value;
                    }

                    foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                    {
                        yield return(childChild);
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }