Example #1
0
        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);
                    }
                }
            }
        }
Example #2
0
        public static ListTreeNode <T> GetArrrayItem <T>(this ListTreeNode <T> self, int index)
            where T : IListTreeItem, IValue <T>
        {
            int i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (i++ == index)
                {
                    return(v);
                }
            }
            throw new KeyNotFoundException();
        }
Example #3
0
        public static int IndexOf <T>(this ListTreeNode <T> self, ListTreeNode <T> child)
            where T : IListTreeItem, IValue <T>
        {
            int i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (v.ValueIndex == child.ValueIndex)
                {
                    return(i);
                }
                ++i;
            }
            throw new KeyNotFoundException();
        }
Example #4
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()];
            int i = 0;

            foreach (var x in s.ArrayItems())
            {
                x.Deserialize(ref u[i++]);
            }
            return(u);
        }
Example #5
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);
        }
        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
                {
                    int 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();
            }
        }
Example #7
0
        public IEnumerable <JsonDiff> Diff(ListTreeNode <T> rhs, JsonPointer path = default(JsonPointer))
        {
            switch (Value.ValueType)
            {
            case ValueNodeType.Null:
            case ValueNodeType.Boolean:
            case ValueNodeType.Number:
            case ValueNodeType.Integer:
            case ValueNodeType.String:
                if (!Equals(rhs))
                {
                    yield return(JsonDiff.Create(this, JsonDiffType.ValueChanged, string.Format("{0} => {1}", Value, rhs.Value)));
                }
                yield break;
            }

            if (Value.ValueType != rhs.Value.ValueType)
            {
                yield return(JsonDiff.Create(this, JsonDiffType.ValueChanged, string.Format("{0} => {1}", Value.ValueType, rhs.Value)));

                yield break;
            }

            if (Value.ValueType == ValueNodeType.Object)
            {
                var l = this.ObjectItems().ToDictionary(x => x.Key, x => x.Value);
                var r = rhs.ObjectItems().ToDictionary(x => x.Key, x => x.Value);

                foreach (var kv in l)
                {
                    ListTreeNode <T> x;
                    if (r.TryGetValue(kv.Key, out x))
                    {
                        r.Remove(kv.Key);
                        // Found
                        foreach (var y in kv.Value.Diff(x))
                        {
                            yield return(y);
                        }
                    }
                    else
                    {
                        // Removed
                        yield return(JsonDiff.Create(kv.Value, JsonDiffType.KeyRemoved, kv.Value.Value.ToString()));
                    }
                }

                foreach (var kv in r)
                {
                    // Addded
                    yield return(JsonDiff.Create(kv.Value, JsonDiffType.KeyAdded, kv.Value.Value.ToString()));
                }
            }
            else if (Value.ValueType == ValueNodeType.Array)
            {
                var ll = this.ArrayItems().GetEnumerator();
                var rr = rhs.ArrayItems().GetEnumerator();
                while (true)
                {
                    var lll = ll.MoveNext();
                    var rrr = rr.MoveNext();
                    if (lll && rrr)
                    {
                        // found
                        foreach (var y in ll.Current.Diff(rr.Current))
                        {
                            yield return(y);
                        }
                    }
                    else if (lll)
                    {
                        yield return(JsonDiff.Create(ll.Current, JsonDiffType.KeyRemoved, ll.Current.Value.ToString()));
                    }
                    else if (rrr)
                    {
                        yield return(JsonDiff.Create(rr.Current, JsonDiffType.KeyAdded, rr.Current.Value.ToString()));
                    }
                    else
                    {
                        // end
                        break;
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }