Tuesday, April 17th, 2007

GWT-Jsonizer: JSON/JavaBeans translator

Category: GWT, JSON, Library

Andrés Testi has developed a JSON/JavaBean translator for the Google Web Toolkit named GWT Jsonizer, which simplifies the
GWT interaction with non-java backends.

The Jsonizer interface, has only two methods: ‘asJavaObject’ and ‘asString’. As possibly you can infer, the ‘asJavaObject’ method unjsonizes JSON values, and the ‘asString’ method, performs the opposite action.

Example

  1. /**
  2.  * If you wants to jsonize it, you need to implement an extension of
  3.  * Jsonizer interface suffixed with the 'Jsonizer' keyword.
  4.  */
  5. interface PersonJsonizer extends Jsonizer{}
  6.  
  7. // A JSON String with Person properties
  8. String json = '{'name':'Andres','age':28}';
  9.  
  10. // Create the Person Jsonizer
  11. PersonJsonizer jsonizer = (PersonJsonizer)GWT.create(PersonJsonizer.class);
  12. try{
  13.   // Translate the JSON String to a Person bean
  14.   Person p = (Person)JsonizerParser.parse(jsonizer, json);
  15. }catch(JsonizerException e){
  16.   Window.alert('JSON Translation Error!');
  17. }

Posted by Dion Almaer at 5:58 am
Comment here

+++--
3.9 rating from 27 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.