Ejemplo n.º 1
0
        /// <summary>
        /// Formats and prints the resulting <see cref="ChangeList{T}"/> from a <see cref="DiffTrees(JsonNode, JsonNode)"/>.
        /// </summary>
        /// <returns>
        /// A string representation of the change list.
        /// </returns>
        public static string PrintTreeChangeList(ChangeList <JsonNode> changeList)
        {
            var treeToPrint = new PrintNode();

            for (int i = 0; i < changeList.Count; ++i)
            {
                var change = changeList[i];
                var node   = change.Value;
                var path   = new LinkedList <string>();
                // Use "it.Parent != null" instead of "it != null" to
                // ignore printing the root parent node that matches the opening bracket
                // for all JSON objects
                for (var it = node; it.Parent != null; it = it.Parent)
                {
                    path.AddFirst(it.Name);
                }

                var printNode = treeToPrint;
                // Build a tree of the change list values, placing nodes based off
                // their positions in the old or new tree
                foreach (var pathAtom in path)
                {
                    if (!printNode.Children.ContainsKey(pathAtom))
                    {
                        printNode.Children.Add(pathAtom, new PrintNode());
                    }

                    printNode = printNode.Children[pathAtom];
                }

                printNode.ChangedNodes.Add(change);
            }

            return(treeToPrint.ToString());
        }
Ejemplo n.º 2
0
 public override void VisitPrintNode(PrintNode p) => Text += p.ToString();