Package com.isomorphic.js
Class JSONFilter
java.lang.Object
com.isomorphic.js.JSONFilter
- All Implemented Interfaces:
Iterator
This class provides you to specify the properties of a Map, Bean or Collection of Maps or Beans to serialize to JSON when processed by JSTranslater.toJS(). When JSONFilter wraps a Bean or Map, the filter is applied directly to the Bean/Map. When JSONFilter wraps a Collection (or Iterator), the filter applies to the immediate members of the Collection or Iterator and only when those elements are Beans or Maps themselves. So, for example:
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 Summary
ConstructorsConstructorDescriptionJSONFilter
(Object obj, DataSource dataSource, boolean dropExtraFields) Creates a new JSONFilter.JSONFilter
(Object obj, IBeanFilter beanFilter) Creates a new JSONFilter.JSONFilter
(Object obj, Object[] propsToKeep) Creates a new JSONFilter.JSONFilter
(Object obj, String dataSource, boolean dropExtraFields) Creates a new JSONFilter.JSONFilter
(Object obj, Collection propsToKeep) Creates a new JSONFilter. -
Method Summary
-
Constructor Details
-
JSONFilter
Creates a new JSONFilter. The set of properties to keep is defined by the set of fields defined on a DataSource. This constructor applies a DataSourceBeanFilter to the object.- Parameters:
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.- See Also:
-
JSONFilter
Creates a new JSONFilter. The set of properties to keep is defined by the set of fields defined on a DataSource. This constructor applies a DataSourceBeanFilter to the object.- Parameters:
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.- See Also:
-
JSONFilter
Creates a new JSONFilter. This constructor applies a KeepPropertiesFilter to the object.- Parameters:
obj
- The object to serializepropsToKeep
- properties to keep.- See Also:
-
JSONFilter
Creates a new JSONFilter. This constructor applies a KeepPropertiesFilter to the object.- Parameters:
obj
- The object to serializepropsToKeep
- properties to keep.- See Also:
-
JSONFilter
Creates a new JSONFilter. This signature allows you to specify a custom class to convert the object to a Map, using the IBeanFilter interface.- Parameters:
obj
- The object to serializebeanFilter
- the beanFilter specifying which properties to keep- See Also:
-
-
Method Details
-
getBeanFilter
- Returns:
- the current bean filter
-
getObj
- Returns:
- object to serialize
-