Thursday, September 27th, 2007p>Charlie Hubbard has updated his well received Flexjson library and has released version 1.5.
There are two major feature updates:
You can now easily include and exclude items from the JSON:
- new JSONSerializer().exclude("*.class").serialize( obj );
- new JSONSerializer().exclude("foo.*.bar.*").prettyPrint( obj );
- new JSONSerializer().exclude("*.class").include("my.hobbies").serialize( obj );
- new JSONSerializer().exclude("foo.phoneNumbers").include("*").prettyPrint( obj );
Note that a new
prettyPrint() is there to output your JSON in an eye-pleasing way.
… 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