Now that the System.Json API has been released (http://wcf.codeplex.com, http://wcf.codeplex.com/wikipage?title=WCF%20jQuery), I'll start posting a series of "improvements" that I've done over the past few months using that library. Let me know if you find it useful, and I'll bring it to the team to include in future releases of the Codeplex project.

To start, something simple: pretty printing. Like in Silverlight, when calling ToString() in a JsonValue object you get the JSON corresponding to the DOM. But in many cases, it's useful to have it pretty printed, for easier inspection or debugging. This is a simple extension method which accomplishes that.

Extension:

  1. public static class SystemJsonExtensions
  2. {
  3.     public static string PrettyPrint(this JsonValue jv)
  4.     {
  5.         StringBuilder sb = new StringBuilder();
  6.         PrettyPrint(sb, jv, 0, true);
  7.         return sb.ToString();
  8.     }
  9.     static void PrettyPrint(StringBuilder sb, JsonValue jv, int indent, bool first)
  10.     {
  11.         if (jv == null)
  12.         {
  13.             AddIndent(sb, indent);
  14.             sb.Append("null");
  15.         }
  16.         else
  17.         {
  18.             int i;
  19.             switch (jv.JsonType)
  20.             {
  21.                 case JsonType.Array:
  22.                     if (!first)
  23.                     {
  24.                         AddIndent(sb, indent);
  25.                     }
  26.  
  27.                     sb.AppendLine("[");
  28.                     for (i = 0; i < jv.Count; i++)
  29.                     {
  30.                         PrettyPrint(sb, jv[i], indent + 1, false);
  31.                         if (i < jv.Count - 1)
  32.                         {
  33.                             sb.AppendLine(",");
  34.                         }
  35.                         else
  36.                         {
  37.                             sb.AppendLine();
  38.                         }
  39.                     }
  40.                     AddIndent(sb, indent);
  41.                     sb.Append(']');
  42.                     break;
  43.                 case JsonType.Object:
  44.                     if (!first)
  45.                     {
  46.                         AddIndent(sb, indent);
  47.                     }
  48.                     else
  49.                     {
  50.                         first = false;
  51.                     }
  52.  
  53.                     sb.AppendLine("{");
  54.                     i = 0;
  55.                     foreach (string key in ((JsonObject)jv).Keys)
  56.                     {
  57.                         PrettyPrint(sb, new JsonPrimitive(key), indent + 1, true);
  58.                         sb.Append(": ");
  59.                         JsonValue value = jv[key];
  60.                         if (value == null || value.JsonType == JsonType.Boolean || value.JsonType == JsonType.Number || value.JsonType == JsonType.String)
  61.                         {
  62.                             PrettyPrint(sb, value, 0, true);
  63.                         }
  64.                         else
  65.                         {
  66.                             PrettyPrint(sb, value, indent + 1, true);
  67.                         }
  68.                         i++;
  69.                         if (i < jv.Count)
  70.                         {
  71.                             sb.AppendLine(",");
  72.                         }
  73.                         else
  74.                         {
  75.                             sb.AppendLine();
  76.                         }
  77.                     }
  78.                     AddIndent(sb, indent);
  79.                     sb.Append('}');
  80.                     break;
  81.                 case JsonType.Boolean:
  82.                 case JsonType.Number:
  83.                 case JsonType.String:
  84.                     AddIndent(sb, indent);
  85.                     sb.Append(jv.ToString());
  86.                     break;
  87.             }
  88.         }
  89.     }
  90.     static void AddIndent(StringBuilder sb, int indent)
  91.     {
  92.         for (int i = 0; i < indent * 2; i++) sb.Append(" ");
  93.     }
  94. }

Sample:

  1. public class Program
  2. {
  3.     public static void Main(string[] args)
  4.     {
  5.         JsonValue jv = new JsonObject
  6.         {
  7.             { "friends",
  8.                 new JsonArray {
  9.                     new JsonObject {
  10.                         { "Name", "Shaggy" },
  11.                         { "Age", 21 },
  12.                         { "Known Relatives", null },
  13.                     },
  14.                     new JsonObject {
  15.                         { "Name", "Scooby Doo" },
  16.                         { "Age", 8 },
  17.                         { "Known Relatives", new JsonArray { "Scrappy-Doo", "Scooby-Dum" } }
  18.                     },
  19.                 }
  20.             }
  21.         };
  22.  
  23.         Console.WriteLine(jv.PrettyPrint());
  24.     }
  25. }