Promo

Preface

Oh, hello.  Here lie a collection of articles, narratives and ponderings of computery things; finely blended with my portfolio bestowing works and experiments in U.I. design, infographics, and software development.  Bon appétit.

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

Categories

posts rss
Cynosura 0 rss
General 0 rss
Graphic Design 4 rss
Programming 6 rss
WWW Tech 2 rss

Archives for March 6. 2010

Freshly Cultivated

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

  1. 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

Author
Ray
Published
Sat. 6 March, 2010 07:05
1 Comments
comments open
Tags
filed under
Programming