Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /hermes/bosnacweb04/bosnacweb04ae/b2501/ipg.parminderscom/apps/blog/wp-content/plugins/jetpack/jetpack.php on line 246 2012 January at Kirpa Apps Blog

Kirpa Apps Blog

Archive for January, 2012

Using Pretty Print to print JSON structure

without comments

I needed to build a “test-api” page for my team so that they could test there API without writing too much code. Nothing special, just a page with a textarea where someone puts in a request string, and another field, where we show the response sent by the server. The server response are in JSON format. For simple responses, everything was fine. But in cases, where the response was huge or had a complex structure, we wanted someway to show the structure in a more readable format. Something that helped developers see the hierarchy.

This is where pretty print (by James Padolsey) helped us.

Its dead simple to use:

[code language=”javascript”]

//include prettyprint .js:

//data contains the JSON response sent by the server.

$(“#detail”).append((prettyPrint($.parseJSON( data ), {
maxArray: 100,
expanded: false,
maxDepth: 100
})));

[/code]

E.g. Output:

 

Written by admin

January 31st, 2012 at 5:27 pm

Posted in Tech

Tagged with , ,

Skipping Fields when deserializing JSON using gson

without comments

Recently, we’re working on legacy code, where a json strings were to be serialized into java classes. These java classes had a deep hierarchy and some classes had field names same as defined in their parent classes. And these fields were not used at all.

GSON, makes it very simple to handle such scenarios. Use GsonBuilder and add an exclusionStrategy.

E.g.:

[code lang=”java”]
new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes field) {
if (field.getName().equals(“FIELD_NAME_TO_BE_SKIPED”)) {
return true;
}
return false;
}

@Override
public boolean shouldSkipClass(Class clazz) {
return clazz.equals(CLASS_TO_BE_SKIPPED.class);
}
}).create().fromJson(json, respClass);
[/code]

Written by admin

January 27th, 2012 at 4:13 pm

Posted in Tech

Tagged with , ,