Thursday, September 27th, 2007

Flexjson 1.5: Transformers and Filters

Category: Java, JavaScript, JSON, Library

<p>Charlie Hubbard has updated his well received Flexjson library and has released version 1.5.

There are two major feature updates:

Filters

You can now easily include and exclude items from the JSON:

  1. new JSONSerializer().exclude("*.class").serialize( obj );
  2. new JSONSerializer().exclude("foo.*.bar.*").prettyPrint( obj );
  3. new JSONSerializer().exclude("*.class").include("my.hobbies").serialize( obj );
  4. new JSONSerializer().exclude("foo.phoneNumbers").include("*").prettyPrint( obj );

Note that a new prettyPrint() is there to output your JSON in an eye-pleasing way.

Tranformers

… robots in disguise. Sorry.

“Transformers allow you to register classes that participate in serializing fields. It’s best understood as described by a use case. Say we have an object that represents an email, and we want to send it to the browser over JSON. But, emails can have illegal characters for HTML like < or > in fields like the to, from, or cc portions. Before we would have to create a separate method that would HTML encode those characters, and exclude the method that returned those values in plain text. Now we can register a transformer for those fields on the fly. Here’s how we can solve this problem:”

new JSONSerializer().transform( new HTMLEncoder(), “to”, “from”, “cc” ).prettyPrint( email );

Posted by Dion Almaer at 10:47 am
Comment here

+++--
3.8 rating from 15 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.