Does what is says on the tin. I wrote this pretty printer well over a year ago after a failed attempt to find one written in C#. I find the light and simple JS notation very useful for analyzing small object graphs in a diagnostics trace. More so than XML. I've also included a quick and dirty serializer for such a purpose. Usual disclaimers apply; I wrote this in two hours, sitting in front of a TV.
issues
-
The Serializer uses a JavaScriptSerializer, which is known to (understandably) keel over when it enconters cyclic references in a given object graph.
outline
/// <summary>
/// A helper class for generating object
/// graphs in JavaScript notation.
/// Supports pretty printing.
/// </summary>
public class JsonHelper
{
public static string Serialize(Object obj);
public static string Serialize(
Object obj, bool format);
}
/// <summary>
/// Pretty prints the given input StringBuilder
/// in to the given output StringBuilder
/// </summary>
public class JsonPrettyPrinter
{
public void PrettyPrint(
StringBuilder in, StringBuilder out);
}
usage
// create your arbitrary object
var myObj = ...;
// serialize it with formatting, to make it
// human readable
string result = JsonHelper.Serialize(
myObj, true);
// output to the log
System.Diagnostics.Trace.WriteLine(result);
download
JsonHelper.cs