public class JSONFilter
extends java.lang.Object
implements java.util.Iterator
String[] propsToKeep = {"foo", "bar"}; MyBean myBean = new MyBean(); JSTranslater.get().toJS(new JSONFilter(myBean, propsToKeep), out);The above would serialize only "foo" and "bar" properties of MyBean. If the above code passed a list of MyBean classes to the JSONFilter constructor, then the filter would be applied to each element of that list.
One of the JSONFilter constructors takes an IBeanFilter interface that allows you to pass a custom Object->Map converter. You can use this to convert complex beans that require more translation than a simple list of properties to include.
Note, this class implements Iterator
in order to support filtered output of Maps and Beans when streaming large datasets (see StreamingResponseIterator
). If the data member of a JSONFilter is not iterable - if it is a simple Map, for example - the iterator interface will still work, there will just be one iteration.
Also see the IToJSON interface, which you can implement to control JavaScript translation of your object regardless of how it is passed to the JSTranslater.
Constructor and Description |
---|
JSONFilter(java.lang.Object obj, java.util.Collection propsToKeep) Creates a new JSONFilter. |
JSONFilter(java.lang.Object obj, DataSource dataSource, boolean dropExtraFields) Creates a new JSONFilter. |
JSONFilter(java.lang.Object obj, IBeanFilter beanFilter) Creates a new JSONFilter. |
JSONFilter(java.lang.Object obj, java.lang.Object[] propsToKeep) Creates a new JSONFilter. |
JSONFilter(java.lang.Object obj, java.lang.String dataSource, boolean dropExtraFields) Creates a new JSONFilter. |
Modifier and Type | Method and Description |
---|---|
IBeanFilter | getBeanFilter() |
java.lang.Object | getObj() |
public JSONFilter(java.lang.Object obj, java.lang.String dataSource, boolean dropExtraFields)
obj
- The object to serializedataSource
- The name of the dataSource from which to fetch field names for use as the set of properties on the object to keep.DataSourceBeanFilter
public JSONFilter(java.lang.Object obj, DataSource dataSource, boolean dropExtraFields)
obj
- The object to serializedataSource
- The dataSource instance from which to fetch field names for use as the set of properties on the object to keep.DataSourceBeanFilter
public JSONFilter(java.lang.Object obj, java.util.Collection propsToKeep)
obj
- The object to serializepropsToKeep
- properties to keep.KeepPropertiesBeanFilter
public JSONFilter(java.lang.Object obj, java.lang.Object[] propsToKeep)
obj
- The object to serializepropsToKeep
- properties to keep.KeepPropertiesBeanFilter
public JSONFilter(java.lang.Object obj, IBeanFilter beanFilter)
obj
- The object to serializebeanFilter
- the beanFilter specifying which properties to keepIBeanFilter
public IBeanFilter getBeanFilter()
public java.lang.Object getObj()