Пример #1
0
 private static void ToJson(Node node, StringBuilder sb)
 {
     sb.Append("{");
     sb.AppendValue("kind", node.Kind);
     sb.Append(",");
     sb.AppendValue("name", node.Name);
     sb.Append(",");
     sb.AppendValue("type", node.Type);
     sb.Append(",");
     sb.AppendValue("value", node.Value);
     sb.Append(",");
     sb.Append("\"children\":[");
     var first = false;
     foreach (var child in node.Children)
     {
         if (!first)
         {
             ToJson(child, sb);
             first = true;
             continue;
         }
         sb.Append(",");
         ToJson(child, sb);
     }
     sb.Append("]");
     sb.Append("}");
 }
Пример #2
0
        private Node Process(string kind, string name, object o, int level)
        {
            level++;
            var node = new Node {Kind = kind, Name = name};

            if (o == null)
            {
                node.Type = "unknown";
                node.Value = "null";
                return node;
            }

            var type = o.GetType();

            node.Type = type.GetFullName();
            node.Value = o.ToString();

            List<Node> children;
            if (type.IsPrimitive || o is String)
            {
                return node;
            }

            if (level == _depth)
            {
                children = new List<Node> {new Node {Kind = "end"}};
            }
            else if (o is IEnumerable)
            {
                children = HandleEnumerable(o, level);
            }
            else
            {
                children = HandleObject(type, o, level);
            }

            node.Children = children;
            return node;
        }