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

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Categories

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

Json Pretty Printer

for C#

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
Rating
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Bookmarks

Commentary

Comments are closed