Interface IToJSON
- All Known Implementing Classes:
AIDataSource,BasicDataSource,DataSource,GAEJPADataSource,HibernateDataSource,JPA2DataSource,JPADataSource,ODataDataSource,Project,RestConnector,RESTDataSource,Screen,SQLDataSource
-
Method Summary
Modifier and TypeMethodDescriptionvoidtoJSON(Writer out, JSTranslater jsTrans) Translate this object to executable JavaScript for transport to the browser.
-
Method Details
-
toJSON
Translate this object to executable JavaScript for transport to the browser.This example shows a simple way to customize Bean translation by implementing the IToJSON interface.
class MyBean implements IToJSON { public void toJSON (Writer out, JSTranslater jsTrans) { try { Map beanProps = DataTools.getProperties(this); // here, remove any properties you don't want to send // (or add or modify any others) jsTrans.toJS(beanProps, out); } catch (Exception ignored) {} } }In this example we use DataTools.getProperties() to get all of the Bean's public properties as a Map. This Map can be modified arbitrarily, then the JSTranslater is invoked to transform the Map to a JavaScript Object. The same approach can be used externally to an object if you can't add the IToJSON interface.
Note that this example reused the JSTranslater instance passed as a parameter - you should do this, rather than retrieving a new JSTranslater, because it preserves automatic recursion detection built into the JSTranslater class.
As an alternative to implementing IToJSON, you can wrap objects in a
JSONFilterbefore they are passed to the JSTranlater class.- Parameters:
out- Write your javascript translation of this object to this writer.jsTrans- The calling JSTranslater instance- Throws:
UnconvertableException- if the object cannot be converted to JSONIOException- if an I/O error occurs during writing- See Also:
-