Social and professional networking

View Ian Clarke's profile on LinkedIn
Shameless plug

Does your company's revenue depend on being able to predict the future based on past data?  SenseArray may be able to help.

RSS
Links
« Simple bash script to name a tab in iTerm | Main | Which is the best Java JSON library? »
Sunday
Oct182009

A good pretty-printer for Google Gson

I decided to do something about the one thing I wasn't happy about in Google Gson, the lack of good pretty-printing.

I created a class called GsonPrettyPrinter which outputs nice readable JSON in a reasonably intelligent way, you can grab the source code here.

Here is what the output looks like:

Note, in particular:

  • Requires at least Gson 1.4 (the current version at the time of writing)
  • It will try to represent JSON objects on arrays on a single line if it can
  • It sorts JSON objects by their keys
  • The code is in the public domain, with a request for attribution

 

Reader Comments (7)

The link to the source is broken :(

October 18, 2009 | Unregistered CommenterAntonio Ognio

You are correct, I guess github deleted it because it was a "private" gist and was getting too much traffic.

I've replaced it with a public gist, so hopefully they won't do that again.

October 18, 2009 | Registered CommenterIan Clarke

My apologies for barging in here, knowing nothing about any of this---and not belonging here at all. But I just wanted to say, Geewhiz! If I knew about all of this stuff, my hypergraphia could take off in an entirely new direction. Unfortunately, I don't induge mine. It indulges me a moment or two of calm every once in awhile. Oh well.

October 18, 2009 | Unregistered CommenterHarold Knight

Thank you for this code! It's simple and gets the job done! You saved me a bunch of time!

I had to tweak it because we are currently using Gson 1.3, and I added a couple of ppJson() overloads, one that takes a JsonElement in case you already have one all parsed and one that takes a String with Json in it, and returns it pretty-printed. That's what I really needed was to reformat unformatted Json.

Besides the added methods, I modified the ppJson method that calls gson.toJsonTree() to construct a JsonElement. For Gson 1.3, you use gson to make the JSON string then you create a JsonElement from that. Below is the modified method and the two new overloads.

Thanks again!


public void ppJson(final Object o, final PrintWriter pw, Type t) {

final String jsonString = gson.toJson(o, t);
final JsonParser parser = new JsonParser();

final JsonElement jsonTree = parser.parse(jsonString);
final List<String> stringList = toStringList(jsonTree);
for (final String s : stringList) {
pw.println(s);
}
}

public String ppJson(final String jsonString) {
final JsonParser parser = new JsonParser();
final JsonElement jsonTree = parser.parse(jsonString);

final StringWriter sw = new StringWriter(128);
final PrintWriter pw = new PrintWriter(sw);
ppJson(jsonTree, pw);

return new String(sw.toString());
}

public void ppJson(final JsonElement jsonElement, final PrintWriter pw) {
final List<String> stringList = toStringList(jsonElement);
for (final String s : stringList) {
pw.println(s);
}
}

December 17, 2009 | Unregistered CommenterMartin Leon

thanks for that one ! it does the job beautifully

(i don't get why such a formatter is not included is the default distribution :/)

April 21, 2010 | Unregistered Commentermots

thanks for the code. it makes my work pretty

April 30, 2010 | Unregistered Commenter11x17 printer

A sail veering about the blank bay waiting for a swollen designer sunglasses wholesale bundle to bob up, roll over to the sun a puffy replica sunglasses wholesale face, salt white. Here I am. They followed the winding path down to the creek. Buck Mulligan stood on a stone, in shirtsleeves, his wholesale cheap sunglasses unclipped tie rippling over his shoulder. A young man clinging to a spur of rock near him moved slowly frogwise his green sunglass wholesale legs in the deep jelly of the water. Buck Mulligan sat down to unlace his wholesale oakley sunglasses boots. An elderly man shot up near the spur of rock a blowing red sunglasses for wholesale face. He scrambled up by the stones, water glistening on his fashion sunglasses wholesale pate and on its garland of grey hair, water rilling over his chest and paunch and spilling jets out of his wholesale sunglasses china black sagging loincloth.

July 13, 2010 | Unregistered Commentersunglass wholesale

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>