/// <summary>
        /// for UnitTest. Use explicit GetT() or Deserialize(ref T)
        /// </summary>
        /// <returns></returns>
        // public static object GetValue(this JsonTreeNode self)
        // {
        //     return self.Value.GetValue<object>();
        // }
        #endregion

        public static IEnumerable <JsonTreeNode> Traverse(this JsonTreeNode self)
        {
            yield return(self);

            if (self.IsArray())
            {
                foreach (var x in self.ArrayItems())
                {
                    foreach (var y in x.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
            else if (self.IsObject())
            {
                foreach (var kv in self.ObjectItems())
                {
                    foreach (var y in kv.Value.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
        }
Example #2
0
 public static int GetObjectCount(this JsonTreeNode self)
 {
     if (!self.IsObject())
     {
         throw new DeserializationException("is not object");
     }
     return(self.Children.Count() / 2);
 }
Example #3
0
        public static IEnumerable <KeyValuePair <JsonTreeNode, JsonTreeNode> > ObjectItems(this JsonTreeNode self)
        {
            if (!self.IsObject())
            {
                throw new DeserializationException("is not object");
            }
            var it = self.Children.GetEnumerator();

            while (it.MoveNext())
            {
                var key = it.Current;

                it.MoveNext();
                yield return(new KeyValuePair <JsonTreeNode, JsonTreeNode>(key, it.Current));
            }
        }
Example #4
0
        public static IEnumerable <JsonTreeNode> GetNodes(this JsonTreeNode self,
                                                          JsonPointer jsonPointer)
        {
            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.IsObject())
            {
                // 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
                {
                    JsonTreeNode 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();
            }
        }