Beispiel #1
0
 private static void ReformatIndexes(PathalizedJson jp1, PathalizedJson jp2)
 {
     if (jp1.IndexFormat != jp2.IndexFormat)
     {
         if (jp1.MaxChildren > jp2.MaxChildren)
         {
             var paths = jp2.Keys;
             foreach (var path in paths)
             {
                 var value = jp2[path];
                 jp2.Remove(path);
                 var newPath = path.Replace("[0", "[0" + new string('0', jp1.MaxChildren - jp2.MaxChildren));
                 jp2.Add(newPath, value);
             }
         }
         if (jp2.MaxChildren > jp1.MaxChildren)
         {
             var paths = jp1.Keys;
             foreach (var path in paths)
             {
                 var value = jp1[path];
                 jp1.Remove(path);
                 var newPath = path.Replace("[0", "[0" + new string('0', jp2.MaxChildren - jp1.MaxChildren));
                 jp1.Add(newPath, value);
             }
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Produces side-by-side output of two "pathalized" JSON structures.
        /// This method writes to the System Console.
        /// </summary>
        /// <param name="pathalizedJson1">The first pathalized JSON structure</param>
        /// <param name="pathalizedJson2">The second pathalized JSON structure</param>
        public static void Juxtapose(
            PathalizedJson pathalizedJson1,
            PathalizedJson pathalizedJson2)
        {
            ReformatIndexes(pathalizedJson1, pathalizedJson2);

            var allPaths = pathalizedJson1.Keys.Union(pathalizedJson2.Keys);

            var maxLen = allPaths.Select(x => x.Length).Max();
            var max1   = pathalizedJson1.Values.Where(x => x != null).Select(x => x.ToString().Length).Max();
            var max2   = pathalizedJson2.Values.Where(x => x != null).Select(x => x.ToString().Length).Max();

            foreach (var path in allPaths)
            {
                Console.Write(path);
                if (maxLen > path.Length)
                {
                    Console.Write(new string(' ', maxLen - path.Length));
                }
                Console.Write(" ");
                string s = "";
                if (pathalizedJson1.TryGetValue(path, out object val1))
                {
                    s = val1 == null ? "" : val1.ToString();
                }
                else if (max1 > s.Length)
                {
                    s = new string('~', max1 - s.Length);
                }

                Console.Write(s);
                if (max1 > s.Length)
                {
                    Console.Write(new string(' ', max1 - s.Length));
                }
                Console.Write(" ");

                if (pathalizedJson2.TryGetValue(path, out object val2))
                {
                    s = val2 == null ? "" : val2.ToString();
                }
                else if (max2 > s.Length)
                {
                    s = new string('~', max2 - s.Length);
                }

                Console.Write(s);
                if (max2 > s.Length)
                {
                    Console.Write(new string(' ', max2 - s.Length));
                }
                Console.Write(" ");

                if (val1?.ToString() != val2?.ToString())
                {
                    Console.WriteLine("X");
                }
                else
                {
                    Console.WriteLine("");
                }
            }
        }