GWT-Jsonizer: JSON/JavaBeans translator

Written by on April 17th, 2007 in Ajax News.

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

JAVA:

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

Source: Ajaxian
Original Article: http://ajaxian.com/archives/gwt-jsonizer-jsonjavabeans-translator

Leave a Reply

You must be logged in to post a comment.



Site Navigation